0

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();
  });
};
Alex028502
  • 3,486
  • 2
  • 23
  • 50

1 Answers1

0

Well, at least you can do like that:

% gulp someTask >/dev/null 2>&1 &
[1] _pid_

And your gulp task will run indefinitely if it's a task like watch.

But this approach is very dirty. You should use something like nodemon to achieve that.