最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js and Jake - How to call system commands synchronously within a task? - Stack Overflow

programmeradmin7浏览0评论

A Jake task executes a long-running system mand. Another task depends on the first task being pletely finished before starting. The 'exec' function of 'child_process' executes system mands asynchronously, making it possible to for the second task to begin before the first task is plete.

What's the cleanest way to write the Jakefile to ensure that the long-running system mand in the first task finishes before the second task starts?

I've thought about using polling in a dummy loop at the end of the first task, but this just smells bad. It seems like there must be a better way. I've seen this SO question, but it doesn't quite address my question.

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

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

desc('second task');
task('second', ['first'], function(params) {
  // do something dependent on the pletion of 'first' task
});

A Jake task executes a long-running system mand. Another task depends on the first task being pletely finished before starting. The 'exec' function of 'child_process' executes system mands asynchronously, making it possible to for the second task to begin before the first task is plete.

What's the cleanest way to write the Jakefile to ensure that the long-running system mand in the first task finishes before the second task starts?

I've thought about using polling in a dummy loop at the end of the first task, but this just smells bad. It seems like there must be a better way. I've seen this SO question, but it doesn't quite address my question.

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

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

desc('second task');
task('second', ['first'], function(params) {
  // do something dependent on the pletion of 'first' task
});
Share Improve this question edited May 23, 2017 at 11:47 CommunityBot 11 silver badge asked Jul 20, 2011 at 0:50 Rich ApodacaRich Apodaca 29k18 gold badges107 silver badges133 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

I found the answer to my own question by re-rereading Matthew Eernisse's post. For those wondering how to do it:

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

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

desc('second task');
task('second', ['first'], function(params) {
  // do something dependent on the pletion of 'first' task
});

Just for future reference, I have a synchronous exec module with no other dependencies.

  • https://npmjs/package/allsync

Example:

var allsync = require("allsync");
allsync.exec( "find /", function(data){
    process.stdout.write(data);
});
console.log("Done!");

In the above exampale, Done is only printed after the find process exists. The exec function essentially blocks until plete.

发布评论

评论列表(0)

  1. 暂无评论