2025-01-16 05:22:33 +00:00
|
|
|
"use strict";
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
exports.default = startProcess;
|
2025-02-04 06:43:18 +00:00
|
|
|
const fs_1 = __importDefault(require("fs"));
|
2025-01-16 05:22:33 +00:00
|
|
|
const console_colors_1 = __importDefault(require("./console-colors"));
|
|
|
|
const preflight_1 = __importDefault(require("./preflight"));
|
|
|
|
const run_1 = __importDefault(require("./run"));
|
|
|
|
const kill_child_1 = __importDefault(require("./kill-child"));
|
|
|
|
let childProcess = null;
|
2025-01-16 08:18:45 +00:00
|
|
|
const pTitle = "buncid";
|
2025-01-16 05:22:33 +00:00
|
|
|
process.title = pTitle;
|
2025-02-03 13:21:32 +00:00
|
|
|
let timeout;
|
2025-01-16 05:22:33 +00:00
|
|
|
/**
|
|
|
|
* # Start the process
|
|
|
|
* @param {object} param0
|
|
|
|
* @param {string} param0.command
|
|
|
|
* @param {string[] | string} param0.preflight
|
|
|
|
* @param {string[] | string} [param0.postflight]
|
|
|
|
* @param {string} param0.redeploy_file
|
|
|
|
* @param {string | number | (string | number)[]} [param0.port] - The port to kill on rebuild
|
|
|
|
* @param {boolean} [param0.first_run] - Whether to run the preflight on first run. Default `false`
|
|
|
|
*/
|
2025-02-03 13:21:32 +00:00
|
|
|
function startProcess({ command, preflight, postflight, redeploy_file, port, first_run, debounce, }) {
|
|
|
|
const DEBOUNCE = debounce || 1000;
|
2025-01-16 05:22:33 +00:00
|
|
|
try {
|
|
|
|
if (first_run) {
|
|
|
|
console.log("First Run ...");
|
2025-02-03 12:41:13 +00:00
|
|
|
const runFirstPreflight = (0, preflight_1.default)(preflight);
|
2025-01-16 05:22:33 +00:00
|
|
|
}
|
|
|
|
childProcess = (0, run_1.default)(command);
|
|
|
|
if (!childProcess) {
|
|
|
|
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} Process couldn't start. Exiting...`);
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
console.log("Watching", redeploy_file);
|
2025-02-04 06:43:18 +00:00
|
|
|
fs_1.default.watchFile(redeploy_file, { interval: 100 }, (curr, prev) => {
|
|
|
|
if (global.DEPLOYING == 1)
|
|
|
|
return;
|
2025-02-03 13:21:32 +00:00
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
console.log(`${console_colors_1.default.BgBlue}File Changed${console_colors_1.default.Reset}`);
|
|
|
|
if (global.REDEPLOYMENTS == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2025-02-04 06:43:18 +00:00
|
|
|
global.DEPLOYING = 1;
|
2025-02-03 13:21:32 +00:00
|
|
|
if (childProcess) {
|
|
|
|
console.log("******************************");
|
|
|
|
console.log(`******** ${console_colors_1.default.FgBlue}Rebuilding ${console_colors_1.default.FgMagenta}${global.REDEPLOYMENTS}${console_colors_1.default.Reset} ********`);
|
|
|
|
console.log("******************************");
|
|
|
|
try {
|
|
|
|
const runPreflight = (0, preflight_1.default)(preflight);
|
|
|
|
if (!runPreflight) {
|
|
|
|
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} Preflight Failed.`);
|
2025-02-07 10:54:13 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
global.DEPLOYING = 0;
|
|
|
|
}, 2000);
|
2025-02-03 13:21:32 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
(0, kill_child_1.default)(childProcess, port).then((kill) => {
|
|
|
|
if (kill) {
|
|
|
|
childProcess = (0, run_1.default)(command);
|
|
|
|
if (postflight) {
|
|
|
|
const runPostflight = (0, preflight_1.default)(postflight, true);
|
|
|
|
if (!runPostflight) {
|
|
|
|
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} Postflight Failed.`);
|
|
|
|
}
|
2025-01-16 05:22:33 +00:00
|
|
|
}
|
2025-02-04 06:43:18 +00:00
|
|
|
global.DEPLOYING = 0;
|
2025-01-16 05:22:33 +00:00
|
|
|
}
|
2025-02-03 13:21:32 +00:00
|
|
|
else {
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} killing child processes => ${error.message}`);
|
|
|
|
process.exit();
|
2025-01-16 05:22:33 +00:00
|
|
|
}
|
|
|
|
}
|
2025-02-04 06:43:18 +00:00
|
|
|
else {
|
|
|
|
global.DEPLOYING = 0;
|
|
|
|
}
|
2025-02-03 13:21:32 +00:00
|
|
|
}, DEBOUNCE);
|
2025-01-16 05:22:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} First run failed! => ${error.message}`);
|
|
|
|
}
|
|
|
|
}
|