包含多个生成任务的 G运行t 任务只会 运行 第一个任务

Grunt task containing multiple spawn tasks will only run the first task

我设置了以下三个任务,它们使用 grunt.util.spawn 来处理各种 Git 任务:

grunt.registerTask('git-add', function() {
  grunt.util.spawn({
    cmd : 'git',
    args: ['add', '.'],
  });
});

grunt.registerTask('git-commit', function(message) {
  grunt.util.spawn({
    cmd : 'git',
    args: ['commit', '-m', message],
  });
});

grunt.registerTask('git-push', function(origin, branch) {
  grunt.util.spawn({
    cmd : 'git',
    args: ['push', origin, branch],
  });
});

运行 这些中的每一个都作为方面单独工作,所以 运行ning:

$ grunt git-add
$ grunt git-commit:"commit message"
$ grunt git-push:origin:"branch name"

我可以成功提交并推送我的更改。那么,为什么将这 3 个任务组合成它们自己的任务时,只有第一个任务 (git-add) 得到 运行?

var target = grunt.option('target');

grunt.registerTask('push-feature', [
  'git-add',
  'git-commit:' + target,
  'git-push:origin:feature/' + target
]);

假设我的分支名为 12345,我应该能够 运行 $ grunt push-feature --target=12345 完成所有这 3 个任务 运行,但只有第一个 git-添加任务 运行s。如果我删除 git-add 任务,下一个 (git-commit) 是唯一执行的任务。

我错过了什么才能按顺序完成这 3 个任务 运行?

这可能是因为异步问题。

尝试在声明任务时将它们标记为异步,并使用 spawn 的回调选项。这是您的第一个任务的示例:

grunt.registerTask('git-add', function () {

    var done = this.async(); // Set the task as async.

    grunt.util.spawn({
        cmd: 'git',
        args: ['add', '.']   // See comment below about this line
    }, done);                // Add done as the second argument here.
});

另请注意,您有一个额外的逗号,可能会干扰操作:

args: ['add', '.'], // <- this comma should be dropped.