Permalink
Browse files

Cleaner exec iteration.

  • Loading branch information...
1 parent a7a27b3 commit d3fa7a6bf2123fa9bc18995b464a593305baa5a2 @mde mde committed Dec 5, 2011
Showing with 24 additions and 21 deletions.
  1. +24 −21 lib/utils.js
View
@@ -22,34 +22,37 @@ var exec = require('child_process').exec
this.exec = function (arr, callback, opts) {
var options = opts || {}
+ , list = arr.slice()
, stdout = options.stdout
, stderr = options.stderr
, breakOnError = typeof options.breakOnError != 'undefined' ?
options.breakOnError : true
- var run = function (cmd) {
- exec(cmd, function (err, stdout, stderr) {
- var next;
- if (err && breakOnError) {
- this.fail('Error: ' + JSON.stringify(err));
- }
- if (stderr && options.stderr) {
- console.log('Error: ' + stderr);
- }
- if (stdout && options.stdout) {
- console.log(stdout);
- }
- next = arr.shift();
- if (next) {
- run(next);
- }
- else {
- if (callback) {
- callback();
+ , run;
+
+ run = function () {
+ var next = list.shift();
+ if (next) {
+ exec(next, function (err, stdout, stderr) {
+ if (err && breakOnError) {
+ this.fail('Error: ' + JSON.stringify(err));
+ }
+ if (stderr && options.stderr) {
+ console.log('Error: ' + stderr);
}
+ if (stdout && options.stdout) {
+ console.log(stdout);
+ }
+ run();
+ });
+ }
+ else {
+ if (callback) {
+ callback();
}
- });
+ }
};
- run(arr.shift());
+
+ run();
};
})();

0 comments on commit d3fa7a6

Please sign in to comment.