#!/usr/bin/env bun
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const console_colors_1 = __importDefault(require("./utils/console-colors"));
const start_1 = __importDefault(require("./utils/start"));
const WORK_DIR = process.cwd();
global.REDEPLOYMENTS = 0;
global.DEPLOYING = 0;
function run() {
    try {
        const configText = fs_1.default.readFileSync(path_1.default.join(WORK_DIR, "buncid.config.json"), "utf-8");
        const config = JSON.parse(configText);
        const { start, preflight, postflight, build, redeploy_path, first_run, port, debounce, } = config;
        let redeployFile;
        if (!redeploy_path) {
            const defaultRedeployPath = path_1.default.join(WORK_DIR, "REDEPLOY");
            const checkExistingPath = fs_1.default.existsSync(defaultRedeployPath);
            if (!checkExistingPath) {
                fs_1.default.writeFileSync(defaultRedeployPath, Date.now().toString(), "utf-8");
            }
            redeployFile = path_1.default.join(WORK_DIR, "REDEPLOY");
        }
        else {
            redeployFile = path_1.default.resolve(WORK_DIR, redeploy_path);
        }
        if (!redeployFile)
            throw new Error("Redeploy file variable not provided!");
        if (!fs_1.default.existsSync(redeployFile))
            throw new Error("Redeploy file not found!");
        (0, start_1.default)({
            command: start,
            preflight,
            redeploy_file: redeployFile,
            first_run,
            port,
            postflight,
            debounce,
        });
    }
    catch (error) {
        console.log(`${console_colors_1.default.FgRed}ERROR:${console_colors_1.default.Reset} CI process failed! => ${error.message}`);
    }
}
run();
process.on("exit", () => {
    console.log("Process exiting ...");
});
process.on("beforeExit", () => {
    console.log("Process Before exit ...");
});