From 5f589f89d804106d1490bafad4dedbfd7dee8c7d Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Mon, 6 Nov 2023 21:06:33 +0100 Subject: [PATCH] Add port array and postflight --- README.md | 3 ++- deploy/start.js | 46 ++++++++++++++++++++++++++++++++++++++-------- index.js | 11 ++++++++++- package.json | 2 +- types.d.js | 11 ++++++++++- 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a695f42..6a32f06 100644 --- a/README.md +++ b/README.md @@ -99,8 +99,9 @@ This app just runs whatever command you send it in an isolated child process, th - **`start`**: _string_: The start Command - **`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` -- **`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`. ### Redeployment diff --git a/deploy/start.js b/deploy/start.js index a12aa22..ddaec2c 100755 --- a/deploy/start.js +++ b/deploy/start.js @@ -34,11 +34,19 @@ process.title = pTitle; * @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} [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` */ -function startProcess({ command, preflight, redeploy_file, port, first_run }) { +function startProcess({ + command, + preflight, + postflight, + redeploy_file, + port, + first_run, +}) { try { if (first_run) { console.log("First Run ..."); @@ -88,6 +96,21 @@ function startProcess({ command, preflight, redeploy_file, port, first_run }) { 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(); } @@ -160,10 +183,12 @@ function run(command) { /** * ## Preflight Function * @param {string[] | string} preflight + * @param {boolean} [postflight] * @returns {boolean} */ -function preflightFn(preflight) { - console.log("Preflight Running ..."); +function preflightFn(preflight, postflight) { + const tag = postflight ? "Postflight" : "Preflight"; + console.log(`${tag} Running ...`); /** @type {import("child_process").ExecSyncOptions} */ const options = { @@ -181,7 +206,7 @@ function preflightFn(preflight) { const execCmd = execSync(cmd, options); } catch (error) { 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; break; @@ -191,7 +216,7 @@ function preflightFn(preflight) { return true; } catch (error) { console.log( - `${colors.FgRed}Error:${colors.Reset} Preflight Failed! => ${error.message}` + `${colors.FgRed}Error:${colors.Reset} ${tag} Failed! => ${error.message}` ); return false; } @@ -203,7 +228,7 @@ function preflightFn(preflight) { /** * ## Kill Child Process Function - * @param {string | number} [port] + * @param {string | number | (string | number)[]} [port] * @returns {Promise} */ async function killChild(port) { @@ -213,7 +238,12 @@ async function killChild(port) { const childProcessPID = childProcess.pid; 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)); } diff --git a/index.js b/index.js index 381b4da..97830ef 100755 --- a/index.js +++ b/index.js @@ -26,7 +26,15 @@ function run() { /** @type {NodeCIConfig} */ 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} */ let redeployFile; @@ -56,6 +64,7 @@ function run() { redeploy_file: redeployFile, first_run, port, + postflight, }); } catch (error) { console.log( diff --git a/package.json b/package.json index 1467b13..3b9acdb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nodecid", - "version": "1.0.6", + "version": "1.0.7", "description": "Simple CI/CD process", "main": "index.js", "bin": { diff --git a/types.d.js b/types.d.js index 5fd8135..62494a4 100644 --- a/types.d.js +++ b/types.d.js @@ -3,8 +3,17 @@ * @property {string} start - Start command. Eg `node index.js` * @property {string[] | string} preflight - And array of commands to run before * 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 * 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 {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`. */