-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgulpfile.js
153 lines (131 loc) · 3.67 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
/* eslint no-console: "off" */
const gulp = require('gulp');
const babel = require('gulp-babel');
const del = require('del');
const glob = require('glob');
const Mocha = require('mocha');
const eslint = require('eslint');
const GLOB = {
lib: './lib/**/*.js',
build: './build/**/*.js',
spec: './test/**/*.spec.js',
};
gulp.task('clean', () => {
return del([GLOB.build]);
});
gulp.task('build', ['clean'], () => {
return gulp
.src(GLOB.lib)
.pipe(babel())
.pipe(gulp.dest('build/'));
});
/**
* Clear module cache.
*/
function clearModuleCache(path) {
delete require.cache[path];
}
/**
* Run tests in the specified file pattern using Mocha
*/
function runTests(pattern, options) {
const mocha = new Mocha(options);
const files = glob.sync(pattern, { realpath: true });
files.forEach(file => {
clearModuleCache(file); // For watching
mocha.addFile(file);
});
return new Promise((resolve, reject) => {
try {
mocha.run(resolve);
} catch (e) {
reject(e);
}
});
}
/**
* Run a given task. And re-run it whenever the specified files change.
*/
function runAndWatch(watchPattern, initialValue, task) {
gulp.watch(watchPattern, event => {
task(event.path, event);
});
return task(initialValue);
}
gulp.task('test:prepare', ['build'], () => {
const mochaEach = require('./build');
if (typeof mochaEach !== 'function') {
throw new Error('Unexpected exports from compiled code');
}
require('@babel/register');
});
gulp.task('test', ['test:prepare'], () => {
return runTests(GLOB.spec)
.then(exitCode => process.exit(exitCode))
.catch(e => {
throw e;
});
});
gulp.task('test:watch', ['test:prepare'], () => {
function test() {
runTests(GLOB.spec, { reporter: 'dot' }).catch(e => console.log(e.stack));
}
const sourceFiles = glob.sync(GLOB.lib, { realpath: true });
gulp.watch(GLOB.lib, () => {
sourceFiles.forEach(f => clearModuleCache(f));
test();
});
runAndWatch(GLOB.spec, null, () => test());
});
/**
* Lint the specified files using eslint.
*/
function lintFiles(pattern, strict, configs) {
const linter = new eslint.CLIEngine(configs);
const report = linter.executeOnFiles([pattern]);
const formatter = linter.getFormatter();
console.log(formatter(report.results));
if (0 < report.errorCount || (strict && 0 < report.warningCount)) {
throw new Error('eslint reports some problems.');
}
}
gulp.task('lint:lib', () => {
lintFiles(GLOB.lib, true);
});
gulp.task('lint:test', () => {
lintFiles(GLOB.spec, true);
});
gulp.task('lint:gulp', () => {
lintFiles('./gulpfile.js', true, {
rules: { 'no-console': 0 },
});
});
gulp.task('lint:doc', () => {
lintFiles('./README.md', true, {
plugins: ['markdown'],
rules: { 'no-undef': 0 },
});
});
gulp.task('lint:watch', () => {
const linter = new eslint.CLIEngine();
function lintAndReport(path) {
const report = linter.executeOnFiles([path]);
const formatter = linter.getFormatter();
console.log(formatter(report.results));
}
runAndWatch(GLOB.lib, GLOB.lib, lintAndReport);
runAndWatch(GLOB.spec, GLOB.spec, lintAndReport);
});
gulp.task('lint', ['lint:lib', 'lint:test']);
gulp.task('lint:all', ['lint', 'lint:gulp', 'lint:doc']);
gulp.task('check', ['lint:all', 'test']);
gulp.task('default', ['lint:watch', 'test:watch']);
gulp.task('doc', () => {
// ESDoc only supports Node.js v6 or later
// but I want to run tests on Node.js v4
// so do not require ESDoc when `gulp check` is run.
const ESDoc = require('esdoc/out/src/ESDoc');
const ESDocPublisher = require('esdoc/out/src/Publisher/publish');
const config = require('./esdoc.json');
ESDoc.generate(config, ESDocPublisher);
});