From 99cc6708c68891b2d397ad8f788d698ee64f5681 Mon Sep 17 00:00:00 2001 From: Ian Wijma Date: Sun, 19 Nov 2023 16:19:24 +1100 Subject: [PATCH] Add new scripts to package.json and handle rpm/rph conflict Updated the package.json with two new scripts: copy-local and build-local. This allows for easier local testing by building and copying the program to a local directory. In index.js, handled the situation where both rpm and rph options are provided by the user, which was causing conflicts. This is resolved by adding explicit handling which returns an error and asks the user to choose only one. --- package.json | 4 +++- src/index.js | 14 +++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 654238f..c4df53e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "bin": "src/index.js", "main": "src/index.js", "scripts": { - "build": "pkg package.json" + "build": "pkg package.json", + "copy-local": "cp -f out/kr-linux ~/bin/kr", + "build-local": "npm run build && npm run copy-local" }, "pkg": { "scripts": "src/**/*.js", diff --git a/src/index.js b/src/index.js index f3fef06..048d14e 100644 --- a/src/index.js +++ b/src/index.js @@ -20,7 +20,6 @@ const yargs = Yargs(process.argv.splice(2)) description: 'Time in seconds we want to delay the restart with.', type: 'number' }) - .conflicts('_rpm', '_rph') const { _ = [], _rpm: rpm, _rph: rph, _delay: delay } = yargs.argv; const [command, ...args ] = _; @@ -36,7 +35,9 @@ let historyMax = 4; let seconds = SECONDS_IN_A_MINUTE; let restartName = 'minute'; -if (rpm) { +if (rpm && rph) { + return console.error('Currently, can not define both --rpm and --rph, please choose only one.'); +} else if (rpm) { historyMax = rpm } else if (rph) { historyMax = rph @@ -64,8 +65,15 @@ const checkHistory = () => Object.keys(history).length <= historyMax; const restart = () => setTimeout(() => runCommand(delay), delay * 1000); +const spawnCommand = () => { + const [ spawnCommand, ...cmdArgs ] = command.split(' '); + const spawnArgs = [...cmdArgs, ...args]; + + return spawn(spawnCommand, spawnArgs); +} + const runCommand = () => { - const runner = spawn(command, args); + const runner = spawnCommand(); const commandLogs = [];