This commit is contained in:
Benjamin Toby 2024-12-04 07:57:15 +01:00
parent 5c27f2c653
commit 6d9121ef66
3 changed files with 81 additions and 12 deletions

4
.npmrc
View File

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

View File

@ -11,6 +11,25 @@ const colors = require("./console-colors");
////////////////////////////////////////////
////////////////////////////////////////////
setInterval(() => {
console.log(
`Batchrun Running for ${process.uptime().toLocaleString()}s ...`
);
}, 60000);
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
if (process.argv[process.argv.length - 1]?.match(/^--version$|^-v$/)) {
console.log(`Batchrun v${require("./package.json").version}`);
process.exit();
}
////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////////////
/** @type {string[]} */
const processesStrings = [];
@ -40,12 +59,12 @@ rl.on("line", (input) => {
* @returns
*/
function readCommands(input) {
if (input?.match(/^(reload|restart|reboot)$/i)) {
if (input?.match(/^(reload|restart|reboot|r)$/i)) {
console.log(
` - ${colors.FgBlue}Reloading processes ...${colors.Reset}`
);
restartAll();
} else if (input?.match(/^(reload|restart|reboot) \d/i)) {
} else if (input?.match(/^(reload|restart|reboot|r) \d/i)) {
const processedIndexesString = input.split(" ")[1];
const processedIndexes = processedIndexesString
? processedIndexesString.split(",")
@ -109,11 +128,48 @@ process.stdin.on("keypress", (character, key) => {
}
});
/**
* Cleanup function to kill all child processes
*/
function cleanupChildProcesses() {
for (const childProcess of processes) {
try {
if (childProcess.pid) {
console.log(
` - ${colors.FgYellow}Killing process PID: ${childProcess.pid}${colors.Reset}`
);
killProcessForce(childProcess.pid);
}
childProcess.kill();
} catch (error) {
console.error(
` - ${colors.FgRed}Error:${colors.Reset} Failed to kill process PID: ${childProcess.pid}`
);
}
}
processes = [];
}
process.on("exit", (code) => {
console.log(
` - ${colors.FgBlue}Process exited with code ${code}${colors.Reset}`
);
rl.close();
cleanupChildProcesses();
});
process.on("SIGINT", () => {
console.log(
` - ${colors.FgYellow}SIGINT received, exiting...${colors.Reset}`
);
process.exit();
});
process.on("SIGTERM", () => {
console.log(
` - ${colors.FgYellow}SIGTERM received, exiting...${colors.Reset}`
);
process.exit();
});
////////////////////////////////////////////
@ -164,7 +220,7 @@ if (!processesStrings?.[0]) {
const spawnOptions = {
cwd: process.cwd(),
shell: isWindows ? "bash.exe" : undefined,
stdio: "inherit",
stdio: ["pipe", "inherit", "inherit"],
detached: false,
};
@ -185,6 +241,24 @@ function startProcesses() {
);
if (childProcess) {
childProcess.on("exit", (code, signal) => {
console.log(
` - ${colors.FgRed}Process ${i} exited with code ${code} and signal ${signal}${colors.Reset}`
);
});
childProcess.on("error", (err) => {
console.error(
` - ${colors.FgYellow}Error:${colors.Reset} Failed to start process ${i}: ${err.message}`
);
});
childProcess.on("close", (code, signal) => {
console.log(
` - ${colors.FgRed}Process ${i} closed with code ${code} and signal ${signal}${colors.Reset}`
);
});
processes.push(childProcess);
} else {
console.error(
@ -297,10 +371,5 @@ function killProcessForce(pid) {
console.log(
` - ${colors.FgGreen}Started ${processesStrings.length} processes${colors.Reset}`
);
startProcesses();
setInterval(() => {
console.log(
`Batchrun Running for ${process.uptime().toLocaleString()}s ...`
);
}, 60000);
startProcesses();

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/batchrun",
"version": "1.0.0",
"version": "1.0.1",
"description": "Run and manage multiple processes concurrently in one terminal",
"main": "index.js",
"bin": {