build(gulp): use chokidar alone instead of gulp-watch

Closes #1759
This commit is contained in:
Caitlin Potter 2015-05-08 14:20:54 -04:00
parent c36ea0221e
commit 23cec1e8e2
2 changed files with 110 additions and 43 deletions

View File

@ -16,7 +16,7 @@ var merge = require('merge');
var merge2 = require('merge2'); var merge2 = require('merge2');
var path = require('path'); var path = require('path');
var semver = require('semver'); var semver = require('semver');
var watch = require('gulp-watch'); var watch = require('./tools/build/watch');
var clean = require('./tools/build/clean'); var clean = require('./tools/build/clean');
var transpile = require('./tools/build/transpile'); var transpile = require('./tools/build/transpile');
@ -222,9 +222,7 @@ gulp.task('build/checkCircularDependencies', function (done) {
// ------------------ // ------------------
// web servers // web servers
gulp.task('serve.js.dev', ['build.js.dev'], function(neverDone) { gulp.task('serve.js.dev', ['build.js.dev'], function(neverDone) {
watch('modules/**', function() { watch('modules/**', '!broccoli.js.dev');
gulp.start('!broccoli.js.dev');
});
jsserve(gulp, gulpPlugins, { jsserve(gulp, gulpPlugins, {
path: CONFIG.dest.js.dev.es5, path: CONFIG.dest.js.dev.es5,
@ -304,7 +302,7 @@ function createDocsTasks(publicBuild) {
gulp.task(taskPrefix, [taskPrefix + '/assets', taskPrefix + '/app', taskPrefix + '/dgeni']); gulp.task(taskPrefix, [taskPrefix + '/assets', taskPrefix + '/app', taskPrefix + '/dgeni']);
gulp.task(taskPrefix + '/watch', function() { gulp.task(taskPrefix + '/watch', function() {
return watch('docs/app/**/*', [taskPrefix + '/app']); return watch('docs/app/**/*', { ignoreInitial: false }, [taskPrefix + '/app']);
}); });
gulp.task(taskPrefix + '/test', function (done) { gulp.task(taskPrefix + '/test', function (done) {
@ -372,20 +370,17 @@ gulp.task('test.unit.js', ['build.js.dev'], function (neverDone) {
'check-format' 'check-format'
); );
watch('modules/**', function() { watch('modules/**', [
runSequence(
'!broccoli.js.dev', '!broccoli.js.dev',
'!test.unit.js/karma-run' '!test.unit.js/karma-run'
); ]);
});
}); });
gulp.task('watch.js.dev', ['build.js.dev'], function (neverDone) { gulp.task('watch.js.dev', ['build.js.dev'], function (neverDone) {
watch('modules/**', function() { watch('modules/**', [
runSequence( '!broccoli.js.dev',
'!broccoli.js.dev' '!test.unit.js/karma-run',
); ]);
});
}); });
@ -404,19 +399,16 @@ gulp.task('!test.unit.js/karma-run', function(done) {
gulp.task('test.unit.dart', ['build/tree.dart'], function (done) { gulp.task('test.unit.dart', ['build/tree.dart'], function (done) {
runSequence( runSequence(
'!test.unit.dart/karma-server', '!test.unit.dart/karma-server',
'!test.unit.dart/karma-run' '!test.unit.dart/karma-run'
); );
watch('modules/angular2/**', function() { watch('modules/angular2/**', [
runSequence(
'!build/tree.dart', '!build/tree.dart',
'build/format.dart', 'build/format.dart',
'!test.unit.dart/karma-run' '!test.unit.dart/karma-run'
); ]);
});
}); });
gulp.task('!test.unit.dart/karma-run', function (done) { gulp.task('!test.unit.dart/karma-run', function (done) {
@ -454,37 +446,29 @@ gulp.task('test.unit.cjs/ci', function(done) {
gulp.task('test.unit.cjs', ['build/clean.js', 'build.tools'], function (done) { gulp.task('test.unit.cjs', ['build/clean.js', 'build.tools'], function (done) {
function buildAndTest() { var buildAndTest = [
runSequence(
'!build.js.cjs', '!build.js.cjs',
'test.unit.cjs/ci' 'test.unit.cjs/ci'
); ];
}
buildAndTest(); watch('modules/**', { ignoreInitial: false }, buildAndTest);
watch('modules/**', buildAndTest);
}); });
gulp.task('test.unit.tools/ci', function(done) { gulp.task('test.unit.tools/ci', function(done) {
fork('./tools/traceur-jasmine', ['dist/tools/**/*.spec.js'], { fork('./tools/traceur-jasmine', ['dist/tools/**/*.spec.js'], {
stdio: 'inherit' stdio: 'inherit'
}).on('close', done); }).on('close', done.bind(null, null));
}); });
gulp.task('test.unit.tools', ['build/clean.tools'], function(done) { gulp.task('test.unit.tools', ['build/clean.tools'], function(done) {
function buildAndTest() { var buildAndTest = [
runSequence(
'!build.tools', '!build.tools',
'test.unit.tools/ci' 'test.unit.tools/ci'
); ];
}
buildAndTest(); watch(['tools/**', '!tools/**/test-fixtures/**'], {ignoreInitial: false}, buildAndTest);
watch('tools/**', buildAndTest);
}); });

83
tools/build/watch.js Normal file
View File

@ -0,0 +1,83 @@
var chokidar = require('chokidar');
var runSequence = require('run-sequence');
var path = require('path');
function watch(globs, opts, tasks) {
if (typeof opts !== 'object' || Array.isArray(opts)) {
tasks = opts;
opts = {};
}
var useRunSequence = typeof tasks !== 'function';
var runTasks;
if (useRunSequence) {
if (!Array.isArray(tasks)) tasks = [tasks];
tasks = tasks.slice();
tasks.push(tasksDone);
runTasks = function runTaskSequence() {
runSequence.apply(null, tasks);
}
} else {
var sync = tasks.length === 0;
runTasks = function runCallback() {
try {
tasks(tasksDone);
if (sync) tasksDone();
} catch (e) {
return tasksDone(e);
}
}
}
var events = opts.events = opts.events || ['add', 'change', 'unlink'];
if (opts.ignoreInitial === undefined) opts.ignoreInitial = true;
var delay = opts.delay;
if (delay === undefined) delay = 100;
var watcher = chokidar.watch(globs, opts).
on('all', handleEvent).
on('error', function(err) {
throw err;
});
var close = watcher.close.bind(watcher);
watcher.close = function() {
if (timeoutId !== null) clearTimeout(timeoutId);
close();
}
var eventsRecorded = 0; // Number of events recorded
var timeoutId = null; // If non-null, event capture window is open
return watcher;
function handleEvent(event, filepath) {
// Ignore unwatched events
if (events.indexOf(event) < 0) return;
// Increment number of events captured in this window
++eventsRecorded;
if (timeoutId === null) {
timeoutId = setTimeout(invokeCallback, delay);
}
}
function invokeCallback() {
eventsRecorded = 0;
runTasks();
}
function tasksDone(err) {
if (err) throw err;
if (eventsRecorded) {
// eventsRecorded has increased during the run, run again on the next turn
timeoutId = setTimeout(invokeCallback, 0);
} else {
timeoutId = null;
}
}
}
module.exports = watch;