-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·70 lines (61 loc) · 1.87 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
'use strict';
const {src, dest, series, parallel, watch} = require('gulp');
const pug = require('pug');
const path = require('path');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const through2 = require('through2');
const rename = require('gulp-rename');
// Code for the pug template
const pugTemplatePath = "template.pug";
let pugTemplate = pug.compileFile(pugTemplatePath);
function buildHTML(path) {
return src("*/info.json")
.pipe(through2.obj(function(file, _, cb) {
// Inline template to build a HTML file from the info.json
if(file.isBuffer()){
const pugResults = pugTemplate({
// Populate the object passed to pug with all of the info.json data
...(JSON.parse(file.contents.toString())),
// Get the directory name relative to the base folder (with the info.json removed)
gitPath: file.relative.slice(0, -("info.json".length))
});
file.contents = Buffer.from(pugResults);
} else {
console.log("Oh Crap, didn't receive a buffer from the thingamabob.");
}
cb(null, file);
}))
.pipe(rename(function(path) {
path.basename = "index";
path.extname = ".html";
}))
.pipe(dest("."));
}
function buildJs(jsonFile, callback){
return src("*/sketch.js")
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(uglify())
.pipe(rename(function(path) {
// Add a .min. into the js name
path.basename = "sketch";
path.extname = ".min.js";
}))
.pipe(dest("."));
}
exports.buildAll = parallel(buildHTML, buildJs);
exports.watchAll = function () {
// Build once
exports.buildAll();
// Update the pug template if it changes
watch(pugTemplatePath, series(function(cb){
pugTemplate = pug.compileFile(pugTemplatePath);
cb();
}, buildHTML));
// Update html processing
watch("*/info.json", buildHTML);
// Update js processing
watch(["**/*.js", "!node_modules/*", "!**/*.min.js"], buildJs);
}