我有一个grunt任务,我想从中运行一个node命令。该命令在运行时没有出现任何错误,但是我期待从该任务获得一些控制台输出,我似乎根本没有这样做。
为了运行此节点任务我缺少什么?
grunt.registerTask('asyncfoo', 'My "asyncfoo" task.', function() {
// Force task into async mode and grab a handle to the "done" function.
var done = this.async();
// Run some sync stuff.
grunt.log.writeln('Processing task...');
grunt.util.spawn({ cmd: 'node', args: ['S3ListBuckets.js']});
// And some async stuff.
setTimeout(function() {
grunt.log.writeln('All done!');
done();
}, 1000);
});
! - 如果其他人想要做类似的事情就是代码
module.exports = function(grunt) {
grunt.registerTask('asyncfoo', 'My "asyncfoo" task.', function() {
// Force task into async mode and grab a handle to the "done" function.
var done = this.async();
// Run some sync stuff.
grunt.log.writeln('Processing task...');
grunt.util.spawn({ cmd: 'node', args: ['S3ListBuckets.js'], opts: {stdio: 'inherit'}});
});
};
! - 列表桶
var fs = require('fs');
var aws = require('aws-sdk');
aws.config.loadFromPath('./grunt-aws.json');
var s3 = new aws.S3();
s3.listBuckets(function (err, data) {
if (err) {
console.log("Error:", err);
}
else {
for (var index in data.Buckets) {
var bucket = data.Buckets[index];
console.log("Bucket: ", bucket.Name, ' : ', bucket.CreationDate);
}
}
});
答案 0 :(得分:1)
答案https://stackoverflow.com/a/15045126/519995建议使用参数opts: {stdio: 'inherit'}
将生成的输出流式传输到父输出流。
同样的答案还列出了其他选择:收听数据事件,或根据需要管道流。
此外,使用超时等待异步任务不是一个好主意。如果您正在等待的是生成的进程,则可以使用回调来了解其完成时间。如果你有更复杂的同步,我建议你开始一个新的StackOverflow问题。