在Windows環境中的grunt構建過程中,我想調用兩個ant命令作爲產生進程以便能夠跟蹤輸出。Grunt/NodeJs - 在Windows環境中調用ant命令作爲產卵進程
,過程大概如下:
grunt.registerTask('app-clean', 'Cleaning tasks', function() {
var done = this.async();
if (context.device.platform === 'android' || context.device.platform === 'all') {
spawnProcess('ant', ['clean'], {
cwd: path.resolve(context.android)
}, done);
}
});
grunt.registerTask('app-build', 'Building tasks', function() {
var done = this.async();
if (context.device.platform === 'android' || context.device.platform === 'all') {
spawnProcess('ant', ['release'], {
cwd: path.resolve(context.android)
}, done);
}
});
// Helper function to execute and log out child process
// TODO: implement a better success/error callback
var spawnProcess = function(command, args, options, callback) {
var process = spawn(command, args, options),
err = false;
process.stdout.on('data', function(data) {
grunt.log.write(data);
});
process.stderr.on('data', function(data) {
err = true;
grunt.log.errorlns(data);
});
if (typeof callback === 'function') {
process.on('exit', function() {
if (!err) {
return callback();
}
});
}
};
使用這些任務的grunt build
然後通過COMAND行一步團隊市觸發。
這兩個命令都返回Fatal error: spawn ENOENT
。
它似乎工作使用exec而不是產卵,但我不知道哪一個將是理想的。 'exec'''沒有環境區分,並且輸出如預期。我會在幾個版本中更新我的問題。 – svassr
exec和spawn之間唯一重要的區別就是它們返回的內容。 'spawn'會給你一個流,其中'exec'給你一個緩衝區。 '致命錯誤:產生ENOENT'意味着你試圖運行的命令不存在,並且幾乎總是在Windows上,假設命令確實存在,這意味着它需要擴展。 –