buncid/dist/utils/kill-child.js

87 lines
3.0 KiB
JavaScript
Raw Normal View History

2025-01-16 05:22:33 +00:00
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = killChild;
const console_colors_1 = __importDefault(require("./console-colors"));
const kill_port_1 = __importDefault(require("kill-port"));
/**
* ## Kill Child Process Function
* @param {string | number | (string | number)[]} [port]
* @returns {Promise<boolean>}
*/
2025-02-22 17:02:00 +00:00
function killChild(
/**
* Child Process to be killed
*/
childProcess,
/**
* Port/ports to be killed
*/
port) {
2025-01-16 05:22:33 +00:00
return __awaiter(this, void 0, void 0, function* () {
if (!childProcess)
return false;
try {
2025-02-22 17:02:00 +00:00
const childKilled = childProcess.kill();
if (childKilled) {
return true;
}
else {
return yield killPortFn(childProcess, port);
}
2025-02-22 13:55:26 +00:00
}
catch (error) {
2025-02-22 17:02:00 +00:00
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");
2025-01-16 05:22:33 +00:00
}
2025-02-22 17:02:00 +00:00
childProcess.kill();
return true;
2025-01-16 05:22:33 +00:00
}
2025-02-22 17:02:00 +00:00
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}`);
2025-01-16 05:22:33 +00:00
return false;
}
});
}