Grunt registerTask() Not Running Tasks in List

1.1k views Asked by At

I have added the following registerTask calls to this Gruntfile.js

grunt.task.registerTask('runDebug', 'Run the app with debug flag', function() {
  var done = this.async();
  grunt.util.spawn({
      cmd: 'node',
      args: ['--debug', './node_modules/nodemon/nodemon.js', 'index.js'],
      opts: {
        stdio: 'inherit'
      }
    }, function (error, result, code) {
      if (error) {
          grunt.log.write (result);
          grunt.fail.fatal(error);
      }
      done();
    });
  grunt.log.writeln ('node started');
  grunt.util.spawn({
      cmd: 'node-inspector',
      args: ['&'],
      opts: {
          //cwd: current working directory
      }
    },
    function (error, result, code) {
      if (error) {
        grunt.log.write (result);
        grunt.fail.fatal(error);
      }
      done();
    });
  grunt.log.writeln ('inspector started');
});

grunt.task.registerTask('debug', ['runDebug', 'compile', 'watch']);

The new debug task is similar to the existing server task. However, grunt server command runs compile, watch, and runNode tasks, whereas grunt debug command only runs runDebug task.

What am I missing here? Why aren't the compile and watch tasks run with grunt debug command.

1

There are 1 answers

2
Louis On

Your code is calling the done() function returned by this.async() more than once. This could be confusing Grunt. I would suggest calling a function of your own which could named spawned() instead of calling done() directly in your callbacks. The function could be something like:

var expected_spawns = 2;
function spawned() {
    if (!--expected_spawns)
        done();

    if (expected_spawns < 0)
        throw new Error("too many spawns!") // Or some of Grunt's fail functions.
}

This way done() will be called once.