Skip to content

Commit

Permalink
Stream output from jake.exec, handle errors better.
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Feb 6, 2012
1 parent 8afc8ad commit dd01cf1
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 35 deletions.
11 changes: 10 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,21 @@ var api = new (function () {
};

this.fail = function (err, code) {
var msg
, errObj;
if (code) {
jake.errorCode = code;
}
if (err) {
if (typeof err == 'string') {
throw new Error(err);
// Use the initial or only line of the error as the error-message
// If there was a multi-line error, use the rest as the stack
msg = err.split('/n');
errObj = new Error(msg.shift());
if (msg.length) {
errObj.stack = msg.join('\n');
}
throw errObj;
}
else if (err instanceof Error) {
throw err;
Expand Down
107 changes: 73 additions & 34 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,48 @@


var exec = require('child_process').exec
, spawn = require('child_process').spawn
, utils;

utils = new (function () {
var _mix = function (targ, src, merge, includeProto) {
for (var p in src) {
// Don't copy stuff from the prototype
if (src.hasOwnProperty(p) || includeProto) {
if (merge &&
// Assumes the source property is an Object you can
// actually recurse down into
(typeof src[p] == 'object') &&
(src[p] !== null) &&
!(src[p] instanceof Array)) {
// Create the source property if it doesn't exist
// TODO: What if it's something weird like a String or Number?
if (typeof targ[p] == 'undefined') {
targ[p] = {};
}
_mix(targ[p], src[p], merge, includeProto); // Recurse
}
// If it's not a merge-copy, just set and forget
else {
targ[p] = src[p];
}
}
}
}

, _trim = function (s) {
var str = s || '';
return str.replace(/^\s*|\s*$/g, '');
}

, _truncate = function (s) {
var str = s || '';
if (str != '\n') {
str = str.replace(/\n$/, '')
}
return str;
};


this.exec = function (arr, callback, opts) {
var options = opts || {}
Expand All @@ -32,19 +71,44 @@ utils = new (function () {
, run;

run = function () {
var next = list.shift();
var sh
, cmd
, args
, next = list.shift()
, errData = '';
// Keep running as long as there are commands in the array
if (next) {
exec(next, function (err, stdout, stderr) {
if (err && breakOnError) {
throw err;
// Split the command-name from the args passed to it
args = next.split(' ');
cmd = args.shift();
// Spawn a child-process, set up output
sh = spawn(cmd, args);
// Out
if (stdout) {
sh.stdout.on('data', function (data) {
console.log(_truncate(data.toString()));
});
}
// Err
sh.stderr.on('data', function (data) {
var d = data.toString();
if (stderr) {
console.log(_truncate(d));
}
if (stderr && options.stderr) {
console.log('Error: ' + stderr);
// Accumulate the error-data so we can use it as the
// stack if the process exits with an error
errData += d;
});
// Exit, handle err or run next
sh.on('exit', function (code) {
var msg = errData || 'Process exited with error.';
msg = _trim(msg);
if (breakOnError && code != 0) {
fail(msg, code);
}
if (stdout && options.stdout) {
console.log(stdout);
else {
run();
}
run();
});
}
else {
Expand All @@ -57,31 +121,6 @@ utils = new (function () {
run();
};

var _mix = function (targ, src, merge, includeProto) {
for (var p in src) {
// Don't copy stuff from the prototype
if (src.hasOwnProperty(p) || includeProto) {
if (merge &&
// Assumes the source property is an Object you can
// actually recurse down into
(typeof src[p] == 'object') &&
(src[p] !== null) &&
!(src[p] instanceof Array)) {
// Create the source property if it doesn't exist
// TODO: What if it's something weird like a String or Number?
if (typeof targ[p] == 'undefined') {
targ[p] = {};
}
_mix(targ[p], src[p], merge, includeProto); // Recurse
}
// If it's not a merge-copy, just set and forget
else {
targ[p] = src[p];
}
}
}
};

this.objectToString = function (object) {
var objectArray = [];
for (var key in object) {
Expand Down

0 comments on commit dd01cf1

Please sign in to comment.