forked from apache/cordova-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
350 lines (298 loc) · 10 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
"use strict";
// dependencies
var path = require("path");
var fse = require("fs-extra");
var child_process = require("child_process");
var gulp = require("gulp");
var gutil = require("gulp-util");
var less = require("gulp-less");
var sass = require("gulp-sass");
var replace = require("gulp-replace");
var header = require("gulp-header");
var footer = require("gulp-footer");
var rename = require("gulp-rename");
var browsersync = require("browser-sync");
var vstream = require("vinyl-source-stream");
var buffer = require("vinyl-buffer");
var browserify = require("browserify");
var reactify = require("reactify");
var uglify = require("gulp-uglify");
var envify = require("envify");
var htmllint = require("gulp-htmllint");
var crawler = require("simplecrawler");
// constants
var ROOT_DIR = ".";
var SOURCE_DIR = path.join(ROOT_DIR, "www");
var DEV_DIR = path.join(ROOT_DIR, "build-dev");
var PROD_DIR = path.join(ROOT_DIR, "build-prod");
var DATA_DIR = path.join(SOURCE_DIR, "_data");
var TOC_DIR = path.join(DATA_DIR, "toc");
var DOCS_DIR = path.join(SOURCE_DIR, "docs");
var CSS_SRC_DIR = path.join(SOURCE_DIR, "static", "css-src");
var CSS_OUT_DIR = path.join(SOURCE_DIR, "static", "css");
var PLUGINS_SRC_DIR = path.join(SOURCE_DIR, "static", "plugins");
var JS_DIR = path.join(SOURCE_DIR, "static", "js");
var BIN_DIR = path.join(ROOT_DIR, "tools", "bin");
var CONFIG_FILE = "_config.yml";
var DEFAULTS_CONFIG_FILE = "_defaults.yml";
var VERSION_CONFIG_FILE = "_version.yml";
var PROD_CONFIG_FILE = "_prod.yml";
var DEV_CONFIG_FILE = "_dev.yml";
var NODOCS_CONFIG_FILE = "_nodocs.yml";
var VERSION_FILE = "VERSION";
var LANGUAGES_FILE = path.join(DATA_DIR, "languages.yml");
var PLUGINS_FILE_NAME = "plugins.js";
var PLUGINS_FILE = path.join(JS_DIR, PLUGINS_FILE_NAME);
var PLUGINS_SRC_FILE = path.join(PLUGINS_SRC_DIR, "app.js");
var DEFAULT_CONFIGS = [CONFIG_FILE, DEFAULTS_CONFIG_FILE, VERSION_CONFIG_FILE];
var DEV_CONFIGS = [DEV_CONFIG_FILE];
var PROD_CONFIGS = [PROD_CONFIG_FILE];
var DEV_FLAGS = ["--trace"];
var PROD_FLAGS = [];
var BASE_URL = "";
var YAML_FRONT_MATTER = "---\n---\n";
var WATCH_INTERVAL = 1000; // in milliseconds
var VERSION_VAR_NAME = "latest_docs_version";
var PROD_BY_DEFAULT = false;
// compute/get/set/adjust passed options
gutil.env.prod = gutil.env.prod || PROD_BY_DEFAULT;
gutil.env.dev = !gutil.env.prod;
gutil.env.outDir = gutil.env.prod ? PROD_DIR : DEV_DIR;
// helpers
function execPiped(command, args, fileName) {
console.log(command + " " + args.join(" "));
var task = child_process.spawn(command, args);
return task.stdout.pipe(vstream(fileName)).pipe(buffer());
}
function exec(command, args, cb) {
console.log(command + " " + args.join(" "));
var task = child_process.spawn(command, args, {stdio: "inherit"});
task.on("exit", cb);
}
function bin(name) {
return path.join(BIN_DIR, name);
}
function remove(path) {
console.log("removing " + path);
fse.removeSync(path);
}
function getJekyllExecutable() {
if (process.platform === "win32") {
return "jekyll.bat";
} else {
return "jekyll";
}
}
function getJekyllConfigs() {
var configs = DEFAULT_CONFIGS;
// add build-specific config files
if (gutil.env.prod) {
configs = configs.concat(PROD_CONFIGS);
} else {
configs = configs.concat(DEV_CONFIGS);
}
// add a special exclude file if "nodocs" was specified
if (gutil.env.nodocs) {
configs = configs.concat(NODOCS_CONFIG_FILE);
}
return configs;
}
function jekyllBuild(done) {
var jekyll = getJekyllExecutable();
var configs = getJekyllConfigs();
var flags = gutil.env.prod ? PROD_FLAGS : DEV_FLAGS;
flags = flags.concat(["--config", configs.join(",")]);
exec(jekyll, ["build"].concat(flags), done);
}
// tasks
gulp.task("watch", ["serve"], function () {
gulp.watch(
[
path.join(CSS_SRC_DIR, "**", "*"),
],
{interval: WATCH_INTERVAL},
["styles"]
);
gulp.watch(
[
path.join(DOCS_DIR, "**", "*.md"),
path.join(DOCS_DIR, "**", "*.html"),
],
{interval: WATCH_INTERVAL},
["configs"]
);
gulp.watch(
[
path.join(PLUGINS_SRC_DIR, "**", "*.js"),
path.join(PLUGINS_SRC_DIR, "**", "*.jsx"),
path.join(PLUGINS_SRC_DIR, "**", "*.json"),
],
{interval: WATCH_INTERVAL},
["plugins"]
);
gulp.watch(
[
path.join(ROOT_DIR, "**", "*.yml"),
path.join(SOURCE_DIR, "**", "*.html"),
path.join(SOURCE_DIR, "**", "*.md"),
path.join(JS_DIR, "**", "*.js"),
path.join(CSS_OUT_DIR, "**", "*.css"),
],
{interval: WATCH_INTERVAL},
["regen"]
);
});
gulp.task("serve", ["build"], function () {
var route = {};
if(gutil.env.prod) {
route[BASE_URL] = gutil.env.outDir;
}
browsersync({
notify: true,
server: {
baseDir: gutil.env.outDir,
routes: route
}
});
});
gulp.task("gen-full", ["configs", "styles", "plugins"], function (done) {
jekyllBuild(done);
});
gulp.task("gen-only", function (done) {
jekyllBuild(done);
});
gulp.task("regen", ["gen-only"], function () {
browsersync.reload();
});
gulp.task("reload", function () {
browsersync.reload();
});
gulp.task("languages", function () {
return execPiped("node", [bin("gen_languages.js"), DOCS_DIR], LANGUAGES_FILE)
.pipe(gulp.dest(ROOT_DIR));
});
gulp.task("version", function () {
// this code is stupid; it's basically the line:
// cat VERSION | sed -e 's/^/VERSION_VAR_NAME: /' > _version.yml
// however we're in Gulp, and we need to support Windows...
// so we contort it into a monster
return gulp
.src(VERSION_FILE)
.pipe(header(VERSION_VAR_NAME + ": "))
.pipe(footer("\n"))
.pipe(rename(VERSION_CONFIG_FILE))
.pipe(gulp.dest("."));
});
gulp.task("defaults", function () {
return execPiped("node", [bin("gen_defaults.js"), DOCS_DIR], DEFAULTS_CONFIG_FILE)
.pipe(gulp.dest(ROOT_DIR));
});
gulp.task("toc", function (done) {
exec("node", [bin("toc.js"), DOCS_DIR, DATA_DIR], done);
});
gulp.task("less", function () {
return gulp
.src(path.join(CSS_SRC_DIR, "**", "*.less"))
.pipe(less())
.pipe(header(YAML_FRONT_MATTER))
.pipe(gulp.dest(CSS_OUT_DIR))
.pipe(gulp.dest(CSS_OUT_DIR.replace(SOURCE_DIR, gutil.env.outDir)))
.pipe(browsersync.reload({stream: true}));
})
gulp.task("css", function () {
return gulp
.src(path.join(CSS_SRC_DIR, "**", "*.css"))
.pipe(header(YAML_FRONT_MATTER))
.pipe(gulp.dest(CSS_OUT_DIR))
.pipe(gulp.dest(CSS_OUT_DIR.replace(SOURCE_DIR, gutil.env.outDir)))
.pipe(browsersync.reload({stream: true}));
})
gulp.task("sass", function() {
return gulp
.src(path.join(CSS_SRC_DIR, "**", "*.scss"))
.pipe(sass().on("error", sass.logError))
.pipe(header(YAML_FRONT_MATTER))
.pipe(gulp.dest(CSS_OUT_DIR))
.pipe(gulp.dest(CSS_OUT_DIR.replace(SOURCE_DIR, gutil.env.outDir)))
.pipe(browsersync.reload({stream: true}));
});
gulp.task("plugins", function() {
if (gutil.env.prod) {
process.env.NODE_ENV = "production";
}
var stream = browserify(PLUGINS_SRC_FILE, {debug: !gutil.env.prod})
.transform(reactify)
.transform(envify)
.bundle()
.on("error", gutil.log)
.pipe(vstream(PLUGINS_FILE_NAME))
.pipe(buffer());
if (gutil.env.prod) {
stream = stream
.pipe(uglify())
.on("error", gutil.log);
}
return stream
.pipe(gulp.dest(JS_DIR.replace(SOURCE_DIR, gutil.env.outDir)))
.pipe(browsersync.reload({stream: true}))
// NOTE:
// adding YAML front matter at the end
// so that uglify doesn't screw it up
.pipe(header(YAML_FRONT_MATTER))
// WORKAOUND:
// minified JS has some things that look like
// Liquid tags, so we replace them manually
.pipe(replace("){{", "){ {"))
.pipe(gulp.dest(JS_DIR));
});
// compound tasks
gulp.task("configs", ["toc", "languages", "defaults", "version"]);
gulp.task("styles", ["less", "css", "sass"]);
gulp.task("build", ["gen-full"]);
gulp.task("default", ["serve"]);
// convenience tasks
gulp.task("link-bugs", function (done) {
exec(bin("linkify-bugs.sh"), [path.join(SOURCE_DIR, "_posts")], done);
});
gulp.task("lint", function() {
return gulp.src(path.join("./", "**", "*.html"))
.pipe(htmllint());
});
gulp.task("newversion", function(done) {
if (!gutil.env.version) {
gutil.log(gutil.colors.red("No version given."));
gutil.log("Specify new version with '--version X.X.X'.");
gutil.log("(Optionally) Specify language with '--language YY'.");
done();
return;
}
var args = [bin("incrementversion.js"), DOCS_DIR, TOC_DIR, gutil.env.version];
if (gutil.env.language) {
args.push(gutil.env.language);
}
exec("node", args, done);
});
gulp.task("checklinks", function(done) {
crawler
.crawl("http://localhost:3000/")
.on("fetch404", function(queueItem, response) {
gutil.log(
"Resource not found linked from " +
queueItem.referrer + " to", queueItem.url
);
gutil.log("Status code: " + response.statusCode);
})
.on("complete", function(queueItem) {
done();
});
});
gulp.task("clean", function () {
remove(DEV_DIR);
remove(PROD_DIR);
remove(path.join(DATA_DIR, "toc", "*-generated.yml"));
remove(CSS_OUT_DIR);
remove(PLUGINS_FILE);
remove(LANGUAGES_FILE);
remove(DEFAULTS_CONFIG_FILE);
remove(VERSION_CONFIG_FILE);
});