This commit is contained in:
Benjamin Toby 2025-02-22 18:02:00 +01:00
parent 7bcddc9517
commit 7e320c87f5
5 changed files with 113 additions and 38 deletions

File diff suppressed because one or more lines are too long

View File

@ -4,4 +4,12 @@ import { ChildProcess } from "child_process";
* @param {string | number | (string | number)[]} [port]
* @returns {Promise<boolean>}
*/
export default function killChild(childProcess?: ChildProcess, port?: string | number | (string | number)[]): Promise<boolean>;
export default function killChild(
/**
* Child Process to be killed
*/
childProcess?: ChildProcess,
/**
* Port/ports to be killed
*/
port?: string | number | (string | number)[]): Promise<boolean>;

View File

@ -20,33 +20,67 @@ const kill_port_1 = __importDefault(require("kill-port"));
* @param {string | number | (string | number)[]} [port]
* @returns {Promise<boolean>}
*/
function killChild(childProcess, port) {
function killChild(
/**
* Child Process to be killed
*/
childProcess,
/**
* Port/ports to be killed
*/
port) {
return __awaiter(this, void 0, void 0, function* () {
if (!childProcess)
return false;
try {
childProcess.kill();
return true;
const childKilled = childProcess.kill();
if (childKilled) {
return true;
}
else {
return yield killPortFn(childProcess, port);
}
}
catch (error) {
try {
if (typeof port == "object" && (port === null || port === void 0 ? void 0 : port[0])) {
for (let i = 0; i < port.length; i++) {
const singlePort = port[i];
yield (0, kill_port_1.default)(Number(singlePort), "tcp");
}
childProcess.kill();
return true;
}
else if (port) {
yield (0, kill_port_1.default)(Number(port), "tcp");
childProcess.kill();
return true;
}
}
catch (_err) { }
yield killPortFn(childProcess, port);
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} Child Process couldn't be killed! ${error.message}`);
return false;
}
});
}
function killPortFn(
/**
* Child Process to be killed
*/
childProcess,
/**
* Port/ports to be killed
*/
port) {
return __awaiter(this, void 0, void 0, function* () {
if (!childProcess)
return false;
try {
if (typeof port == "object" && (port === null || port === void 0 ? void 0 : port[0])) {
for (let i = 0; i < port.length; i++) {
const singlePort = port[i];
yield (0, kill_port_1.default)(Number(singlePort), "tcp");
}
childProcess.kill();
return true;
}
else if (port) {
yield (0, kill_port_1.default)(Number(port), "tcp");
childProcess.kill();
return true;
}
else {
return false;
}
}
catch (_err) {
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} Child Process PORT couldn't be killed! ${_err.message}`);
return false;
}
});
}

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/buncid",
"version": "1.1.1",
"version": "1.1.2",
"author": "Benjamin Toby",
"repository": {
"type": "git",

View File

@ -8,29 +8,27 @@ import killPort from "kill-port";
* @returns {Promise<boolean>}
*/
export default async function killChild(
/**
* Child Process to be killed
*/
childProcess?: ChildProcess,
/**
* Port/ports to be killed
*/
port?: string | number | (string | number)[]
): Promise<boolean> {
if (!childProcess) return false;
try {
childProcess.kill();
return true;
const childKilled = childProcess.kill();
if (childKilled) {
return true;
} else {
return await killPortFn(childProcess, port);
}
} 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) {}
await killPortFn(childProcess, port);
console.log(
`${colors.FgRed}Error:${colors.Reset} Child Process couldn't be killed! ${error.message}`
@ -39,3 +37,38 @@ export default async function killChild(
return false;
}
}
async function killPortFn(
/**
* Child Process to be killed
*/
childProcess?: ChildProcess,
/**
* Port/ports to be killed
*/
port?: string | number | (string | number)[]
): Promise<boolean> {
if (!childProcess) return false;
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;
} else {
return false;
}
} catch (_err: any) {
console.log(
`${colors.FgRed}Error:${colors.Reset} Child Process PORT couldn't be killed! ${_err.message}`
);
return false;
}
}