Skip to content

Commit

Permalink
Do not store entire stdout in memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
nwronski committed Oct 27, 2015
1 parent 4ca8c82 commit 52bf268
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/child-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,42 @@ module.exports = function childPromise(cmd, args, file, dir, mute) {
var childErrMsg = '[Child Error] ' + file + "\n\n";
var child;

if (!mute) {
say('[Output: ' + cmd + '] ' + file + "\n", 'debug');
}

child = environment.windows ?
// Use exec() and special escape syntax for Windows
exec(windowsCommand(cmd, args), opts) :
// Use spawn() and normal syntax for non-Windows
spawn(cmd, args, opts);

function dataHandler(data) {
data = data.toString();
buff += data;
var dataHandler = function dataHandler(data) {
data = data.toString().trim();
// Only keep the last packet of data to prevent large
// amount of output being saved in memory
buff = !/\n/.test(data) ? buff + data : data;
if (!mute) {
console.log(data.trim() + "\n");
console.log(data + "\n");
}
}

function stdErrHandler(data) {
var stdErrHandler = function stdErrHandler(data) {
errs += data.toString();
}

function errHandler(err) {
var errHandler = function errHandler(err) {
rej(new Error(childErrMsg + err.toString().trim() + "\n"));
}

function closeHandler(code) {
var closeHandler = function closeHandler(code) {
if (code !== 0) {
errHandler(errs);
} else {
acc(buff);
}
}

if (!mute) {
say('[Output: ' + cmd + '] ' + file + "\n", 'debug');
}

child = environment.windows ?
// Use exec() and special escape syntax for Windows
exec(windowsCommand(cmd, args), opts) :
// Use spawn() and normal syntax for non-Windows
spawn(cmd, args, opts);

child.stdout.on('data', dataHandler);
child.stderr.on('data', stdErrHandler);
child.on('close', closeHandler);
Expand Down

0 comments on commit 52bf268

Please sign in to comment.