Javascript 在命令运行时以Grunt显示输出?
在这里,我没有得到任何输出,命令将永远运行:Javascript 在命令运行时以Grunt显示输出?,javascript,node.js,asynchronous,gruntjs,spawn,Javascript,Node.js,Asynchronous,Gruntjs,Spawn,在这里,我没有得到任何输出,命令将永远运行: grunt.registerTask('serve', function () { var done = this.async(); grunt.util.spawn({ cmd: 'jekyll', args: ['serve'], stdio: 'inherit' }, function (err, res, exit_code) { res.stdout
grunt.registerTask('serve', function () {
var done = this.async();
grunt.util.spawn({
cmd: 'jekyll',
args: ['serve'],
stdio: 'inherit'
}, function (err, res, exit_code) {
res.stdout && console.info(res.stdout);
exit_code && console.error(res.stderr);
done(err);
});
});
我希望输出(以及命令运行直到出错或中断)。尝试以下操作:
grunt.registerTask('serve', function(){
var done = this.async();
var child = grunt.util.spawn({
cmd: 'jekyll',
args: ['serve']
}, function(){
...
});
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
});
请参阅:您可以使用
以下功能可用
grunt.log.write(msg)
grunt.log.writeln([msg])
grunt.log.error([msg])
grunt.log.errorlns(msg)
grunt.log.ok([msg])
grunt.log.oklns(msg)
grunt.log.subhead(msg)
grunt.log.writeflags(obj, prefix)
grunt.log.debug(msg)
谢谢你的回答,@Lihau Tan很接近正确的答案 将这个答案与我的尝试结合起来,我提出了这个解决方案:
grunt.registerTask('serve', function () {
var child = grunt.util.spawn({
cmd: 'jekyll',
args: ['serve', '-P', process.env.PORT || 4001],
stdio: 'inherit'
}, this.async());
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
});