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