2

registerTask为此添加了以下调用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']);

debug任务类似于现有server任务。但是,grunt servercommand 运行compilewatchrunNodetasks,而grunt debugcommand 只运行runDebugtask。

我在这里想念什么?为什么compileandwatch任务不使用grunt debug命令运行。

4

1 回答 1

0

您的代码不止一次调用done()返回的函数。this.async()这可能会让 Grunt 感到困惑。我建议调用你自己的函数,它可以命名spawned()而不是done()直接在你的回调中调用。该函数可能类似于:

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.
}

这种方式done()将被调用一次。

于 2014-02-13T19:47:25.373 回答