If you have a task that starts and finishes, that needs to be run every time files change, that well documented here https://gulpjs.com/docs/en/getting-started/watching-files
const { watch } = require('gulp');
exports.default = function() {
// The task will be executed upon startup
watch('src/*.js', { ignoreInitial: false }, function(cb) {
// body omitted
cb();
});
};
But is there a way to use gulp watch like nodemon? Where you leave a task running, and then stop and start it whenever a file on the watchlist is changed?
--- more ---
was asked for some examples, so here are some examples that will not work
the problem is that I don't know how to set it up so that it stops the existing server whenever the watch is triggered.
---- example #1 - run server in process -----
exports.default = function() {
watch('src/*.js', { ignoreInitial: false }, function(cb) {
// this will call back but when the watch is triggered again
// it will try to start another instance
app.listen(3000, cb);
});
};
---- example #2 - run server in its own process process -----
exports.default = function() {
watch('src/*.js', { ignoreInitial: false }, function(cb) {
const proc = spawn('node', ['server.js']);
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
// this will never call the call back
// so never complete
cb();
});
};