forked from Kong/docs.konghq.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
468 lines (405 loc) · 12 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
464
465
466
467
468
"use strict";
var browserSync = require("browser-sync").create();
var childProcess = require("child_process");
var log = require("fancy-log");
var del = require("del");
var gulp = require("gulp");
var path = require("path");
var fs = require("fs");
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-3.6.0.min.js",
"node_modules/@segment/analytics-next/dist/umd/standalone.js",
"node_modules/js-cookie/dist/js.cookie.js",
paths.assets + "javascripts/app.js",
paths.assets + "javascripts/compat-dropdown.js",
paths.assets + "javascripts/plugin-hub.js",
paths.assets + "javascripts/subscribe.js",
paths.assets + "javascripts/editable-code-snippet.js",
paths.assets + "javascripts/navbar.js",
paths.assets + "javascripts/feedback-widget.js",
// uncomment the path to promo-banner.js when adding a new promo banner
// also uncomment the promo banner sections in app/_assets/stylesheets/header.less and /app/_includes/nav-v2.html -->
// paths.assets + "javascripts/promo-banner.js",
paths.assets + "javascripts/copy-code-snippet-support.js",
],
images: paths.assets + "images/**/*",
manifests: paths.assets + "manifests/**/*",
fonts: [
paths.modules + "font-awesome/fonts/**/*.*",
paths.assets + "fonts/*.*",
],
keys: paths.assets + "keys/**/*",
};
// Destinations
var dest = {
html: paths.dist + "**/*.html",
js: paths.dist + "assets/app.js",
};
/* 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($.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)
// Add Segment write key
.pipe($.replace(/__WRITE_KEY__/, "X7EZTdbdUKQ8M6x42SHHPWiEhjsfs1EQ"))
.pipe($.plumber())
.pipe($.if(dev, $.sourcemaps.init()))
.pipe(
$.if(
!dev,
$.minify({
noSource: true,
ext: {
min: ".js",
},
})
)
)
.pipe($.concat("app.js"))
.pipe($.if(dev, $.sourcemaps.write()))
.pipe(gulp.dest("dist/assets"))
.pipe($.if(!dev, $.size()))
.pipe(browserSync.stream())
);
}
function images() {
return gulp
.src(sources.images)
.pipe($.plumber())
.pipe(gulp.dest(paths.dist + "assets/images"))
.pipe($.if(!dev, $.size()));
}
function manifests() {
return gulp
.src(sources.manifests)
.pipe($.plumber())
.pipe(gulp.dest(paths.dist + "assets"))
.pipe($.if(!dev, $.size()));
}
function keys() {
return gulp
.src(sources.keys)
.pipe($.plumber())
.pipe(gulp.dest(paths.dist + "assets/keys"))
.pipe($.if(!dev, $.size()));
}
function fonts() {
return gulp
.src(sources.fonts)
.pipe($.plumber())
.pipe(gulp.dest(paths.dist + "assets/fonts"))
.pipe($.if(!dev, $.size()))
.pipe(browserSync.stream());
}
function jekyll(cb) {
var profile = "";
if (dev) {
profile = "-dev";
}
var command = `bundle exec jekyll build --config jekyll${profile}.yml --profile`;
childProcess.exec(command, function (err, stdout, stderr) {
log(stdout);
log(stderr);
cb(err);
});
}
// 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();
}
var reload_browser = (done) => {
browserSync.reload();
done();
};
function clean() {
return del(["dist"]);
}
function watch_files() {
gulp.watch(sources.content, gulp.series(jekyll, reload_browser));
gulp.watch(sources.styles, styles);
gulp.watch(sources.images, gulp.series(images, reload_browser));
gulp.watch(sources.js, gulp.series(js, reload_browser));
gulp.watch(paths.assets, css);
}
function set_dev(cb) {
dev = true;
cb();
}
/*----------------------------------------*/
// Basic Tasks
gulp.task("js", gulp.series(set_dev, js));
gulp.task("js_min", js);
gulp.task("css", css);
gulp.task("styles", styles);
gulp.task("images", gulp.series(set_dev, images));
gulp.task("manifests", gulp.series(set_dev, manifests));
gulp.task("keys", keys);
gulp.task("fonts", fonts);
gulp.task("jekyll", jekyll);
// 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("set_dev", set_dev);
// Call the tasks in the correct order
function build_site(steps, append) {
steps = steps || [];
append = append || [];
if (process.env.JEKYLL_ENV == "preview") {
var configFilePath = "./jekyll.yml";
var doc = fs.readFileSync(configFilePath, "utf8");
var newDoc = doc.replace(
/ web: https:\/\/docs.konghq.com/,
` web: ${process.env.DEPLOY_PRIME_URL}`
);
fs.writeFileSync(configFilePath, newDoc);
}
// These are the steps that always run for every build
// If set_dev is called, some of these methods behave differently
steps = steps.concat([
gulp.parallel(js, images, manifests, keys, fonts, css),
jekyll,
styles,
]);
if (append.length) {
steps = steps.concat(append);
}
return gulp.series.apply(null, steps);
}
gulp.task("default", build_site([set_dev], [browser_sync, watch_files]));
gulp.task("dev", build_site([set_dev], [browser_sync, watch_files]));
gulp.task("prod", build_site(null, [browser_sync, watch_files]));
gulp.task("build", build_site());