This commit is contained in:
Benjamin Toby 2024-11-03 10:57:44 +01:00
parent 714f50314f
commit f4364524ef
6 changed files with 483 additions and 406 deletions

2
.npmrc Normal file
View File

@ -0,0 +1,2 @@
registry=https://git.tben.me/api/packages/Moduletrace/npm/
//git.tben.me/api/packages/Moduletrace/npm/:_authToken=${GITBEN_NPM_TOKEN}

131
index.js
View File

@ -37,54 +37,78 @@ rl.on("line", (input) => {
*/ */
function readCommands(input) { function readCommands(input) {
if (input?.match(/^(reload|restart|reboot)$/i)) { if (input?.match(/^(reload|restart|reboot)$/i)) {
console.log(` - ${colors.FgBlue}Reloading processes ...${colors.Reset}`); console.log(
` - ${colors.FgBlue}Reloading processes ...${colors.Reset}`
);
restartAll(); restartAll();
} else if (input?.match(/^(reload|restart|reboot) \d/i)) { } else if (input?.match(/^(reload|restart|reboot) \d/i)) {
const processedIndexesString = input.split(" ")[1]; const processedIndexesString = input.split(" ")[1];
const processedIndexes = processedIndexesString ? processedIndexesString.split(",") : null; const processedIndexes = processedIndexesString
? processedIndexesString.split(",")
: null;
if (!processedIndexes?.length) { if (!processedIndexes?.length) {
console.log(` - ${colors.FgRed}Error:${colors.Reset} No processes to reload`); console.log(
` - ${colors.FgRed}Error:${colors.Reset} No processes to reload`
);
return; return;
} else { } else {
console.log(` - ${colors.FgBlue}Reloading processes ${processedIndexesString} ...${colors.Reset}`); console.log(
` - ${colors.FgBlue}Reloading processes ${processedIndexesString} ...${colors.Reset}`
);
processedIndexes.forEach((index) => { processedIndexes.forEach((index) => {
restartOne(index); restartOne(index);
}); });
console.log(` - ${colors.FgGreen}Processes Restarted Successfully ${processedIndexesString} ...${colors.Reset}`); console.log(
` - ${colors.FgGreen}Processes Restarted Successfully ${processedIndexesString} ...${colors.Reset}`
);
} }
} }
if (input?.match(/^kill$/i)) { if (input?.match(/^kill$/i)) {
console.log(` - ${colors.FgYellow}Killing processes ...${colors.Reset}`); console.log(
` - ${colors.FgYellow}Killing processes ...${colors.Reset}`
);
process.exit(); process.exit();
} else if (input?.match(/^kill \d/i)) { } else if (input?.match(/^kill \d/i)) {
const processedIndexesString = input.split(" ")[1]; const processedIndexesString = input.split(" ")[1];
const processedIndexes = processedIndexesString ? processedIndexesString.split(",") : null; const processedIndexes = processedIndexesString
? processedIndexesString.split(",")
: null;
if (!processedIndexes?.length) { if (!processedIndexes?.length) {
console.log(` - ${colors.FgRed}Error:${colors.Reset} No processes to reload`); console.log(
` - ${colors.FgRed}Error:${colors.Reset} No processes to reload`
);
return; return;
} else { } else {
console.log(` - ${colors.FgYellow}Killing processes ${processedIndexesString} ...${colors.Reset}`); console.log(
` - ${colors.FgYellow}Killing processes ${processedIndexesString} ...${colors.Reset}`
);
processedIndexes.forEach((index) => { processedIndexes.forEach((index) => {
killOne(index); killOne(index);
}); });
console.log(` - ${colors.FgGreen}Processes Killed ${processedIndexesString} ...${colors.Reset}`); console.log(
` - ${colors.FgGreen}Processes Killed ${processedIndexesString} ...${colors.Reset}`
);
} }
} }
} }
process.stdin.on("keypress", (character, key) => { process.stdin.on("keypress", (character, key) => {
if (key.ctrl && key.name === "r") { if (key.ctrl && key.name === "r") {
console.log(` - ${colors.FgBlue}Reloading processes ...${colors.Reset}`); console.log(
` - ${colors.FgBlue}Reloading processes ...${colors.Reset}`
);
restartAll(); restartAll();
} }
}); });
process.on("exit", (code) => { process.on("exit", (code) => {
console.log(` - ${colors.FgBlue}Process exited with code ${code}${colors.Reset}`); console.log(
` - ${colors.FgBlue}Process exited with code ${code}${colors.Reset}`
);
rl.close(); rl.close();
}); });
@ -97,7 +121,9 @@ const argvProcessList = process.argv.at(-1);
const processesFilePath = path.resolve(process.cwd(), "batchrun.config.json"); const processesFilePath = path.resolve(process.cwd(), "batchrun.config.json");
if (argvProcessList?.match(/batchrun/i) && !fs.existsSync(processesFilePath)) { if (argvProcessList?.match(/batchrun/i) && !fs.existsSync(processesFilePath)) {
console.error(` - ${colors.FgRed}Error:${colors.Reset} No arguments to run`); console.error(
` - ${colors.FgRed}Error:${colors.Reset} No arguments to run`
);
process.exit(1); process.exit(1);
} }
@ -117,12 +143,16 @@ if (fs.existsSync(processesFilePath)) {
processesStrings.push(strippedProcessString); processesStrings.push(strippedProcessString);
} }
} else { } else {
console.error(` - ${colors.FgRed}Error:${colors.Reset} No arguments to run or \`batchrun.config.json\` file present`); console.error(
` - ${colors.FgRed}Error:${colors.Reset} No arguments to run or \`batchrun.config.json\` file present`
);
process.exit(1); process.exit(1);
} }
if (!processesStrings?.[0]) { if (!processesStrings?.[0]) {
console.error(` - ${colors.FgRed}Error:${colors.Reset} No processes to run`); console.error(
` - ${colors.FgRed}Error:${colors.Reset} No processes to run`
);
process.exit(1); process.exit(1);
} }
@ -131,6 +161,7 @@ const spawnOptions = {
cwd: process.cwd(), cwd: process.cwd(),
shell: process.platform.match(/win/i) ? "bash.exe" : undefined, shell: process.platform.match(/win/i) ? "bash.exe" : undefined,
stdio: ["pipe", "inherit", "inherit"], stdio: ["pipe", "inherit", "inherit"],
detached: false,
}; };
/** /**
@ -142,39 +173,23 @@ function startProcesses() {
const processStringArray = processString.split(" "); const processStringArray = processString.split(" ");
const targetProcess = processStringArray.shift(); const targetProcess = processStringArray.shift();
if (targetProcess) { if (targetProcess) {
const childProcess = spawn(targetProcess, processStringArray, spawnOptions); const childProcess = spawn(
targetProcess,
processStringArray,
spawnOptions
);
if (childProcess) { if (childProcess) {
processes.push(childProcess); processes.push(childProcess);
// childProcess.stdin?.on("keypress", (character, key) => {
// if (key.ctrl && key.name === "r") {
// console.log(` - ${colors.FgBlue}Reloading processes ...${colors.Reset}`);
// restartAll();
// }
// });
// childProcess.on("error", (error) => {
// console.log(` - ${colors.FgRed}Error:${colors.Reset} ${error.message}`);
// killOne(i.toString());
// });
// childProcess.on("exit", (code, signal) => {
// console.log(` - ${colors.FgRed}Error:${colors.Reset} Process ${i} exited with code ${code} and signal ${signal}`);
// killOne(i.toString());
// });
// childProcess.on("message", (code, signal) => {
// console.log(` - ${colors.FgRed}Error:${colors.Reset} Process ${i} exited with code ${code} and signal ${signal}`);
// killOne(i.toString());
// });
// childProcess.stdin?.pipe(process.stdin);
} else { } else {
console.error(` - ${colors.FgRed}Error:${colors.Reset} Failed to start process ${i}`); console.error(
` - ${colors.FgRed}Error:${colors.Reset} Failed to start process ${i}`
);
process.exit(1); process.exit(1);
} }
} else { } else {
console.error(` - ${colors.FgRed}Error:${colors.Reset} A target process is not defined in \`${processString}\``); console.error(
` - ${colors.FgRed}Error:${colors.Reset} A target process is not defined in \`${processString}\``
);
process.exit(1); process.exit(1);
} }
} }
@ -192,12 +207,16 @@ function restartAll() {
childProcess.kill(); childProcess.kill();
// processes.splice(i, 1); // processes.splice(i, 1);
} catch (error) { } catch (error) {
console.log(` - ${colors.FgRed}Error:${colors.Reset} Failed to kill process ${childProcess.pid}`); console.log(
` - ${colors.FgRed}Error:${colors.Reset} Failed to kill process ${childProcess.pid}`
);
process.exit(); process.exit();
} }
} }
console.log(` - ${colors.FgGreen}Restarted ${processes.length} processes${colors.Reset}`); console.log(
` - ${colors.FgGreen}Restarted ${processes.length} processes${colors.Reset}`
);
processes = []; processes = [];
@ -217,14 +236,20 @@ function restartOne(index) {
if (childProcess.pid) killProcessForce(childProcess.pid); if (childProcess.pid) killProcessForce(childProcess.pid);
childProcess.kill(); childProcess.kill();
} catch (error) { } catch (error) {
console.log(` - ${colors.FgRed}Error:${colors.Reset} Failed to kill process ${childProcess.pid}`); console.log(
` - ${colors.FgRed}Error:${colors.Reset} Failed to kill process ${childProcess.pid}`
);
process.exit(); process.exit();
} }
const processString = processesStrings[index]; const processString = processesStrings[index];
const processStringArray = processString.split(" "); const processStringArray = processString.split(" ");
const targetProcess = processStringArray.shift(); const targetProcess = processStringArray.shift();
const newChildProcess = spawn(targetProcess, processStringArray, spawnOptions); const newChildProcess = spawn(
targetProcess,
processStringArray,
spawnOptions
);
processes.splice(parseInt(index), 1, newChildProcess); processes.splice(parseInt(index), 1, newChildProcess);
} }
@ -239,7 +264,9 @@ function killOne(index) {
try { try {
} catch (error) { } catch (error) {
console.log(` - ${colors.FgRed}Error:${colors.Reset} Failed to kill process ${childProcess.pid}`); console.log(
` - ${colors.FgRed}Error:${colors.Reset} Failed to kill process ${childProcess.pid}`
);
process.exit(); process.exit();
} }
} }
@ -261,5 +288,13 @@ function killProcessForce(pid) {
} catch (error) {} } catch (error) {}
} }
console.log(` - ${colors.FgGreen}Started ${processesStrings.length} processes${colors.Reset}`); console.log(
` - ${colors.FgGreen}Started ${processesStrings.length} processes${colors.Reset}`
);
startProcesses(); startProcesses();
setInterval(() => {
console.log(
`Turbo Sync Running for ${process.uptime().toLocaleString()}s ...`
);
}, 60000);

37
package-lock.json generated Normal file
View File

@ -0,0 +1,37 @@
{
"name": "batchrun",
"version": "1.0.7",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "batchrun",
"version": "1.0.7",
"license": "MIT",
"bin": {
"batch-run": "index.js",
"batchrun": "index.js"
},
"devDependencies": {
"@types/node": "^22.8.7"
}
},
"node_modules/@types/node": {
"version": "22.8.7",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.8.7.tgz",
"integrity": "sha512-LidcG+2UeYIWcMuMUpBKOnryBWG/rnmOHQR5apjn8myTQcx3rinFRn7DcIFhMnS0PPFSC6OafdIKEad0lj6U0Q==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": "~6.19.8"
}
},
"node_modules/undici-types": {
"version": "6.19.8",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz",
"integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==",
"dev": true,
"license": "MIT"
}
}
}

View File

@ -21,5 +21,8 @@
"monitor" "monitor"
], ],
"author": "Benjamin Toby", "author": "Benjamin Toby",
"license": "MIT" "license": "MIT",
"devDependencies": {
"@types/node": "^22.8.7"
}
} }