提前谢谢你看看这个.
我在一个无效的循环中有一个异步任务.我已经确定:
这个功能输出......什么都没有!?由于某种原因,grunt.log.writeln语句甚至没有触发.任务完成且没有错误.我还尝试添加一个20秒的延迟,以防脚本在异步任务返回之前完成.奇怪的是,如果我不调用"完成(错误)",文件将被写入文件(当我用grunt.file.write语句替换writeln时).
var done = this.async(),
exec = require('child_process').exec,
myJSON = {
"file1" : "C:/home/me/jquery.js",
"file2 " : "C:/home/me/grunt.js",
...
"fileN" : "C:/home/me/N.js"
},
count;
for (var key in myJSON) {
if (myJSON.hasOwnProperty(key)) {
(function (key) {
exec( 'type "' + myJSON[key] + '"',
function(error, stdout, stderr) {
if (error) {
grunt.log.writeln('!!! exec error: ' + error);
}
grunt.log.writeln('stdout: ' + stdout);
grunt.log.writeln('stderr: ' + stderr);
done(error);
}
);
})(key);
count++;
}
} …Run Code Online (Sandbox Code Playgroud) 您是否可以帮助以下使用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);
}
}
);
Run Code Online (Sandbox Code Playgroud)
参考文献:
http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options