Grunt任务+ Ruby脚本无法打印标准输出

即时尝试做简单的咕task任务,调用一个ruby脚本,但我不能从nodejs child_process.exec命令得到任何输出。 现在我试图只是得到“ls -la”命令的内容,但是当我运行我的任务时,我什么也得不到。

任务:

'use strict'; var which = require('which'), cp = require('child_process'), sys = require('sys'); module.exports = function (grunt) { var log = grunt.log; grunt.registerMultiTask('testtask', 'The best Grunt plugin ever.', function () { var command = 'ls -la'; // ruby script.rb var args = [ "test" ]; var childProcess = cp.exec(command, args, function() {}); log.write("Test log"); childProcess.stdout.on('data', function (d) { log.write(d); }); childProcess.stderr.on('data', function (d) { log.error(d); }); }); }; 

任务的Gruntconfiguration是愚蠢的:

 testtask: { options: { // Task-specific options go here. }, testtarget: { // Target-specific file lists and/or options go here. } } 

当我运行咕噜的任务,我得到这样一个输出:

 Running "hockeyapp:Android" (hockeyapp) task Test log Done, without errors. 

我想我应该看到“ls”命令的输出吗? 我错过了什么?


解:

问题是我没有使用async grunt任务机制。 基本上应该是这样的:

  grunt.registerMultiTask('testtask', 'The best Grunt plugin ever.', function () { var command = 'ls -la'; // ruby script.rb var args = [ "test" ]; var done = this.async(); cp.exec(command, args, function(error, stdout, stderr) { if(error === null) { grunt.log.write(stdout); done(); } else { grunt.log.error(stderr); done(false); } }); });