buncid/utils/kill-child.ts
Benjamin Toby 4540404522 Updates
2025-02-21 09:00:49 +01:00

39 lines
1.1 KiB
TypeScript
Executable File

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