-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
88 lines (69 loc) · 2.09 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
"use strict";
///////////////////////////////////////////////////////////////////////////
const _ = require('lodash');
const P = require('bluebird');
const path = require('path');
const gulp = require('gulp');
const fswatch = require('gulp-watch');
const concat = require('gulp-concat');
const subprocess = require('child_process');
const forever = new P((resolve, reject) => null);
///////////////////////////////////////////////////////////////////////////
const PROJECT_ROOT = __dirname;
const PROJECT = path.basename(PROJECT_ROOT);
const SRC = path.join(PROJECT_ROOT, 'src');
const BUILD = path.join(PROJECT_ROOT, 'build');
///////////////////////////////////////////////////////////////////////////
/**
* Register a filesystem watcher
* @param {Array} globs
* @param {Function} task
* @return void
*/
const watch = (globs, task) => {
let debounce = null;
let wrapper = () => {
try {
task();
}
catch (e) {
console.log(e);
}
};
fswatch(globs, {usePolling: false}, (vinyl) => {
clearTimeout(debounce);
debounce = setTimeout(wrapper, 100);
});
};
/**
* Task decorator function that logs task execution time
* @param {String} name name of the task
* @param {Function} fn the task
* @return {Function}
*/
const task = (name, fn) => {
return () => {
let start = Date.now();
return fn().then(() => {
console.log('%s task finished in %sms', name, Date.now() - start);
})
};
};
///////////////////////////////////////////////////////////////////////////
const tasks = {};
tasks.test = task('test', () => {
let src = path.join(__dirname, 'src');
let subproc = subprocess.spawn('bin/mocha', {stdio: 'inherit', cwd: src});
let log = (loggable) => console.log(String(loggable));
return new P((resolve, reject) => {
subproc.on('error', reject);
subproc.on('end', resolve);
});
})
tasks.watch = task('watch', (done) => {
watch([SRC + '/**/*.js'], tasks.test);
return P.resolve();
});
///////////////////////////////////////////////////////////////////////////
gulp.task('test', tasks.test);
gulp.task('default', tasks.watch);