Refactor to typescript

This commit is contained in:
Benjamin Toby
2025-01-16 06:22:33 +01:00
parent 5f589f89d8
commit 09e8b1f6d7
34 changed files with 756 additions and 381 deletions
+1
View File
@@ -0,0 +1 @@
export {};
+50
View File
@@ -0,0 +1,50 @@
"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();
function run() {
try {
const configText = fs_1.default.readFileSync(path_1.default.join(WORK_DIR, "nodecid.config.json"), "utf-8");
const config = JSON.parse(configText);
const { start, preflight, postflight, build, redeploy_path, first_run, port, } = config;
/** @type {string | undefined} */
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 not found!");
(0, start_1.default)({
command: start,
preflight,
redeploy_file: redeployFile,
first_run,
port,
postflight,
});
}
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 ...");
});
+1
View File
File diff suppressed because one or more lines are too long
+13
View File
@@ -0,0 +1,13 @@
export interface NodeCIConfig {
start: string;
preflight: string[] | string;
postflight?: string[] | string;
redeploy_path?: string;
first_run?: boolean;
port?: string | number | (string | number)[];
build?: NodeCIBuild;
}
export interface NodeCIBuild {
paradigm: "Next.JS" | "Remix";
out_dir?: string;
}
+2
View File
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
+28
View File
@@ -0,0 +1,28 @@
declare const colors: {
Reset: string;
Bright: string;
Dim: string;
Underscore: string;
Blink: string;
Reverse: string;
Hidden: string;
FgBlack: string;
FgRed: string;
FgGreen: string;
FgYellow: string;
FgBlue: string;
FgMagenta: string;
FgCyan: string;
FgWhite: string;
FgGray: string;
BgBlack: string;
BgRed: string;
BgGreen: string;
BgYellow: string;
BgBlue: string;
BgMagenta: string;
BgCyan: string;
BgWhite: string;
BgGray: string;
};
export default colors;
+30
View File
@@ -0,0 +1,30 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const colors = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
FgGray: "\x1b[90m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m",
BgGray: "\x1b[100m",
};
exports.default = colors;
+6
View File
@@ -0,0 +1,6 @@
/**
* ## Kill Child Process Function
* @param {string | number | (string | number)[]} [port]
* @returns {Promise<boolean>}
*/
export default function killChild(port?: string | number | (string | number)[]): Promise<boolean>;
+49
View File
@@ -0,0 +1,49 @@
"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"));
let childProcess = null;
const pTitle = "nodecid";
process.title = pTitle;
/**
* ## Kill Child Process Function
* @param {string | number | (string | number)[]} [port]
* @returns {Promise<boolean>}
*/
function killChild(port) {
return __awaiter(this, void 0, void 0, function* () {
if (!childProcess)
return false;
try {
const childProcessPID = childProcess.pid;
childProcess.kill();
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));
}
}
else if (port) {
yield (0, kill_port_1.default)(Number(port));
}
return true;
}
catch (error) {
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} Child Process couldn't be killed! ${error.message}`);
return false;
}
});
}
+7
View File
@@ -0,0 +1,7 @@
/**
* ## Preflight Function
* @param {string[] | string} preflight
* @param {boolean} [postflight]
* @returns {boolean}
*/
export default function preflightFn(preflight?: string[] | string, postflight?: boolean): boolean;
+45
View File
@@ -0,0 +1,45 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = preflightFn;
const child_process_1 = require("child_process");
const console_colors_1 = __importDefault(require("../utils/console-colors"));
/**
* ## Preflight Function
* @param {string[] | string} preflight
* @param {boolean} [postflight]
* @returns {boolean}
*/
function preflightFn(preflight, postflight) {
const tag = postflight ? "Postflight" : "Preflight";
console.log(`${tag} Running ...`);
const options = {
cwd: process.cwd(),
stdio: "inherit",
};
try {
if (typeof preflight == "string") {
(0, child_process_1.execFileSync)(preflight, options);
}
else if (typeof preflight == "object" && (preflight === null || preflight === void 0 ? void 0 : preflight[0])) {
for (let i = 0; i < preflight.length; i++) {
const cmd = preflight[i];
try {
const execCmd = (0, child_process_1.execSync)(cmd, options);
}
catch (error) {
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} ${tag} command ${cmd} Failed! => ${error.message}`);
return false;
break;
}
}
}
return true;
}
catch (error) {
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} ${tag} Failed! => ${error.message}`);
return false;
}
}
+7
View File
@@ -0,0 +1,7 @@
import { ChildProcess } from "child_process";
/**
* ## Preflight Function
* @param {string} command
* @returns {ChildProcess | null}
*/
export default function run(command: string): ChildProcess | null;
+39
View File
@@ -0,0 +1,39 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = run;
const child_process_1 = require("child_process");
const console_colors_1 = __importDefault(require("./console-colors"));
let redeployments = 0;
const KILL_SIGNAL = "SIGTERM";
const pTitle = "nodecid";
process.title = pTitle;
/**
* ## Preflight Function
* @param {string} command
* @returns {ChildProcess | null}
*/
function run(command) {
console.log("\n******************************");
console.log(`****** ${console_colors_1.default.FgGreen}Starting App ...${console_colors_1.default.Reset} ******`);
console.log("******************************\n");
const startCommandArray = command.split(" ").filter((str) => str.trim());
try {
const firstCommand = startCommandArray.shift();
if (!firstCommand) {
throw new Error("No Starting Command Found in command string!");
}
let childProcess = (0, child_process_1.spawn)(firstCommand, startCommandArray, {
stdio: "inherit",
killSignal: KILL_SIGNAL,
});
redeployments++;
return childProcess;
}
catch (error) {
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} running start command => ${error.message}`);
return null;
}
}
+18
View File
@@ -0,0 +1,18 @@
/**
* # 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`
*/
export default function startProcess({ command, preflight, postflight, redeploy_file, port, first_run, }: {
command: string;
preflight: string[] | string;
postflight?: string[] | string;
redeploy_file: string;
port?: string | number | (string | number)[];
first_run?: boolean;
}): void;
+84
View File
@@ -0,0 +1,84 @@
"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 redeployments = 0;
let childProcess = null;
const pTitle = "nodecid";
process.title = pTitle;
/**
* # 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, }) {
try {
if (first_run) {
console.log("First Run ...");
const runPreflight = (0, preflight_1.default)(preflight);
}
if (!preflight) {
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} No preflight included in config file. If you don't want to run any preflight command simply add an empty array.`);
process.exit();
}
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) => {
console.log(`${console_colors_1.default.BgBlue}File Changed${console_colors_1.default.Reset}`);
if (redeployments == 0)
return;
if (childProcess) {
console.log("******************************");
console.log(`******** ${console_colors_1.default.FgBlue}Rebuilding ${console_colors_1.default.FgMagenta}${redeployments}${console_colors_1.default.Reset} ********`);
console.log("******************************");
try {
const runPreflight = (0, preflight_1.default)(preflight);
if (!runPreflight) {
// TODO: Action to take if preflight fails
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} Preflight Failed.`);
}
else {
(0, kill_child_1.default)(port).then((kill) => {
if (kill) {
childProcess = (0, run_1.default)(command);
if (postflight) {
const runPostflight = (0, preflight_1.default)(postflight, true);
if (!runPostflight) {
// TODO: Action to take if postflight fails
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} Postflight Failed.`);
}
}
}
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();
}
}
});
}
catch (error) {
console.log(`${console_colors_1.default.FgRed}Error:${console_colors_1.default.Reset} First run failed! => ${error.message}`);
}
}
+1
View File
@@ -0,0 +1 @@
declare function githubWebhook(): boolean;
+4
View File
@@ -0,0 +1,4 @@
"use strict";
function githubWebhook() {
return true;
}