Add port array and postflight

This commit is contained in:
Benjamin Toby 2023-11-06 21:06:33 +01:00
parent bb3d0712ca
commit 5f589f89d8
5 changed files with 61 additions and 12 deletions

View File

@ -99,8 +99,9 @@ This app just runs whatever command you send it in an isolated child process, th
- **`start`**: _string_: The start Command - **`start`**: _string_: The start Command
- **`preflight`**: _string | Array_: Array of commands or shell script file to run before reloading application - **`preflight`**: _string | Array_: Array of commands or shell script file to run before reloading application
- **`postflight`**: _string | Array_: _Optional_: Array of commands or shell script file to run after reloading application
- **`redeploy_path`**: _string_: _Optional_: The path to trigger a redeployment. Default `./REDEPLOY` - **`redeploy_path`**: _string_: _Optional_: The path to trigger a redeployment. Default `./REDEPLOY`
- **`port`**: _string | number_: _Optional_: A port to kill if running a server. _NOTE_: it is important to provide this option if running a server else the process may not terminate properly - **`port`**: _string | number | (string | number)[]_: _Optional_: A port(or array of ports) to kill if running a server. _NOTE_: it is important to provide this option if running a server else the process may not terminate properly
- **`first_run`**: _boolean_: _Optional_: If the preflight should run on first run. Default `false`. - **`first_run`**: _boolean_: _Optional_: If the preflight should run on first run. Default `false`.
### Redeployment ### Redeployment

View File

@ -34,11 +34,19 @@ process.title = pTitle;
* @param {object} param0 * @param {object} param0
* @param {string} param0.command * @param {string} param0.command
* @param {string[] | string} param0.preflight * @param {string[] | string} param0.preflight
* @param {string[] | string} [param0.postflight]
* @param {string} param0.redeploy_file * @param {string} param0.redeploy_file
* @param {string | number} [param0.port] - The port to kill on rebuild * @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` * @param {boolean} [param0.first_run] - Whether to run the preflight on first run. Default `false`
*/ */
function startProcess({ command, preflight, redeploy_file, port, first_run }) { function startProcess({
command,
preflight,
postflight,
redeploy_file,
port,
first_run,
}) {
try { try {
if (first_run) { if (first_run) {
console.log("First Run ..."); console.log("First Run ...");
@ -88,6 +96,21 @@ function startProcess({ command, preflight, redeploy_file, port, first_run }) {
killChild(port).then((kill) => { killChild(port).then((kill) => {
if (kill) { if (kill) {
childProcess = run(command); 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 { } else {
process.exit(); process.exit();
} }
@ -160,10 +183,12 @@ function run(command) {
/** /**
* ## Preflight Function * ## Preflight Function
* @param {string[] | string} preflight * @param {string[] | string} preflight
* @param {boolean} [postflight]
* @returns {boolean} * @returns {boolean}
*/ */
function preflightFn(preflight) { function preflightFn(preflight, postflight) {
console.log("Preflight Running ..."); const tag = postflight ? "Postflight" : "Preflight";
console.log(`${tag} Running ...`);
/** @type {import("child_process").ExecSyncOptions} */ /** @type {import("child_process").ExecSyncOptions} */
const options = { const options = {
@ -181,7 +206,7 @@ function preflightFn(preflight) {
const execCmd = execSync(cmd, options); const execCmd = execSync(cmd, options);
} catch (error) { } catch (error) {
console.log( console.log(
`${colors.FgRed}Error:${colors.Reset} Preflight command ${cmd} Failed! => ${error.message}` `${colors.FgRed}Error:${colors.Reset} ${tag} command ${cmd} Failed! => ${error.message}`
); );
return false; return false;
break; break;
@ -191,7 +216,7 @@ function preflightFn(preflight) {
return true; return true;
} catch (error) { } catch (error) {
console.log( console.log(
`${colors.FgRed}Error:${colors.Reset} Preflight Failed! => ${error.message}` `${colors.FgRed}Error:${colors.Reset} ${tag} Failed! => ${error.message}`
); );
return false; return false;
} }
@ -203,7 +228,7 @@ function preflightFn(preflight) {
/** /**
* ## Kill Child Process Function * ## Kill Child Process Function
* @param {string | number} [port] * @param {string | number | (string | number)[]} [port]
* @returns {Promise<boolean>} * @returns {Promise<boolean>}
*/ */
async function killChild(port) { async function killChild(port) {
@ -213,7 +238,12 @@ async function killChild(port) {
const childProcessPID = childProcess.pid; const childProcessPID = childProcess.pid;
childProcess.kill(); childProcess.kill();
if (port) { 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)); await kill(Number(port));
} }

View File

@ -26,7 +26,15 @@ function run() {
/** @type {NodeCIConfig} */ /** @type {NodeCIConfig} */
const config = JSON.parse(configText); const config = JSON.parse(configText);
const { start, preflight, redeploy_path, first_run, port } = config; const {
start,
preflight,
postflight,
build,
redeploy_path,
first_run,
port,
} = config;
/** @type {string | undefined} */ /** @type {string | undefined} */
let redeployFile; let redeployFile;
@ -56,6 +64,7 @@ function run() {
redeploy_file: redeployFile, redeploy_file: redeployFile,
first_run, first_run,
port, port,
postflight,
}); });
} catch (error) { } catch (error) {
console.log( console.log(

View File

@ -1,6 +1,6 @@
{ {
"name": "nodecid", "name": "nodecid",
"version": "1.0.6", "version": "1.0.7",
"description": "Simple CI/CD process", "description": "Simple CI/CD process",
"main": "index.js", "main": "index.js",
"bin": { "bin": {

View File

@ -3,8 +3,17 @@
* @property {string} start - Start command. Eg `node index.js` * @property {string} start - Start command. Eg `node index.js`
* @property {string[] | string} preflight - And array of commands to run before * @property {string[] | string} preflight - And array of commands to run before
* the application starts, or a single `.sh` file path. * the application starts, or a single `.sh` file path.
* @property {string[] | string} [postflight] - And array of commands to run after
* the application starts.
* @property {string} [redeploy_path] - The path to the file that will trigger a * @property {string} [redeploy_path] - The path to the file that will trigger a
* redeployment if content is changed. Default file path is `./REDEPLOY` * redeployment if content is changed. Default file path is `./REDEPLOY`
* @property {boolean} [first_run] - Whether to run the preflight on first run. Default `false` * @property {boolean} [first_run] - Whether to run the preflight on first run. Default `false`
* @property {string | number} [port] - The port to kill on reload * @property {string | number | (string | number)[]} [port] - The port to kill on reload
* @property {NodeCIBuild} [build] - Build configurations
*/
/**
* @typedef {object} NodeCIBuild
* @property {"Next.JS" | "Remix"} paradigm - The paradigm to build on
* @property {string} [out_dir] - The output Directory. Default `.dist`.
*/ */