Refactor to typescript
This commit is contained in:
@@ -28,4 +28,4 @@ const colors = {
|
||||
BgGray: "\x1b[100m",
|
||||
};
|
||||
|
||||
module.exports = colors;
|
||||
export default colors;
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
import { ChildProcess } from "child_process";
|
||||
import colors from "./console-colors";
|
||||
import kill from "kill-port";
|
||||
|
||||
let childProcess: ChildProcess | null = null;
|
||||
|
||||
const pTitle = "nodecid";
|
||||
process.title = pTitle;
|
||||
|
||||
/**
|
||||
* ## Kill Child Process Function
|
||||
* @param {string | number | (string | number)[]} [port]
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
export default async function killChild(
|
||||
port?: string | number | (string | number)[]
|
||||
): Promise<boolean> {
|
||||
if (!childProcess) return false;
|
||||
|
||||
try {
|
||||
const childProcessPID = childProcess.pid;
|
||||
childProcess.kill();
|
||||
|
||||
if (typeof port == "object" && port?.[0]) {
|
||||
for (let i = 0; i < port.length; i++) {
|
||||
const singlePort = port[i];
|
||||
await kill(Number(singlePort));
|
||||
}
|
||||
} else if (port) {
|
||||
await kill(Number(port));
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
console.log(
|
||||
`${colors.FgRed}Error:${colors.Reset} Child Process couldn't be killed! ${error.message}`
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Executable
+46
@@ -0,0 +1,46 @@
|
||||
import { execSync, execFileSync, type ExecSyncOptions } from "child_process";
|
||||
import colors from "../utils/console-colors";
|
||||
|
||||
/**
|
||||
* ## Preflight Function
|
||||
* @param {string[] | string} preflight
|
||||
* @param {boolean} [postflight]
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export default function preflightFn(
|
||||
preflight?: string[] | string,
|
||||
postflight?: boolean
|
||||
): boolean {
|
||||
const tag = postflight ? "Postflight" : "Preflight";
|
||||
console.log(`${tag} Running ...`);
|
||||
|
||||
const options: ExecSyncOptions = {
|
||||
cwd: process.cwd(),
|
||||
stdio: "inherit",
|
||||
};
|
||||
|
||||
try {
|
||||
if (typeof preflight == "string") {
|
||||
execFileSync(preflight, options);
|
||||
} else if (typeof preflight == "object" && preflight?.[0]) {
|
||||
for (let i = 0; i < preflight.length; i++) {
|
||||
const cmd = preflight[i];
|
||||
try {
|
||||
const execCmd = execSync(cmd, options);
|
||||
} catch (error: any) {
|
||||
console.log(
|
||||
`${colors.FgRed}Error:${colors.Reset} ${tag} command ${cmd} Failed! => ${error.message}`
|
||||
);
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
console.log(
|
||||
`${colors.FgRed}Error:${colors.Reset} ${tag} Failed! => ${error.message}`
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Executable
+46
@@ -0,0 +1,46 @@
|
||||
import { spawn, ChildProcess } from "child_process";
|
||||
import colors from "./console-colors";
|
||||
|
||||
let redeployments = 0;
|
||||
|
||||
const KILL_SIGNAL: NodeJS.Signals | number = "SIGTERM";
|
||||
|
||||
const pTitle = "nodecid";
|
||||
process.title = pTitle;
|
||||
|
||||
/**
|
||||
* ## Preflight Function
|
||||
* @param {string} command
|
||||
* @returns {ChildProcess | null}
|
||||
*/
|
||||
export default function run(command: string): ChildProcess | null {
|
||||
console.log("\n******************************");
|
||||
console.log(
|
||||
`****** ${colors.FgGreen}Starting App ...${colors.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 = spawn(firstCommand, startCommandArray, {
|
||||
stdio: "inherit",
|
||||
killSignal: KILL_SIGNAL,
|
||||
});
|
||||
|
||||
redeployments++;
|
||||
|
||||
return childProcess;
|
||||
} catch (error: any) {
|
||||
console.log(
|
||||
`${colors.FgRed}Error:${colors.Reset} running start command => ${error.message}`
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Executable
+123
@@ -0,0 +1,123 @@
|
||||
import fs from "fs";
|
||||
import { ChildProcess } from "child_process";
|
||||
import colors from "./console-colors";
|
||||
import kill from "kill-port";
|
||||
import preflightFn from "./preflight";
|
||||
import run from "./run";
|
||||
import killChild from "./kill-child";
|
||||
|
||||
let redeployments = 0;
|
||||
|
||||
let childProcess: ChildProcess | null = 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`
|
||||
*/
|
||||
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;
|
||||
}) {
|
||||
try {
|
||||
if (first_run) {
|
||||
console.log("First Run ...");
|
||||
const runPreflight = preflightFn(preflight);
|
||||
}
|
||||
|
||||
if (!preflight) {
|
||||
console.log(
|
||||
`${colors.FgRed}Error:${colors.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 = run(command);
|
||||
|
||||
if (!childProcess) {
|
||||
console.log(
|
||||
`${colors.FgRed}Error:${colors.Reset} Process couldn't start. Exiting...`
|
||||
);
|
||||
process.exit();
|
||||
}
|
||||
|
||||
console.log("Watching", redeploy_file);
|
||||
|
||||
fs.watchFile(redeploy_file, { interval: 100 }, (curr, prev) => {
|
||||
console.log(`${colors.BgBlue}File Changed${colors.Reset}`);
|
||||
|
||||
if (redeployments == 0) return;
|
||||
|
||||
if (childProcess) {
|
||||
console.log("******************************");
|
||||
console.log(
|
||||
`******** ${colors.FgBlue}Rebuilding ${colors.FgMagenta}${redeployments}${colors.Reset} ********`
|
||||
);
|
||||
console.log("******************************");
|
||||
|
||||
try {
|
||||
const runPreflight = preflightFn(preflight);
|
||||
|
||||
if (!runPreflight) {
|
||||
// TODO: Action to take if preflight fails
|
||||
|
||||
console.log(
|
||||
`${colors.FgRed}Error:${colors.Reset} Preflight Failed.`
|
||||
);
|
||||
} else {
|
||||
killChild(port).then((kill) => {
|
||||
if (kill) {
|
||||
childProcess = run(command);
|
||||
|
||||
if (postflight) {
|
||||
const runPostflight = preflightFn(
|
||||
postflight,
|
||||
true
|
||||
);
|
||||
|
||||
if (!runPostflight) {
|
||||
// TODO: Action to take if postflight fails
|
||||
|
||||
console.log(
|
||||
`${colors.FgRed}Error:${colors.Reset} Postflight Failed.`
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
process.exit();
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log(
|
||||
`${colors.FgRed}Error:${colors.Reset} killing child processes => ${error.message}`
|
||||
);
|
||||
process.exit();
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.log(
|
||||
`${colors.FgRed}Error:${colors.Reset} First run failed! => ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user