coool coool - 7 months ago 72
Node.js Question

How can Gulp be restarted upon each Gulpfile change?

I am developing a

. Can it be made to restart as soon as it changes? I am developing it in CoffeeScript. Can
in order to restart when changes are saved?


You can create a task that will for gulpfile.js and simply spawn another gulp child_process.

var gulp = require('gulp'),
    argv = require('yargs').argv, // for args parsing
    spawn = require('child_process').spawn;

gulp.task('log', function() {
  console.log('CSSs has been changed');

gulp.task('watching-task', function() {'*.css', ['log']);

gulp.task('auto-reload', function() {
  var p;'gulpfile.js', spawnChildren);

  function spawnChildren(e) {
    // kill previous spawned process
    if(p) { p.kill(); }

    // `spawn` a child `gulp` process linked to the parent `stdio`
    p = spawn('gulp', [argv.task], {stdio: 'inherit'});

I used yargs in order to accept the 'main task' to run once we need to restart. So in order to run this, you would call:

gulp auto-reload --task watching-task

And to test, call either touch gulpfile.js or touch a.css to see the logs.