Skip to content

Commit

Permalink
Merge 93e1cd4 into e422160
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed Feb 13, 2018
2 parents e422160 + 93e1cd4 commit 47b93fb
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 34 deletions.
64 changes: 60 additions & 4 deletions lib/bagofcli.js
Expand Up @@ -188,7 +188,10 @@ function _postCommand(args, commandsConfig, globalOptsConfig) {

/**
* Execute a one-liner command.
* Both stderr and stdout will be logged via console.error/log accordingly.
*
* The output emitted on stderr and stdout of the child process will be written to process.stdout
* and process.stderr of this process.
*
* Fallthrough is handy in situation where there are multiple execs running in sequence/parallel,
* and they all have to be executed regardless of success/error on either one of them.
*
Expand All @@ -197,6 +200,50 @@ function _postCommand(args, commandsConfig, globalOptsConfig) {
* @param {Function} cb: standard cb(err, result) callback
*/
function exec(command, fallthrough, cb) {
execute(command, fallthrough, false, function(err, stdOutOuput, stdErrOuput, result) {
// drop stdOutOuput and stdErrOuput parameters to keep exec backwards compatible.
cb(err, result);
});
}

/**
* Execute a one-liner command and collect the output.
*
* The output emitted on stderr and stdout of the child process will be
* collected and passed on to the given callback.
*
* Fallthrough is handy in situation where there are multiple execs running in sequence/parallel,
* and they all have to be executed regardless of success/error on either one of them.
*
* @param {String} command: command to execute
* @param {Boolean} fallthrough: allow error to be camouflaged as a non-error
* @param {Function} cb: (err, stdOutOuput, stdErrOuput, result) callback
*/
function execAndCollect(command, fallthrough, cb) {
execute(command, fallthrough, true, cb);
}

// not exported
/**
* Execute a one-liner command.
*
* The output emitted on stderr and stdout of the child process will either be written to
* process.stdout and process.stderr of this process or collected and passed on to the
* given callback, depending on collectOutput.
*
* Fallthrough is handy in situation where there are multiple execs running in sequence/parallel,
* and they all have to be executed regardless of success/error on either one of them.
*
* @param {String} command: command to execute
* @param {Boolean} fallthrough: allow error to be camouflaged as a non-error
* @param {Boolean} collectOutput: pass the output of the child process to the callback instead
* of writing it to error to be camouflaged as a non-error
* @param {Function} cb: (err, stdOutOuput, stdErrOuput, result) callback
* @private
*/
function execute(command, fallthrough, collectOutput, cb) {
var collectedStdOut = '';
var collectedStdErr = '';

var _exec = child.exec(command, function (err) {
var result;
Expand All @@ -205,15 +252,23 @@ function exec(command, fallthrough, cb) {
result = err;
err = null;
}
cb(err, result);
cb(err, collectedStdOut, collectedStdErr, result);
});

_exec.stdout.on('data', function (data) {
process.stdout.write(colors.green(data.toString()));
if (collectOutput) {
collectedStdOut += data.toString().trim();
} else {
process.stdout.write(colors.green(data.toString()));
}
});

_exec.stderr.on('data', function (data) {
process.stderr.write(colors.red(data.toString()));
if (collectOutput) {
collectedStdErr += data.toString().trim();
} else {
process.stderr.write(colors.red(data.toString()));
}
});
}

Expand Down Expand Up @@ -461,6 +516,7 @@ exports.command = command;
exports._preCommand = _preCommand;
exports._postCommand = _postCommand;
exports.exec = exec;
exports.execAndCollect = execAndCollect;
exports.exit = exit;
exports.exitCb = exitCb;
exports.files = files;
Expand Down

0 comments on commit 47b93fb

Please sign in to comment.