"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; const fs_1 = __importDefault(require("fs")); 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; const pTitle = "buncid"; process.title = pTitle; let timeout; /** * # 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` */ function startProcess({ command, preflight, postflight, redeploy_file, port, first_run, debounce, }) { const DEBOUNCE = debounce || 1000; try { if (first_run) { console.log("First Run ..."); const runFirstPreflight = (0, preflight_1.default)(preflight); } 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); fs_1.default.watchFile(redeploy_file, { interval: 100 }, (curr, prev) => { if (global.DEPLOYING == 1) return; clearTimeout(timeout); timeout = setTimeout(() => { console.log(`${console_colors_1.default.BgBlue}File Changed${console_colors_1.default.Reset}`); if (global.REDEPLOYMENTS == 0) { return; } global.DEPLOYING = 1; 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.`); } 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.`); } } global.DEPLOYING = 0; } 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(); } } else { global.DEPLOYING = 0; } }, DEBOUNCE); }); } catch (error) { console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} First run failed! => ${error.message}`); } }