Node.js和Jake - 如何在任务中同步调用系统命令?

Ric*_*aca 5 javascript node.js jake

Jake任务执行长时间运行的系统命令.另一项任务取决于在开始之前完成的第一项任务.'child_process'的'exec'功能异步执行系统命令,使得第二个任务可以在第一个任务完成之前开始.

编写Jakefile以确保第一个任务中长时间运行的系统命令在第二个任务启动之前完成的最简洁方法是什么?

我已经考虑过在第一个任务结束时在虚拟循环中使用轮询,但这只是闻起来很糟糕.似乎必须有更好的方法.我已经看到了这个问题,但它并没有完全解决我的问题.

var exec = require('child_process').exec;

desc('first task');
task('first', [], function(params) {
  exec('long running system command');
});

desc('second task');
task('second', ['first'], function(params) {
  // do something dependent on the completion of 'first' task
});
Run Code Online (Sandbox Code Playgroud)

Ric*_*aca 2

我通过重读Matthew Eernisse 的帖子找到了自己问题的答案。对于那些想知道如何做的人:

var exec = require('child_process').exec;

desc('first task');
task('first', [], function(params) {
  exec('long running system command', function() {
    complete();
  });
}, true); // this prevents task from exiting until complete() is called

desc('second task');
task('second', ['first'], function(params) {
  // do something dependent on the completion of 'first' task
});
Run Code Online (Sandbox Code Playgroud)