eisd
Version:
Execute your favorite command in SubDirectories. Use it like: `eisd '[command]' [subdirs...]` (Example: `eisd 'yarn build' client server scripts`)
68 lines (67 loc) • 2.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.startMaster = void 0;
const colors_1 = require("colors");
const sh = require("shelljs");
const node_fs = require("fs");
const cluster = require("cluster");
function startMaster(commandToExecute, directoriesToUse, allowFailures = false, aSynchronous = false, verbose = false) {
return new Promise(resolve => {
let directoriesToDo = directoriesToUse.length;
const directoriesSucces = [];
const directoriesFailed = [];
if (aSynchronous)
while (startWorker()) { }
else
startWorker();
cluster.on("message", (worker, message) => {
worker.kill();
directoriesToDo--;
message.result === "SUCCESS"
? directoriesSucces.push(message.directory)
: directoriesFailed.push(message.directory);
// No more directories or there is a failure (in synchronous mode) and we don't allow any? Resolve!
if (!directoriesToDo || (!aSynchronous && !allowFailures && directoriesFailed.length)) {
resolve({ directoriesSucces, directoriesFailed });
}
else {
startWorker();
}
});
});
/**
* Returns if there was a worker started
*/
function startWorker() {
return (!!directoriesToUse.length &&
cluster.fork({ commandToExecute, directory: directoriesToUse.shift(), verbose: verbose.toString() }));
}
}
exports.startMaster = startMaster;
function executeWork(commandToExecute, directory, verbose) {
const result = { directory, result: "SUCCESS" };
try {
node_fs.accessSync(directory, node_fs.constants.F_OK & node_fs.constants.R_OK);
}
catch (e) {
process.stdout.write(colors_1.red(`ERROR: Could not access path "${directory}"`));
result.result = "ERROR";
process.stdout.write("\n\n");
process.send(result);
process.exit(1);
}
sh.cd(directory);
console.log(colors_1.yellow(`Executing command "${commandToExecute}" in: ${directory}`));
const execOutput = sh.exec(commandToExecute);
result.result = execOutput.code === 0 ? "SUCCESS" : "ERROR";
if (verbose) {
result.result === "SUCCESS"
? process.stdout.write(colors_1.green(`Done with executing "${commandToExecute}" in ${directory}`))
: process.stdout.write(colors_1.red(`Done with errors after executing "${commandToExecute}" in ${directory}`));
process.stdout.write("\n\n");
}
process.send(result);
}
if (cluster.isWorker) {
executeWork(process.env.commandToExecute, process.env.directory, process.env.verbose === "true");
}