Skip to content

Instantly share code, notes, and snippets.

@spiralx
Last active December 15, 2016 23:37
Show Gist options
  • Save spiralx/aee491b29663a12688ef to your computer and use it in GitHub Desktop.
Save spiralx/aee491b29663a12688ef to your computer and use it in GitHub Desktop.
Gulp - suspend watcher during task execution
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']);
@davidrenne
Copy link

This looked promising for my use case, but my use case uses gulp.watch() which is a long running process and I tried tweaking this example to stop my process and it only seemed to work once no matter what I did. Oh well I found another solution to get my process to not fight with gulp running and killing processes when it detected file changes.

@kevinbeal
Copy link

@davidrenne What was the other solution? This solution doesn't work for me either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment