Bones/node_modules/concurrently/test/utils.js
SOUTHERNCO\x2mjbyrn 7efe7605b8 Template Upload
2017-05-17 13:45:25 -04:00

44 lines
1.1 KiB
JavaScript

var childProcess = require('child_process');
var _ = require('lodash');
var Promise = require('bluebird');
var shellQuote = require('shell-quote');
function run(cmd, opts) {
opts = _.merge({
pipe: true,
cwd: undefined,
callback: function(child) {
// Since we return promise, we need to provide
// this callback if one wants to access the child
// process reference
// Called immediately after successful child process
// spawn
}
}, opts);
var child;
var parts = shellQuote.parse(cmd);
try {
child = childProcess.spawn(_.head(parts), _.tail(parts), {
cwd: opts.cwd,
stdio: opts.pipe ? "inherit" : null
});
} catch (e) {
return Promise.reject(e);
}
opts.callback(child);
return new Promise(function(resolve, reject) {
child.on('error', function(err) {
reject(err);
});
child.on('close', function(exitCode) {
resolve(exitCode);
});
});
}
module.exports = {
run: run
};