您能帮忙使用grunt运行以下 Node exec命令的示例吗?
echo命令正在执行,并且hello-world.txt已创建,但是回调函数中的grunt.log.writeln命令未触发。

var exec = require('child_process').exec,
    child;

    child = exec('echo hello, world! > hello-world.txt',
        function(error, stdout, stderr){
            grunt.log.writeln('stdout: ' + stdout);
            grunt.log.writeln('stderr: ' + stderr);
            if (error !== null) {
                grunt.log.writeln('exec error: ' + error);
          }
        }
    );

引用:

http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Retrieving a value from a node child process

最佳答案

DOH!这是在常见问题解答中。

当使用Gruntjs执行异步任务时,必须手动指定任务完成的时间。
https://github.com/gruntjs/grunt/wiki/Frequently-Asked-Questions
https://github.com/robdodson/async-grunt-tasks
https://github.com/rwldrn/dmv/blob/master/node_modules/grunt/docs/api_task.md

为了后代,上面的内容应如下所示:

var exec = require('child_process').exec,
    child,
    done = grunt.task.current.async(); // Tells Grunt that an async task is complete

child = exec('echo hello, world! > hello-world.txt',
    function(error, stdout, stderr){
        grunt.log.writeln('stdout: ' + stdout);
        grunt.log.writeln('stderr: ' + stderr);
        done(error); // Technique recommended on #grunt IRC channel. Tell Grunt asych function is finished. Pass error for logging; if operation completes successfully error will be null

      }
    }
);

关于node.js - nodejs grunt子进程回调函数示例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13957303/

10-16 21:15