From 6d9121ef667997ce6ae7af612600ee9248b08d4f Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Wed, 4 Dec 2024 07:57:15 +0100 Subject: [PATCH] Updates --- .npmrc | 4 +-- index.js | 87 ++++++++++++++++++++++++++++++++++++++++++++++------ package.json | 2 +- 3 files changed, 81 insertions(+), 12 deletions(-) diff --git a/.npmrc b/.npmrc index 50ccfdf..6bc2539 100644 --- a/.npmrc +++ b/.npmrc @@ -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} diff --git a/index.js b/index.js index 106e40c..03864eb 100644 --- a/index.js +++ b/index.js @@ -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(); diff --git a/package.json b/package.json index 41f3e07..c382f62 100644 --- a/package.json +++ b/package.json @@ -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": {