-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
464 lines (372 loc) · 12.2 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
// Node modules
const browserify = require("browserify");
const source = require("vinyl-source-stream");
const browserSync = require("browser-sync");
const reload = browserSync.reload;
const nib = require("nib");
const del = require("del");
const glob = require('glob')
// Gulp-related
const gulp = require("gulp");
const pug = require("gulp-pug");
const preprocess = require("gulp-preprocess");
const shell = require("gulp-shell");
const stylus = require("gulp-stylus");
const autoprefixer = require('autoprefixer');
const postcss = require('gulp-postcss');
const replace = require('gulp-replace');
const gulpif = require('gulp-if');
const touch = require('gulp-touch-cmd');
// Non-gulp NPM modules
const fs = require("fs");
const request = require('request');
// Local modules
const args = require("./gulp/cli").parse();
const config = require("./gulp/config");
const utils = require("./gulp/utils");
// Configuration
const thingConfig = require("./thing-config.js")
const gdoc_id = thingConfig.gdoc_id;
const gdoc_host = "127.0.0.1:6006";
const gdoc_url = "http://"+gdoc_host+"/"+ gdoc_id;
var content = {};
var data_from_file = {};
const qzthingspath = process.env.QZTHINGS_PATH || "~/qz-things";
const thingName = thingConfig.thing_name;
const thingPath = qzthingspath + thingConfig.thing_path;
const datafiles = thingConfig.datafiles.map(d => {
return {
...d,
'taskName':` get-data ${d.fn}`,
"cleanFN": d.fn.split("/").pop().split(".")[0].replace(/-/g, "_")
}
})
const AWS_URL = "https://things-assets.qz.com";
const AWS_DATA_URL = AWS_URL + thingConfig.thing_path + "/data/";
const AWS_ASSETS_URL = AWS_URL + thingConfig.thing_path + "/assets/";
const isProd = args.build ? true : false;
const preprocessOpts = {
context: {
ENV: isProd ? "prod" : "dev"
}
};
// there are three groups of tasks that get fill with taskts to run in parallel.
// Tasks that load content files and check files for policy compliance
var contentTasks = []
// Tasks that compile or transpile source files into their final form
var compilationTasks = []
// tasks that move files from the source folder to the build folder
var copyTasks = []
var sizeLimitedFolders = ["assets", "data"]
var sizeCheckTasks = sizeLimitedFolders.map(d => d + "-check")
// set up the tasks that just copy files from folder to folder
var allFoldersToCopy = []
var allFoldersToAlwaysCopy = [
{id:"libs", src: config.paths.src.js + "/libs/*", dst: config.paths.build.js + "/libs"},
{id:"assets", src: config.paths.src.assets + "/**", dst: config.paths.build.assets},
{id:"data", src: config.paths.src.data + "/**", dst: config.paths.build.data},
]
var allFoldersToCopyIfLocal = [
{id:"fonts", src: config.paths.src.fonts + "/**", dst: config.paths.build.fonts},
{id:"favicon", src: "./src/favicon.ico", dst: "./build"},
]
var allFoldersToCopyIfProd = [
]
allFoldersToCopy.push(...allFoldersToAlwaysCopy)
if(isProd) {
allFoldersToCopy.push(...allFoldersToCopyIfProd)
}
else {
allFoldersToCopy.push(...allFoldersToCopyIfLocal)
}
// get all of the pages that are in the pages folder
var allPages = fs.readdirSync("./src/pug/pages/")
.filter(d => d.includes(".pug"))
.map(d => d.replace(".pug", ""))
// get all scripts that are in the scripts folder
var allScripts = fs.readdirSync("./src/js/scripts/")
.filter(d => d.includes(".js"))
.map(d => d.replace(".js", ""))
// get all the stylesheets that are in the stylesheet folder
var allStylesheets = fs.readdirSync("./src/styl/stylesheets")
.filter(d => d.includes(".styl"))
.map(d => d.replace(".styl", ""))
var pugTasks = [];
var browserifyTasks = []
var stylusTasks = []
var shellCmd = utils.generateShellCmd(args.build, thingPath, thingName, thingConfig.thing_path);
sizeLimitedFolders.forEach(folderName => {
function checkFolderTask(doneCallback) {
var numFileLimit = 13;
var totalSizeLimit = 5; //in MB
var singleSizeLimit = 1.25; //in MB
glob("./src/"+ folderName +"/**/*", (err, files) => {
// limit the number of files
if (files.length > numFileLimit) {
doneCallback(new Error("You have "+files.length+" files in your "+ folderName +" folder. We only allow "+(numFileLimit - 1)+". Move them to the aws-"+ folderName +" and adjust your code accordingly."))
return
}
var fileSizes = files.map(d => fs.statSync(d).size / 1000000);
// limit the total file size
var totalFileSize = Math.round(fileSizes.reduce((acc, cur) => acc + cur, 0)*10)/10
if(totalFileSize > totalSizeLimit) {
doneCallback(new Error("Your files in the "+ folderName +" folder are "+totalFileSize+"MB. That's over the "+totalSizeLimit+"MB limit for the "+ folderName +" folder. Move them to the aws-"+ folderName +" and adjust your code accordingly."))
return
}
// limit the size of individual files
var filesLargerThanLimit = fileSizes.filter(d => d > singleSizeLimit).length;
if(filesLargerThanLimit) {
doneCallback(new Error("You have "+filesLargerThanLimit+" files larger than "+singleSizeLimit+"MB in the "+ folderName +" folder. Move them to the aws-"+ folderName +" and adjust your code accordingly."))
return
}
doneCallback();
})
}
let taskName = ` ${folderName}-check`
gulp.task(taskName, checkFolderTask)
contentTasks.push(taskName)
})
/**
* Fetch ArchieML data from Google Docs.
*/
function getContentTask(doneCallback) {
if(gdoc_id !== "") {
request.get({
"url": gdoc_url
},
function(error, resp, body) {
if(!error && resp.statusCode < 400) {
content = JSON.parse(body);
fs.writeFileSync("content.json", body, "utf-8");
doneCallback();
}
else {
// if the server isn't up load from file
if(resp && resp.statusCode >= 400) {
console.log(body);
}
console.log("Cannot load content from server, loading from file");
readContentFromFile(doneCallback);
}
});
}
else {
console.log("No google doc specified, loading from file");
readContentFromFile(doneCallback);
}
}
gulp.task(" get-content", getContentTask);
contentTasks.push(" get-content")
// loop through each data file and create a task to load and parse it
datafiles.forEach(o => {
function readDataFromFile(doneCallback) {
request.get(o.fn, function(err, data) {
if (!err) {
data_from_file[o.cleanFN] = o.parser ? o.parser(data.body.toString("utf-8")) : JSON.parse(data.body);
doneCallback();
}
else {
console.log(`Cannot load data from ${o.fn}`);
doneCallback(err);
}
})
// fs.readFile(o.fn, function(err, data){
// });
}
gulp.task(o.taskName, readDataFromFile)
contentTasks.push(o.taskName)
})
/**
* Compile Pug HTML templates. (Also triggers a refetch of the ArchieML doc.)
*/
allPages.forEach(function(pageid){
var pugTaskName = " pug: " + pageid
pugTasks.push(pugTaskName);
compilationTasks.push(pugTaskName)
function compilePugTask() {
var context = {
"content": content, // this is the AML document
...data_from_file, // these are any of the files listed to load
"build_info": {
"timestamp": (new Date()).getTime().toString(),
"thing_name": thingConfig.thing_name,
"thing_description": thingConfig.thing_description,
"thing_path": thingConfig.thing_path,
"gdoc_id": thingConfig.gdoc_id
}
}
return gulp.src(config.paths.src.pug + "/pages/"+ pageid +".pug")
.pipe(pug({ pretty: true, locals: context }))
.pipe(gulpif(isProd, replace("aws-data/", AWS_DATA_URL)))
.pipe(gulpif(isProd, replace("aws-assets/", AWS_ASSETS_URL)))
.pipe(gulp.dest(config.dirs.build))
.pipe(touch())
.pipe(reload({ stream: true }));
}
gulp.task(pugTaskName, compilePugTask);
})
/**
* Writes environment configuration variables to config.js and puts it in
* the build directory.
*/
function preprocessTask() {
return gulp.src([config.paths.src.js + "/config.js"])
.pipe(preprocess(preprocessOpts))
.pipe(gulp.dest(config.paths.build.js))
.pipe(touch());
}
gulp.task("preprocess", preprocessTask);
/**
* Bundle Javascript with browserify.
*/
allScripts.forEach(function(scriptid){
var browserifyTaskName = " browserify: " + scriptid;
browserifyTasks.push(browserifyTaskName);
compilationTasks.push(browserifyTaskName);
function compileJavascriptTask(doneCallback) {
var bundler = browserify({
entries: [config.paths.src.js + "/scripts/"+ scriptid +".js"],
debug: !isProd
})
// target the javascript capabilities of the browsers
// specified under "browserslist" in package.json
bundler.transform("babelify", {
presets: [[
"@babel/preset-env",
{
"useBuiltIns": "entry",
"corejs": "3"
}
]]
})
if (isProd && !args["dont-minify"]) {
bundler.transform({ global: true }, "uglifyify");
}
return bundler
.bundle()
.on('error', function(err) {
console.error('ERROR IN ' + scriptid + ".js");
console.error(err.message);
doneCallback();
})
.pipe(source(scriptid + ".js"))
.pipe(gulpif(isProd, replace("aws-data/", AWS_DATA_URL)))
.pipe(gulpif(isProd, replace("aws-assets/", AWS_ASSETS_URL)))
.pipe(gulp.dest(config.paths.build.js))
.pipe(touch())
.pipe(reload({ stream: true }));
}
gulp.task(browserifyTaskName, compileJavascriptTask);
})
/**
* Compile Stylus CSS meta-language.
*/
allStylesheets.forEach(function(styleid){
var stylusTaskName = " stylus: " + styleid;
stylusTasks.push(stylusTaskName);
compilationTasks.push(stylusTaskName);
function compileStylusTask() {
return gulp.src(config.paths.src.styl + "/stylesheets/" + styleid + ".styl")
.pipe(stylus({
use: [nib()],
"include css": true,
errors: true
}))
.pipe(postcss([ autoprefixer() ]))
.pipe(gulp.dest(config.paths.build.css))
.pipe(touch())
.pipe(reload({ stream: true }));
}
gulp.task(stylusTaskName, compileStylusTask);
})
/**
* Create all the copy tasks
*/
allFoldersToCopy.forEach(function(opts){
var copyTaskName = " copy-" + opts.id;
copyTasks.push(copyTaskName);
function copyTask() {
return gulp.src(opts.src)
.pipe(gulp.dest(opts.dst))
.pipe(touch())
.pipe(reload({ stream: true }));
}
gulp.task(copyTaskName, copyTask)
})
/**
* Reads compiled ArchieML content from the local JSON file.
*/
function readContentFromFile(doneCallback) {
fs.readFile("content.json", function(err, data){
if (!err) {
content = JSON.parse(data);
doneCallback();
}
else {
console.log("Cannot load content from file");
doneCallback(err);
}
});
}
/**
* Clear files from the build directory.
*/
function cleanTask() {
return del([
config.dirs.build + "/*"
]);
}
gulp.task("clean", cleanTask);
gulp.task("reload", (cb) => browserSync.reload() || cb())
/**
* Watches project files for changes and runs the appropriate copy/compile
* tasks.
*/
function watchTask(done) {
allFoldersToCopy.forEach(opts => gulp.watch(opts.src, gulp.series(` copy-${opts.id}`, "reload")))
datafiles.forEach(df => gulp.watch(df.fn, gulp.series(df.taskName, pugTasks, "reload")))
gulp.watch(config.paths.src.js + "/**", gulp.series(browserifyTasks, "reload"));
gulp.watch(config.paths.src.styl + "/**", gulp.series(stylusTasks, "reload"));
gulp.watch(config.paths.src.pug + "/**", gulp.series(contentTasks, pugTasks, "reload"));
done();
}
gulp.task("watch", watchTask);
/**
* Starts the browsersync server.
*/
function browserSyncTask() {
browserSync({
server: {
baseDir: "build",
routes : {
"/aws-assets": "src/aws-assets",
"/aws-data": "src/aws-data"
},
},
injectChanges: true,
open: false
});
}
gulp.task("browser-sync", gulp.series("watch", browserSyncTask));
// create the paralell tasks from the populated groups
gulp.task("content-tasks", gulp.parallel(...contentTasks));
gulp.task("compilation-tasks", gulp.parallel(...compilationTasks));
gulp.task("copy-tasks", gulp.parallel(...copyTasks))
/**
* Command tasks
*
* these tasks wrap all the above into the commands we run
*/
// run the __build.sh command to move files as appropriate
gulp.task("shell-commands", shell.task(shellCmd))
// Do everything in the right order!
gulp.task("build", gulp.series(
"content-tasks",
"preprocess",
"compilation-tasks",
"copy-tasks",
"shell-commands"
));
// Only run browser-sync if we're doing local dev
var defaultTasks = args.build ? gulp.series("clean", "build") : gulp.series("clean", "build", "browser-sync");
gulp.task("default", defaultTasks)