The function gulp.watch()
returns an object that has, amongst other things, an end()
method you can call - see the definition in the glob-watcher module.
You could do something along these lines:
var gulp = require('gulp');
function doGitCheckout(callback) {
console.log('doing checkout...');
setTimeout(function() {
console.log('done checkout...');
callback();
}, 1000);
}
var watcher;
gulp.task('watch:start', function() {
if (!watcher) {
watcher = gulp.watch('dist/**/*', ['build']);
console.log('started watcher');
}
});
gulp.task('watch:stop', function() {
if (watcher) {
watcher.end();
watcher = null;
console.log('stopped watcher');
}
});
gulp.task('git:checkout', ['watch:stop'], function(cb) {
console.log('isWatching = ' + !!watcher);
// Start the Git checkout...
doGitCheckout(function() {
// The Git checkout is finished!
gulp.start('watch:start', function() {
cb();
});
});
});
gulp.task('default', ['watch:start', 'git:checkout']);
Gist with this code here
So we keep a reference to the watcher object when we start watching, and have a task to stop the watcher if running. Our Git task depends on watch:stop
to begin, and then starts the Git checkout, with a callback for when it's completed. The callback starts the task watch:start
, and when that's done, we finally trigger the function cb
to indicate that git:checkout
has finished!
An alternative might be using the add(pattern)
and remove(pattern)
on the watch object to turn it off and then on again instead of destroying the object and recreating it.