-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
415 lines (351 loc) · 13.1 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
/******************************************************************************************
Automated building of JavaScript and LESS files courtesy of Gulp
******************************************************************************************/
require("./sanitycheck.js");
try {
var package = require("./package.json");
var config = require("./config.js");
var path = require("path");
var fs = require("fs");
var util = require("util");
var parseargs = require("minimist");
var gulp = require("gulp");
var gulpsync = require("gulp-sync")(gulp);
var watch = require("gulp-watch");
var less = require("gulp-less");
var sourcemaps = require("gulp-sourcemaps");
var uglify = require("gulp-uglify");
var gulpif = require("gulp-if");
var rename = require("gulp-rename");
var filesize = require("gulp-filesize");
var iconfont = require("gulp-iconfont");
var consolidate = require("gulp-consolidate");
var ngtemplates = require("gulp-ng-templates");
var htmlmin = require("gulp-htmlmin");
var buffer = require("vinyl-buffer");
var source = require("vinyl-source-stream");
var browserify = require("browserify");
var watchify = require("watchify");
var babel = require("babelify");
var LessPluginCleanCSS = require("less-plugin-clean-css");
var cleancss = new LessPluginCleanCSS({ advanced: true, verbose: true, debug: true });
var replace = require("gulp-replace");
var htmlhint = require("gulp-htmlhint");
var gutil = require("gulp-util");
var findparentdir = require("find-parent-dir");
var mkdirp = require("mkdirp");
var glob = require("glob")
var es = require("event-stream");
var prompt = require("prompt-sync")();
var settings = require("./settings.js");
var eslint = require("gulp-eslint");
var tasksGit = require("./tasks-git.js");
var tasksMisc = require("./tasks-misc.js");
var connect = require("gulp-connect");
}
catch (error) {
console.log(error);
console.log("\nA required module may be missing, try the following command to fix this:\n npm install\n");
process.exit(1);
}
var argv = parseargs(process.argv.slice(2));
var iconList = [];
var watching = false;
// Return root path for minifify
var compressPath = function(p) {
return p;
// Despite the docs saying this works, Chrome seems to show it doesn't
return path.relative(settings.paths.build.source, p);
};
var errorHandler = function(err) {
if (err instanceof SyntaxError) {
gutil.log(gutil.colors.red("ERROR:"), err.message);
console.log(err.codeFrame);
}
else if (err instanceof Error)
gutil.log(gutil.colors.red("ERROR:"), err.message);
else
gutil.log(err);
if (watching)
this.emit("end");
else
process.exit(1);
};
// Build icon font
gulp.task("iconfont", function(){
return gulp.src([settings.paths.build.source + "font-svgs/iconfont/*.svg"])
.pipe(iconfont({
fontName: "iconfont",
appendCodepoints: false
}))
.on("glyphs", function(glyphs, options) {
iconList = [];
// Make a list of glyphs we can provide the app version page with
for (var u = 0; u < glyphs.length; u++)
iconList.push(glyphs[u].name);
// Create the CSS template
return gulp.src(settings.paths.build.source + "less/font.less.template")
.pipe(consolidate("swig", {
glyphs: glyphs,
fontName: "iconfont",
fontPath: "../fonts/",
className: "iconfont",
commithash: settings.githash
}))
.pipe(rename("iconfont.less"))
.pipe(gulp.dest(settings.paths.build.source + "less/"));
})
.pipe(gulp.dest(settings.paths.build.target + "fonts/"));
});
// Compile CSS
gulp.task("css", function(done) {
// List available theme files
var themePaths = settings.buildPathArray(settings.paths.build.source, settings.lists.css.build);
var streams = [];
themePaths.forEach((matchPath, index) => {
var themeFiles = glob.sync(matchPath);
themeFiles.forEach((theme, index) => {
var basename = path.basename(theme, ".less");
// Process each theme file
streams.push(gulp.src(settings.buildPathArray(settings.paths.build.source, settings.lists.css.master), { base: settings.paths.build.source })
.pipe(replace(/{{theme-name}}/ig, "themes/" + basename))
.pipe(replace(/{{iconfont-path}}/ig, ""))
.pipe(sourcemaps.init({ loadMaps: true, debug: true }))
.pipe(less({
plugins: [
cleancss
]
}))
.on("error", errorHandler)
.pipe(rename(basename + ".main.min.css"))
.pipe(filesize())
.pipe(sourcemaps.write("../maps"))
.pipe(gulp.dest(settings.paths.build.target + "css/")));
});
});
es.merge(streams).on("end", done);
});
// Copy static html templates
gulp.task("html", function() {
return gulp.src(settings.buildPathArray(settings.paths.build.source, settings.lists.html.build))
.pipe(replace(/{{commithash}}/ig, settings.githash))
.pipe(replace(/{{branch}}/ig, settings.gitbranch))
.pipe(replace(/{{fonturl}}/ig, config.fonts))
.pipe(filesize())
.pipe(gulp.dest(settings.paths.static.targethtml));
});
// Compile an individual directory's partials
function compilePartials(filepath) {
if (process.platform === "win32")
var pathArray = filepath.split("\\");
else
var pathArray = filepath.split("/");
pathArray.pop();
var basename = pathArray.pop();
pathArray.push(basename);
var destpath = pathArray.join("/");
return gulp.src(filepath + "/**/*.html")
.pipe(htmlhint({ "doctype-first": false }))
.pipe(watching ? htmlhint.reporter() : htmlhint.failReporter())
.pipe(ngtemplates({
htmlMinifier: settings.minifiy,
module: basename + ".partials",
path: function(path, base) {
if (!base.endsWith("partials/"))
base += "partials/";
return basename + "/" + path.replace(base, "");
}
}))
.pipe(uglify(settings.uglify))
.pipe(rename("partials.js"))
.pipe(filesize())
.pipe(gulp.dest(destpath));
};
// Compile all partials
gulp.task("partials", function(done) {
// List available partial files
var partialPaths = settings.buildPathArray(settings.paths.build.source, settings.lists.apps.partials);
var streams = [];
partialPaths.forEach((matchPath, index) => {
var partialFiles = glob.sync(matchPath);
partialFiles.forEach((partial, index) => {
streams.push(compilePartials(partial));
});
});
es.merge(streams).on("end", done);
});
// Compile app scripts
gulp.task("apps-scripts", ["partials"], function(done) {
// List available app scripts
var appPaths = settings.buildPathArray(settings.paths.build.source, settings.lists.apps.build);
var streams = [];
appPaths.forEach((matchPath, index) => {
var appFiles = glob.sync(matchPath);
appFiles.forEach((app, index) => {
var basename = path.basename(app, "-main.js");
if (argv.apps) {
var appList = argv.apps.split(",");
var matched = false;
appList.forEach((appName, index) => {
if (basename.toLowerCase().indexOf(appName.toLowerCase()) != -1)
matched = true;
});
if (!matched)
return;
}
app = path.normalize(app.replace(settings.paths.build.source, ""));
var bundler = browserify(settings.browserify);
if (watching)
bundler.plugin(watchify);
bundler.transform(babel, settings.babel);
bundler.add(app);
bundler.external("moment");
var bundle = function() {
gutil.log("Bundling js module '" + basename + "'");
return bundler
.bundle()
.on("error", errorHandler)
.pipe(source(basename + ".min.js"))
.pipe(buffer())
.pipe(replace(/\/\*{{gulp-build-version}}\*\//ig, "version: '" + package.version + "', commithash: '" + settings.githash + "', gitbranch: '" + settings.gitbranch + "', release: '" + package.release + "', builddate: new Date(" + JSON.stringify(new Date()) + "), iconlist: " + JSON.stringify(iconList)))
.pipe(replace(/\/\*{{gulp-env-config}}\*\//ig, JSON.stringify(config).slice(1, -1)))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(gulpif(settings.compress, uglify(settings.uglify)))
.pipe(sourcemaps.write("../maps"))
.pipe(gulp.dest(settings.paths.build.target + "js/"));
}
if (watching) {
bundler.on("update", function() {
bundle().on("finish", function() {
gulp.start("copyto");
});
});
bundler.on("log", function(msg) {
gutil.log(basename + ": " + msg);
});
}
streams.push(bundle());
});
});
es.merge(streams).on("end", done);
});
// Watch files and trigger minimal builds
gulp.task("watch", function(done) {
watching = true;
gulp.start("dev");
watch(settings.buildPathArray(settings.paths.static.source, settings.lists.css.watch), function(file) {
var basename = path.basename(file.path);
var extension = path.extname(file.path);
var destination = path.normalize(settings.paths.build.source + path.relative(settings.paths.static.source, file.path));
gutil.log("Watched less file changed: '" + basename + "'");
// check if the file has been removed
fs.access(file.path, function(err) {
if (!err) {
fs.createReadStream(file.path)
.pipe(fs.createWriteStream(destination)
.on("close", function() {
gulp.start(gulpsync.sync([ "css", "copyto" ]));
})
);
} else {
// file has been removed, cleanup destination
fs.unlinkSync(destination);
gulp.start(gulpsync.sync([ "css", "copyto" ]));
}
});
});
// Make all eslint options warnings as we are watching
for (key in settings.eslint.rules) {
if (Array.isArray(settings.eslint.rules[key]) && (settings.eslint.rules[key][0] == 2))
settings.eslint.rules[key][0] = 1;
else if (settings.eslint.rules[key] == 2)
settings.eslint.rules[key] = 1;
}
watch(settings.buildPathArray(settings.paths.static.source, settings.lists.apps.watch), function(file) {
var basename = path.basename(file.path);
var extension = path.extname(file.path);
var destination = path.normalize(settings.paths.build.source + path.relative(settings.paths.static.source, file.path));
gutil.log("Watched app file changed: '" + basename + "'");
var compile = function() {
switch (extension) {
case ".html":
// Find the parent partials file so we can rebuild this partial set only
findparentdir(destination, "partials.js", function(error, directory) {
if (error)
gutil.log("Unable to find partial file: " + error);
else
compilePartials(directory);
});
break;
case ".js":
// Do nothing for js files but lint them, watchify will notice the new file has been copied over
// unfortunately watchify doesn't handle adding or removing files so you will need to restart watchify
gulp.src(file.path)
.pipe(eslint(settings.eslint))
.pipe(eslint.format())
.pipe(eslint.failAfterError())
.on("error", (error) => {
this.emit("end");
});
break;
}
};
// check if the file has been removed
fs.access(file.path, function(err) {
if (!err) {
fs.createReadStream(file.path)
.pipe(fs.createWriteStream(destination)
.on("close", compile)
);
} else {
// file has been removed, cleanup destination
fs.unlinkSync(destination);
compile();
}
});
});
done();
});
// Main release build chain
gulp.task("build", gulpsync.sync(["gitinfo", "copyfrom", "iconfont", "css", "html", "apps-scripts", "copyto"], "sync release"));
gulp.task("build-lint", gulpsync.sync(["gitinfo", "copyfrom", "eslint", "iconfont", "css", "html", "apps-scripts", "copyto"], "sync release-lint"));
// Uncompressed release build chain
gulp.task("dev", gulpsync.sync(["uncompressed", "build"], "sync dev"));
// Uncompressed es6 release build chain
gulp.task("dev-es6", gulpsync.sync(["es6", "uncompressed", "build"], "sync dev-es6"));
// Default release build chain with clean
gulp.task("clean-build", gulpsync.sync(["clean-all", "build"], "sync default"));
// Font creation build chain
gulp.task("icons", gulpsync.sync(["clean-all", "copyfrom", "iconfont", "copyto"], "sync icons"));
// ESLint test build chain
gulp.task("lint", gulpsync.sync(["clean-all", "copyfrom", "eslint"], "sync lint"));
// Aliases
gulp.task("build-dev", ["dev"]);
gulp.task("debug", ["dev"]);
// Less only build chain
gulp.task("less", gulpsync.sync(["gitinfo", "uncompressed", "copyfrom", "html", "iconfont", "css", "copyto"], "sync css"));
// App scripts only build chain
gulp.task("apps", gulpsync.sync(["gitinfo", "uncompressed", "copyfrom", "html", "iconfont", "apps-scripts", "copyto"], "sync apps"));
gulp.task("server", ["dev"], function() {
connect.server({ root: config.target });
});
// Present help info
gulp.task("help", function() {
console.log("options:");
console.log("build\n : standard build");
console.log("build-upload\n : standard build and upload to artifactory");
console.log("dev\n : unminified build");
console.log("dev-es6\n : unminified es6 browser build");
console.log("less\n : only build CSS files");
console.log("less --themes prs,ssr\n : only build themes beginning with prs and ssr");
console.log("apps\n : only build app bundles");
console.log("icons\n : build just the icon font and matching less file");
console.log("clean\n : clean build directories");
console.log("clean-all\n : clean build and target static directories");
console.log("watch\n : build on changes");
console.log("watch --apps main\n : only build matching apps on changes");
console.log("lint\n : run eslint tests");
console.log("lint --fix\n : run eslint tests and attempt to fix issues");
});
// Default build task
gulp.task("default", ["server"]);