buncid/buncid.ts
Benjamin Toby 69eb6c1128 Updates
2025-02-04 07:43:18 +01:00

88 lines
2.1 KiB
TypeScript
Executable File

#!/usr/bin/env bun
import fs from "fs";
import path from "path";
import colors from "./utils/console-colors";
import startProcess from "./utils/start";
import type { NodeCIConfig } from "./types";
const WORK_DIR = process.cwd();
declare global {
var REDEPLOYMENTS: number;
var DEPLOYING: 0 | 1;
}
global.REDEPLOYMENTS = 0;
global.DEPLOYING = 0;
function run() {
try {
const configText = fs.readFileSync(
path.join(WORK_DIR, "buncid.config.json"),
"utf-8"
);
const config: NodeCIConfig = JSON.parse(configText);
const {
start,
preflight,
postflight,
build,
redeploy_path,
first_run,
port,
debounce,
} = config;
let redeployFile: string | undefined;
if (!redeploy_path) {
const defaultRedeployPath = path.join(WORK_DIR, "REDEPLOY");
const checkExistingPath = fs.existsSync(defaultRedeployPath);
if (!checkExistingPath) {
fs.writeFileSync(
defaultRedeployPath,
Date.now().toString(),
"utf-8"
);
}
redeployFile = path.join(WORK_DIR, "REDEPLOY");
} else {
redeployFile = path.resolve(WORK_DIR, redeploy_path);
}
if (!redeployFile)
throw new Error("Redeploy file variable not provided!");
if (!fs.existsSync(redeployFile))
throw new Error("Redeploy file not found!");
startProcess({
command: start,
preflight,
redeploy_file: redeployFile,
first_run,
port,
postflight,
debounce,
});
} catch (error: any) {
console.log(
`${colors.FgRed}ERROR:${colors.Reset} CI process failed! => ${error.message}`
);
}
}
run();
process.on("exit", () => {
console.log("Process exiting ...");
});
process.on("beforeExit", () => {
console.log("Process Before exit ...");
});