roc
Version:
Build modern web applications easily
50 lines (43 loc) • 1.55 kB
JavaScript
;
exports.__esModule = true;
exports.execute = execute;
require('source-map-support/register');
var _child_process = require('child_process');
/**
* Executes a command string.
*
* Quite simple in its current state and should be expected to change in the future.
* Can manage multiple commands if they are divided by either & or &&. Important that there is spacing on both sides.
*
* @param {string} command - A command string that should run.
* @returns {Promise} - A promise that is resolved when all the commands are completed.
*/
function execute(command) {
var parallelCommands = command.split(/ & /);
return executeNext(parallelCommands);
}
function executeNext(parallelCommands) {
var syncCommand = parallelCommands.shift();
if (syncCommand) {
return Promise.all([runCommand(syncCommand), executeNext(parallelCommands)]);
}
return Promise.resolve();
}
function runCommand(syncCommand) {
var syncCommands = syncCommand.split(/ && /);
var command = syncCommands.shift();
var parts = command.split(/\s+/g);
var cmd = parts[0];
var args = parts.slice(1);
return new Promise(function (resolve) {
_child_process.spawn(cmd, args, { stdio: 'inherit' }).on('exit', function (code) {
if (code) {
/* eslint-disable no-process-exit */
return process.exit(code);
/* eslint-enable */
}
executeNext(syncCommands).then(resolve);
});
});
}
//# sourceMappingURL=execute.js.map