在命令运行时以Grunt显示输出

Show output in Grunt as command runs?

本文关键字:显示 输出 Grunt 命令 运行时      更新时间:2023-09-26

这里没有输出,命令永远运行:

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);
    });
});

我想要输出(并且命令一直运行到出现错误或中断)。

感谢您的回答,@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);
});

试试这个:

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派生的进程未捕获输出

您可以使用grunt log

以下功能可用

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)