2

我正在创建一个 grunt 插件,它广泛使用了该grunt.util.spawn功能(http://gruntjs.com/api/grunt.util#grunt.util.spawn)。我的插件的基本骨架如下:

'use strict';

module.exports = function (grunt) {

    var spawn = require("child_process").spawn;

    var createCommit = function (text) {
        var commit = grunt.util.spawn({
                    cmd: "git",
                    args: ["commit","-a","-m", text]
        }, function() {
                    console.log("FINISH! FINISH! FINISH!");
        });
    };

    grunt.registerMultiTask("myplugin", "Plugin to commit awesome things", function () {
        createCommit("0.2.0");
    });
};

然而,当我试图执行这个垃圾任务时,console.log("FINISH! FINISH! FINISH!");mi回调永远不会执行......有人可以帮助我吗?

4

1 回答 1

1

最后我在这个问题中找到了一个解决方案(等待异步咕噜任务完成)。解决这个问题的关键是使用this.async注册里面的函数。一个可能的解决方案可能是:

'use strict';

module.exports = function (grunt) {

    var spawn = require("child_process").spawn,
        donePromise;

    var createCommit = function (text) {
        var i = 0;
    var commit = grunt.util.spawn({
            cmd: "git",
            args: ["commit","-a","-m", text]
        }, function() {
            callback();
            donePromise();
        });
    };


   grunt.registerMultiTask("myplugin", "Plugin to commit awesome things", function () {
        donePromise = this.async();
        createCommit("0.3.0");
    });
};
于 2013-11-11T15:53:22.437 回答