forked from Kong/docs.konghq.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
463 lines (392 loc) · 11.9 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
'use strict'
var browserSync = require('browser-sync').create()
var childProcess = require('child_process')
var log = require('fancy-log')
var del = require('del')
var ghPages = require('gh-pages')
var gulp = require('gulp')
var path = require('path')
var fs = require('fs')
var sequence = require('run-sequence')
var dev = false
// load gulp plugins
var $ = require('gulp-load-plugins')()
var paths = {
assets: './app/_assets/',
modules: './node_modules/',
dist: './dist/'
}
// Sources
var sources = {
content: 'app/**/*.{markdown,md,html,txt,yml,yaml}',
styles: paths.assets + 'stylesheets/**/*',
js: [
paths.assets + 'javascripts/jquery.2.1.3.min.js',
paths.assets + 'javascripts/app.js',
paths.modules + 'bootstrap/js/dropdown.js',
paths.modules + 'bootstrap/js/affix.js'
],
images: paths.assets + 'images/**/*',
fonts: [
paths.modules + 'font-awesome/fonts/**/*.*',
paths.assets + 'fonts/*.*'
]
}
// Destinations
var dest = {
html: paths.dist + '**/*.html',
js: paths.dist + 'assets/app.js'
}
var reload = done => {
browserSync.reload()
done()
}
/* Functions
--------------------------------------- */
// Basic Tasks
function css() {
return gulp.src(paths.assets + 'css/*.css')
.pipe(gulp.dest(paths.dist + 'assets'))
}
function styles() {
return gulp.src(paths.assets + 'stylesheets/index.less')
.pipe($.plumber())
.pipe($.if(dev, $.sourcemaps.init()))
.pipe($.less())
.pipe($.autoprefixer())
// .pipe($.purifycss([dest.html], {
// whitelist: [
// '.affix',
// '.alert',
// '.close',
// '.collaps',
// '.fade',
// '.has',
// '.help',
// '.in',
// '.modal',
// '.open',
// '.popover',
// '.tooltip'
// ]
// }))
.pipe($.cleanCss({ compatibility: 'ie8' }))
.pipe($.rename('styles.css'))
.pipe($.if(dev, $.sourcemaps.write()))
.pipe(gulp.dest(paths.dist + 'assets'))
.pipe($.size())
.pipe(browserSync.stream())
}
function js() {
return gulp.src(sources.js)
.pipe($.plumber())
.pipe($.if(dev, $.sourcemaps.init()))
.pipe($.concat('app.js'))
.pipe($.if(dev, $.sourcemaps.write()))
.pipe(gulp.dest('dist/assets'))
.pipe(browserSync.stream())
}
function js_min() {
return gulp.src(sources.js)
.pipe($.plumber())
.pipe($.if(dev, $.sourcemaps.init()))
.pipe($.minify({
noSource: true,
ext: {
min: '.js'
}
}))
.pipe($.concat('app.js'))
.pipe($.if(dev, $.sourcemaps.write()))
.pipe(gulp.dest('dist/assets'))
.pipe($.size())
.pipe(browserSync.stream())
}
function images() {
return gulp.src(sources.images)
.pipe($.plumber())
.pipe(gulp.dest(paths.dist + 'assets/images'))
}
function images_min() {
return gulp.src(sources.images)
.pipe($.plumber())
.pipe($.imagemin())
.pipe(gulp.dest(paths.dist + 'assets/images'))
.pipe($.size())
}
function fonts() {
return gulp.src(sources.fonts)
.pipe($.plumber())
.pipe(gulp.dest(paths.dist + 'assets/fonts'))
.pipe($.size())
.pipe(browserSync.stream())
}
function jekyll(cb) {
var command = 'bundle exec jekyll build --config jekyll.yml --profile --destination ' + paths.dist
childProcess.exec(command, function (err, stdout, stderr) {
log(stdout)
log(stderr)
cb(err)
})
}
function jekyll_dev(cb) {
var command = 'bundle exec jekyll build --config jekyll-dev.yml --profile --destination ' + paths.dist
childProcess.exec(command, function (err, stdout, stderr) {
log(stdout)
log(stderr)
cb(err)
})
}
function html() {
return gulp.src(paths.dist + '/**/*.html')
.pipe($.plumber())
.pipe(gulp.dest(paths.dist))
.pipe($.size())
}
// Lua Tasks
function pdk_docs(cb) {
var KONG_PATH, KONG_VERSION,
navFilepath, doc, pdkRegex, newNav, newDoc,
cmd, obj, errLog,
refDir, modules,
gitSha1, confFilepath
// 0 Obtain "env-var params"
KONG_PATH = process.env.KONG_PATH
if (KONG_PATH === undefined) {
return cb('No KONG_PATH environment variable set')
}
KONG_VERSION = process.env.KONG_VERSION
if (KONG_VERSION === undefined) {
return cb('No KONG_VERSION environment variable set. Example: 0.14.x')
}
// 1. Update nav file
// 1.1 Check that nav file exists
navFilepath = './app/_data/docs_nav_' + KONG_VERSION + '.yml'
try {
doc = fs.readFileSync(navFilepath, 'utf8')
} catch (err) {
return cb('Could not find the file ' + navFilepath + '. Err: ' + err)
}
// 1.2 Check that nav file has the correct yaml entry
pdkRegex = /[ ]+- text: Plugin Development Kit[\s\S]+\n-/gm
if (!doc.match(pdkRegex)) {
return cb('Could not find the appropriate section in ' + navFilepath)
}
// 1.3 Generate new yaml using ldoc
cmd = 'LUA_PATH="$LUA_PATH;./?.lua" ' +
'ldoc -q -i --filter ldoc/filters.nav ' +
KONG_PATH + '/kong/pdk'
obj = childProcess.spawnSync(cmd, { shell: true })
// ignore "unkwnown tag" errors
errLog = obj.stderr.toString().replace(/.*unknown tag.*\n/g, '')
if (errLog.length > 0) {
return cb(errLog)
}
// 1.4 Replace existing yaml with generated one
newNav = obj.stdout.toString()
newDoc = doc.replace(pdkRegex, newNav + '\n-')
fs.writeFileSync(navFilepath, newDoc)
log('Updated contents of ' + navFilepath + ' with new navigation items')
// 2. Generate markdown docs using custom ldoc templates
// 2.1 Prepare ref folder
refDir = 'app/' + KONG_VERSION + '/pdk'
cmd = 'rm -rf ' + refDir + ' && mkdir ' + refDir
obj = childProcess.spawnSync(cmd, { shell: true })
errLog = obj.stderr.toString()
if (errLog.length > 0) {
return cb(errLog)
}
// 2.2 Obtain the list of modules in json form & parse it
cmd = 'LUA_PATH="$LUA_PATH;./?.lua" ' +
'ldoc -q -i --filter ldoc/filters.json ' +
KONG_PATH + '/kong/pdk'
obj = childProcess.spawnSync(cmd, { shell: true })
// ignore "unkwnown tag" errors
errLog = obj.stderr.toString().replace(/.*unknown tag.*\n/g, '')
if (errLog.length > 0) {
return cb(errLog)
}
modules = JSON.parse(obj.stdout.toString())
// 2.3 For each module, generate its docs in markdown
for (let module of modules) {
cmd = 'LUA_PATH="$LUA_PATH;./?.lua" ' +
'ldoc -q -c ldoc/config.ld ' +
module.file + ' && ' +
'mv ./' + module.generated_name + '.md ' + refDir + '/' +
module.name + '.md'
obj = childProcess.spawnSync(cmd, { shell: true })
errLog = obj.stderr.toString()
if (errLog.length > 0) {
return cb(errLog)
}
}
log('Re-generated PDK docs in ' + refDir)
// 3 Write pdk_info yaml file
// 3.1 Obtain git sha-1 hash of the current git log
cmd = 'pushd ' + KONG_PATH + ' > /dev/null; git rev-parse HEAD; popd > /dev/null'
obj = childProcess.spawnSync(cmd, { shell: true })
errLog = obj.stderr.toString()
if (errLog.length > 0) {
return cb(errLog)
}
gitSha1 = obj.stdout.toString().trim()
// 3.2 Write it into file
confFilepath = 'app/_data/pdk_info.yml'
fs.writeFileSync(confFilepath, 'sha1: ' + gitSha1 + '\n')
log('git SHA-1 (' + gitSha1 + ') written to ' + confFilepath)
}
function admin_api_docs(cb) {
var KONG_PATH, KONG_VERSION, cmd, obj, errLog
// 0 Obtain "env-var params"
KONG_PATH = process.env.KONG_PATH
if (KONG_PATH === undefined) {
return cb('No KONG_PATH environment variable set')
}
KONG_VERSION = process.env.KONG_VERSION
if (KONG_VERSION === undefined) {
return cb('No KONG_VERSION environment variable set. Example: 0.14.x')
}
// 1 Generate admin-api.md
cmd = 'resty autodoc-admin-api/run.lua'
obj = childProcess.spawnSync(cmd, { shell: true })
errLog = obj.stderr.toString()
if (errLog.length > 0) {
return cb(errLog)
}
log('Re-generated Admin API docs for ' + KONG_VERSION)
}
function cli_docs(cb) {
var KONG_PATH, KONG_VERSION, cmd, obj, errLog
// 0 Obtain "env-var params"
KONG_PATH = process.env.KONG_PATH
if (KONG_PATH === undefined) {
return cb('No KONG_PATH environment variable set')
}
KONG_VERSION = process.env.KONG_VERSION
if (KONG_VERSION === undefined) {
return cb('No KONG_VERSION environment variable set. Example: 1.0.x')
}
// 1 Generate cli.md
cmd = 'luajit autodoc-cli/run.lua'
obj = childProcess.spawnSync(cmd, { shell: true })
errLog = obj.stderr.toString()
if (errLog.length > 0) {
return cb(errLog)
}
log('Re-generated CLI docs for ' + KONG_VERSION)
}
function conf_docs(cb) {
var KONG_PATH, KONG_VERSION, cmd, obj, errLog
// 0 Obtain "env-var params"
KONG_PATH = process.env.KONG_PATH
if (KONG_PATH === undefined) {
return cb('No KONG_PATH environment variable set')
}
KONG_VERSION = process.env.KONG_VERSION
if (KONG_VERSION === undefined) {
return cb('No KONG_VERSION environment variable set. Example: 1.0.x')
}
// Generate configuration.md
cmd = 'luajit autodoc-conf/run.lua'
obj = childProcess.spawnSync(cmd, { shell: true })
errLog = obj.stderr.toString()
if (errLog.length > 0) {
return cb(errLog)
}
log('Re-generated Conf docs for ' + KONG_VERSION)
}
function nav_docs(cb) {
var KONG_VERSION, cmd, obj, errLog
// 0 Obtain "env-var params"
KONG_VERSION = process.env.KONG_VERSION
if (KONG_VERSION === undefined) {
return cb('No KONG_VERSION environment variable set. Example: 1.0.x')
}
// 1 Generate docs_nav_X.Y.x.yml
cmd = 'luajit autodoc-nav/run.lua'
obj = childProcess.spawnSync(cmd, { shell: true })
errLog = obj.stderr.toString()
if (errLog.length > 0) {
return cb(errLog)
}
log('Re-generated navigation file for ' + KONG_VERSION)
}
// Custom Tasks
function browser_sync(done) {
browserSync.init({
logPrefix: ' ▶ ',
minify: false,
notify: false,
server: 'dist',
open: false
})
done()
}
function gh_pages(cb) {
var cmd = 'git rev-parse --short HEAD'
childProcess.exec(cmd, function (err, stdout, stderr) {
if (err) {
cb(err)
}
ghPages.publish(path.join(__dirname, paths.dist), {
message: 'Deploying ' + stdout + '(' + new Date().toISOString() + ')'
}, cb)
})
}
function cloudflare(cb) {
// configure cloudflare
var cloudflare = require('cloudflare').createClient({
email: process.env.MASHAPE_CLOUDFLARE_EMAIL,
token: process.env.MASHAPE_CLOUDFLARE_TOKEN
})
cloudflare.clearCache('docs.getkong.com', function (err) {
if (err) {
log(err.message)
}
cb()
})
}
function clean() {
ghPages.clean()
return del(['dist', '.gh-pages'])
}
function watch_files() {
gulp.watch(sources.content, gulp.series(jekyll, html, reload))
gulp.watch(sources.styles, styles)
gulp.watch(sources.images, gulp.series(images, reload))
gulp.watch(sources.js, gulp.series(js, reload))
gulp.watch(paths.assets + 'css/hub.css', css)
}
function set_dev(cb) {
dev = true
cb()
}
/*----------------------------------------*/
// Basic Tasks
gulp.task("js", js)
gulp.task("js_min", js_min)
gulp.task("css", css)
gulp.task("styles", styles)
gulp.task("images", images)
gulp.task("images_min", images_min)
gulp.task("fonts", fonts)
gulp.task("jekyll", jekyll)
gulp.task("jekyll_dev", jekyll_dev)
gulp.task("html", html)
// Lua Tasks
gulp.task("pdk_docs", pdk_docs)
gulp.task("admin_api_docs", admin_api_docs)
gulp.task("cli_docs", cli_docs)
gulp.task("conf_docs", conf_docs)
gulp.task("nav_docs", nav_docs)
// Custom Tasks
gulp.task("browser_sync", browser_sync)
gulp.task("gh_pages", gh_pages)
gulp.task("cloudflare", cloudflare)
gulp.task("set_dev", set_dev)
// Gulp Commands
gulp.task("build", gulp.series(gulp.parallel(js, images, fonts, css), jekyll, html, styles))
gulp.task("watch", gulp.series(browser_sync, watch_files))
gulp.task("dev", gulp.series(set_dev, clean, gulp.parallel(js, images, fonts, css), jekyll, html, styles, browser_sync, watch_files))
gulp.task('default', gulp.series(clean, gulp.parallel(js, images, fonts, css), jekyll_dev, html, styles, browser_sync, watch_files))
gulp.task("deploy", gulp.series(gulp.parallel(js_min, images_min, fonts, css), jekyll, html, styles, gh_pages))