roc
Version:
Build modern web applications easily
157 lines (125 loc) • 5.7 kB
JavaScript
;
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.execute = execute;
exports.executeSync = executeSync;
var _path = require('path');
var _crossSpawn = require('cross-spawn');
/**
* 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) {
const parallelCommands = command.split(/ & /);
return executeParallel(parallelCommands);
}
function executeParallel(parallelCommands) {
let path = arguments.length <= 1 || arguments[1] === undefined ? process.cwd() : arguments[1];
const syncCommand = parallelCommands.shift();
if (syncCommand) {
return Promise.all([executeCommand(syncCommand, path), executeParallel(parallelCommands, path)]);
}
return Promise.resolve();
}
function executeCommand(syncCommand) {
let path = arguments.length <= 1 || arguments[1] === undefined ? process.cwd() : arguments[1];
const syncCommands = syncCommand.split(/ && /);
return runCommand(syncCommands, path);
}
function runCommand(syncCommands) {
let path = arguments.length <= 1 || arguments[1] === undefined ? process.cwd() : arguments[1];
const command = syncCommands.shift();
if (command) {
const parts = getParts(command);
const cmd = parts[0];
const args = parts.slice(1);
// If the command is to change directory we will carry the path over to the next command.
if (cmd === 'cd') {
// If the path is absolute, starts with a /, we will not join in with the previous
const newPath = args[0].charAt(0) === '/' ? args[0] : (0, _path.join)(path, args[0]);
return runCommand(syncCommands, newPath);
}
return new Promise((resolve, reject) => {
(0, _crossSpawn.spawn)(cmd, args, { stdio: 'inherit', cwd: path }).on('exit', code => {
if (code !== 0) {
return reject(code);
}
return runCommand(syncCommands, path).then(resolve).catch(reject);
});
});
}
return Promise.resolve();
}
/**
* Executes a command string in a synchronous manner.
*
* 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.
*/
function executeSync(command) {
// Will run them in parallel anyway, nothing we can do about it currently
const parallelCommands = command.split(/ & /);
parallelCommands.forEach(syncCommand => {
const syncCommands = syncCommand.split(/ && /);
const status = runCommandSync(syncCommands);
if (status) {
/* eslint-disable no-process-exit */
process.exit(status);
/* eslint-enable */
}
});
}
function runCommandSync(syncCommands) {
let path = arguments.length <= 1 || arguments[1] === undefined ? process.cwd() : arguments[1];
const command = syncCommands.shift();
if (command) {
const parts = getParts(command);
const cmd = parts[0];
const args = parts.slice(1);
// If the command is to change directory we will carry the path over to the next command.
if (cmd === 'cd') {
// If the path is absolute, starts with a /, we will not join in with the previous
const newPath = args[0].charAt(0) === '/' ? args[0] : (0, _path.join)(path, args[0]);
return runCommandSync(syncCommands, newPath);
}
var _sync = (0, _crossSpawn.sync)(cmd, args, { stdio: 'inherit', cwd: path });
const status = _sync.status;
if (status) {
return status;
}
return runCommandSync(syncCommands, path);
}
}
/**
* Will take a command string an return an array with the different parts of the command.
*
* Will manage command parts that has been grouped with " or '.
*
* @param {string} commandString - The string to split up into parts.
*
* @returns {string[]} - An array with the different parts of the command.
*/
function getParts(commandString) {
const regex = /[^\s"']+|"([^"]*)"|'([^']*)'/g;
const parts = [];
let match;
while (match = regex.exec(commandString)) {
var _match = match;
var _match2 = _slicedToArray(_match, 3);
const generalMatch = _match2[0];
const doubleQuotationMatch = _match2[1];
const singleQuotationMatch = _match2[2];
parts.push(doubleQuotationMatch || singleQuotationMatch || generalMatch);
}
return parts;
}
//# sourceMappingURL=execute.js.map