Skip to content

Commit

Permalink
Merge pull request #24 from jhen0409/patch-1
Browse files Browse the repository at this point in the history
Pipe stdout, stderr
  • Loading branch information
benoror committed Dec 10, 2015
2 parents 2dafaff + 2e15ab2 commit 6ba2de6
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions lib/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,25 @@ var objectAssign = require('object-assign');

module.exports = function exec(script) {

// Grab the command section of the entered command
var command = script.command.split(' ')[0];
// Anything else is options
var options = script.command.split(' ').slice(1);

var argv = process.argv.splice(3);

options = options.concat(argv);
var command = script.command + ' ' + argv.join(' ');

script.env = script.env || {};

var env = objectAssign(process.env, script.env, dotenv);

console.log('to be executed:', command, options.join(' '));
var command = spawn(command, options, {env: env});

command.stdout.on('data', function(data) {
process.stdout.write(data);
});

command.stderr.on('data', function(data) {
process.stderr.write(data);
});

command.on('error', function(err) {
process.stderr.write(err);
process.exit(err.code || 1);
var sh = 'sh', shFlag = '-c';
if (process.platform === 'win32') {
sh = 'cmd';
shFlag = '/c';
}

console.log('to be executed:', command);
spawn(sh, [shFlag, command], {
env: env,
stdio: ['pipe', process.stdout, process.stderr]
}).on('close', function(code) {
process.exit(code);
});

}

0 comments on commit 6ba2de6

Please sign in to comment.