generated from bitmodo/node-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
423 lines (337 loc) · 11.9 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
// @ts-nocheck
// Gulp functions
const { src, dest, parallel, series, watch, lastRun } = require('gulp');
// Utility Gulp modules
const through = require('through2');
const merge = require('merge2');
// const gulpIf = require('gulp-if');
// Compilation Gulp plugins
const sourcemap = require('gulp-sourcemaps');
const ts = require('gulp-typescript');
const terser = require('gulp-terser-js');
// Check Gulp plugins
const mocha = require('gulp-mocha');
const tslint = require('gulp-tslint');
const { Linter } = require('tslint');
// Utility modules
const fs = require('fs');
const del = require('del');
const path = require('path');
const { fork } = require('child_process');
// Settings
const paths = require('./paths');
/**
* @param {string} string
* @returns {string}
*/
function capitalize(string) {
return `${string.substr(0, 1).toUpperCase()}${string.substr(1)}`;
}
function isValid(project) {
return fs.statSync(paths.project(project)).isDirectory()
&& fs.existsSync(paths.packageJson(project));
}
const prefix = '@bitform/';
function fixPrefix(pkg) {
if (pkg.startsWith(prefix))
return pkg.substr(prefix.length);
return pkg;
}
function flatten(array, result) {
for (const element of array) {
if (Array.isArray(element)) {
flatten(element, result);
} else {
result.push(element);
}
}
}
function flat(array) {
let result = [];
flatten(array, result);
return result;
}
// Function generators
function generateBuildFunction(displayName, projects) {
let globs = [];
for (let project of projects) {
globs.push(paths.libGlob(project));
}
return function () {
const project = ts.createProject(paths.tsconfig, { skipLibCheck: true });
const directoryFixer = () => through.obj(function (file, _, cb) {
file.path = file.path.split(path.sep).map(part => part === 'lib' ? 'dist' : part).join(path.sep);
cb(null, file);
});
const out = src(globs, { since: lastRun(displayName), base: paths.root })
.pipe(sourcemap.init({ loadMaps: true }))
.pipe(project());
return merge([
out.js
.pipe(terser({
compress: {
ecma: 2018,
module: true,
},
mangle: {
module: true,
},
output: {
ecma: 2018,
},
ecma: 2018,
module: true,
}))
.pipe(directoryFixer())
.pipe(sourcemap.mapSources(function (sourcePath, file) {
if (sourcePath.endsWith('.ts')) {
return path.relative(path.dirname(file.relative), sourcePath);
}
return sourcePath;
}))
.pipe(sourcemap.write('.')),
out.dts
.pipe(directoryFixer())
]).pipe(dest(paths.root));
};
}
function generateMochaFunction(name) {
return function () {
// noinspection JSValidateTypes
process.env.TS_NODE_PROJECT = paths.tsconfig;
return src(paths.testGlob(name))
.pipe(mocha());
};
}
function generateNycFunction(name) {
return function (cb) {
let nycBin = path.join(require.resolve('nyc'), '..', require('nyc/package.json').bin.nyc);
let mochaBin = path.join(require.resolve('mocha'), '..', 'bin', 'mocha');
let nycArgs = [
'--all',
'--cache',
`--include='${name === '*' ? paths.relativeAllLibGlob : paths.relativeLibGlob(name)}'`,
'--reporter=clover', '--reporter=lcov', '--reporter=text-summary',
`--report-dir='${name === '*' ? paths.coveragePath : paths.coverage(name)}'`,
`--cache-dir='${name === '*' ? paths.cachePath : paths.cache(name)}'`,
`--temp-dir='${paths.buildPath}'`,
`--cwd='${paths.root}'`,
'--require=ts-node/register',
];
let args = [`${mochaBin}`, `'${name === '*' ? paths.allTestGlob : paths.testGlob(name)}'`];
let child = fork(nycBin, nycArgs.concat(args), {
cwd: paths.cwd,
detached: false,
env: {
...process.env,
TS_NODE_PROJECT: paths.tsconfig,
},
});
child.on('error', (e) => {
cb(e);
});
child.on('close', (code) => {
if (code) {
cb(new Error('Error running code coverage'));
} else {
cb();
}
});
};
}
// Task functions
function addBuildTask(name, projects) {
const displayName = `build:${name}`;
const fn = generateBuildFunction(displayName, projects);
let taskName = `build${capitalize(name)}`;
fn.name = taskName;
fn.displayName = displayName;
fn.description = `Build the ${name} project`;
exports[taskName] = fn;
return fn;
}
function addWatchTask(name, build) {
const fn = function () {
return watch(paths.libGlob(name), build);
};
let taskName = `watch${capitalize(name)}`;
fn.name = taskName;
fn.displayName = `watch:${name}`;
fn.description = `Watch the ${name} project and rebuild when there are changes`;
exports[taskName] = fn;
return fn;
}
function addLintTask(name) {
const fn = function () {
const program = Linter.createProgram(paths.tsconfig, paths.project(name));
return src(paths.libGlob(name))
.pipe(tslint({
fix: false,
configuration: paths.tslint,
program: program,
}))
.pipe(tslint.report({
allowWarnings: true,
}));
};
let taskName = `lint${capitalize(name)}`;
fn.name = taskName;
fn.displayName = `lint:${name}`;
fn.description = `Lint the ${name} project`;
exports[taskName] = fn;
return fn;
}
function addTestTask(name) {
const fn = generateMochaFunction(name);
let taskName = `test${capitalize(name)}`;
fn.name = taskName;
fn.displayName = `test:${name}`;
fn.description = `Test the ${name} project`;
exports[taskName] = fn;
return fn;
}
function addCoverageTask(name) {
const fn = generateNycFunction(name);
let taskName = `coverage${capitalize(name)}`;
fn.name = taskName;
fn.displayName = `coverage:${name}`;
fn.description = `Get the code coverage for the ${name} project`;
exports[taskName] = fn;
return fn;
}
function addCleanTask(name) {
function cleanTask(dir, path) {
const fn = function () {
return del(path);
}
let taskName = `clean${capitalize(name)}${capitalize(dir)}`;
fn.name = taskName;
fn.displayName = `clean:${name}:${dir}`;
fn.description = `Clean out the ${dir} directory for ${name}`;
exports[taskName] = fn;
return fn;
}
const fn = parallel(
cleanTask('dist', paths.dist(name)),
cleanTask('cache', paths.cache(name)),
cleanTask('coverage', paths.coverage(name)),
);
let taskName = `clean${capitalize(name)}`;
fn.name = taskName;
fn.displayName = `clean:${name}`;
fn.description = `Clean out the build products for ${name}`;
exports[taskName] = fn;
return fn;
}
// Setup functions
let projectBuilds = [];
let projectWatches = [];
let projectLints = [];
let projectCoverages = [];
let projectTests = [];
let projectCleans = [];
function setupProject(project) {
let buildTask = addBuildTask(project, [project]);
let watchTask = addWatchTask(project, buildTask);
let lintTask = addLintTask(project);
let coverageTask = addCoverageTask(project);
let testTask = addTestTask(project);
let cleanTask = addCleanTask(project);
projectBuilds.push(buildTask);
projectWatches.push(watchTask);
projectLints.push(lintTask);
projectCoverages.push(coverageTask);
projectTests.push(testTask);
projectCleans.push(cleanTask);
const fn = series(buildTask, lintTask, testTask, coverageTask);
fn.name = project;
fn.displayName = project;
fn.description = `Build and test the ${project} project`;
exports[project] = fn;
}
function setupProjects(projects) {
let buildTasks = [];
let groupNum = 0;
for (const project of projects) {
if (Array.isArray(project)) {
for (const proj of project) {
setupProject(proj);
}
const phase = `phase${groupNum++}`;
const displayName = `build:${phase}`;
const fn = generateBuildFunction(displayName, project);
fn.name = phase;
fn.displayName = displayName;
fn.description = `Build group ${groupNum - 1} projects`;
exports[phase] = fn;
buildTasks.push(fn);
} else {
setupProject(project);
}
}
const buildFn = series(buildTasks);
buildFn.name = 'build';
buildFn.displayName = 'build';
buildFn.description = 'Build all of the projects';
exports.build = buildFn;
const buildAllFn = parallel(projectBuilds);
buildAllFn.name = 'buildAll';
buildAllFn.displayName = 'build:all';
buildAllFn.description = 'Build all of the projects in parallel without checking dependencies';
exports.buildAll = buildAllFn;
const watchFn = series(exports.build, parallel(projectWatches));
watchFn.name = 'watch';
watchFn.displayName = 'watch';
watchFn.description = 'Build all of the projects then watch them and rebuild when they have changes';
exports.watch = watchFn;
const lintFn = parallel(projectLints);
lintFn.name = 'lint';
lintFn.displayName = 'lint';
lintFn.description = 'Lint all of the projects';
exports.lint = lintFn;
const testFn = generateMochaFunction('*');
testFn.name = 'test';
testFn.displayName = 'test';
testFn.description = 'Test all of the projects';
exports.test = testFn;
const coverageFn = generateNycFunction('*');
coverageFn.name = 'coverage';
coverageFn.displayName = 'coverage';
coverageFn.description = 'Get the code coverage for all of the projects';
exports.coverage = coverageFn;
const cleanFn = parallel(projectCleans);
cleanFn.name = 'clean';
cleanFn.displayName = 'clean';
cleanFn.description = 'Clean all of the build products in the projects';
exports.clean = cleanFn;
const fn = series(exports.build, exports.lint, exports.test, exports.coverage);
fn.name = 'default';
fn.displayName = 'default';
fn.description = 'Build, lint, and test all of the projects';
return fn;
}
// Setup all of the tasks
let deps = {};
let packages = fs.readdirSync(paths.packages).filter(isValid);
for (let pkg of packages) {
// noinspection JSUnresolvedVariable
deps[pkg] = Object.keys(require(paths.packageJson(pkg)).dependencies)
.map(fixPrefix);
}
for (let [name, dependencies] of Object.entries(deps)) {
deps[name] = dependencies.filter((dep) => Object.keys(deps).includes(dep));
}
let projects = [];
let group = Object.entries(deps)
.filter(([_, dependencies]) => dependencies.length === 0)
.map(([name, _]) => name);
while (group.length > 0) {
projects.push(group);
for (let name of group) {
delete deps[name];
}
group = Object.entries(deps)
.filter(([_, dependencies]) => dependencies.every((dep) => flat(projects).includes(dep)))
.map(([name, _]) => name);
}
exports.default = setupProjects(projects);