2025-01-16 05:22:33 +00:00
|
|
|
import { ChildProcess } from "child_process";
|
|
|
|
import colors from "./console-colors";
|
|
|
|
import kill from "kill-port";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Kill Child Process Function
|
|
|
|
* @param {string | number | (string | number)[]} [port]
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
export default async function killChild(
|
2025-01-16 08:28:46 +00:00
|
|
|
childProcess?: ChildProcess,
|
2025-01-16 05:22:33 +00:00
|
|
|
port?: string | number | (string | number)[]
|
|
|
|
): Promise<boolean> {
|
|
|
|
if (!childProcess) return false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const childProcessPID = childProcess.pid;
|
|
|
|
childProcess.kill();
|
|
|
|
|
2025-02-03 12:41:13 +00:00
|
|
|
try {
|
|
|
|
if (typeof port == "object" && port?.[0]) {
|
|
|
|
for (let i = 0; i < port.length; i++) {
|
|
|
|
const singlePort = port[i];
|
|
|
|
await kill(Number(singlePort));
|
|
|
|
}
|
|
|
|
} else if (port) {
|
|
|
|
await kill(Number(port));
|
2025-01-16 05:22:33 +00:00
|
|
|
}
|
2025-02-03 12:41:13 +00:00
|
|
|
} catch (error) {}
|
2025-01-16 05:22:33 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (error: any) {
|
|
|
|
console.log(
|
|
|
|
`${colors.FgRed}Error:${colors.Reset} Child Process couldn't be killed! ${error.message}`
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|