@j-o-r/sh 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/SH.js +29 -1
- package/lib/SHDispatch.js +4 -4
- package/lib/SHExecute.js +12 -3
- package/package.json +2 -2
- package/types/SH.d.ts +15 -0
- package/types/SHDispatch.d.ts +1 -1
- package/types/SHExecute.d.ts +1 -0
package/README.md
CHANGED
|
@@ -59,7 +59,7 @@ The `SH` method accepts a template literal string enclosed in backticks as its a
|
|
|
59
59
|
### Additional Utilities
|
|
60
60
|
|
|
61
61
|
The module also provides additional utilities for common tasks:
|
|
62
|
-
- `args
|
|
62
|
+
- `parseArgs(process.args)`: Transform an array of strings into an object
|
|
63
63
|
- `cd(dir)`: Change the working directory.
|
|
64
64
|
- `sleep(duration)`: Pause execution for a specified duration.
|
|
65
65
|
- `retry(count, interval, callback)`: Retry a command a specified number of times with an optional interval.
|
package/lib/SH.js
CHANGED
|
@@ -29,7 +29,6 @@
|
|
|
29
29
|
import assert from 'node:assert';
|
|
30
30
|
import SHDispatch from './SHDispatch.js';
|
|
31
31
|
|
|
32
|
-
|
|
33
32
|
/**
|
|
34
33
|
* Creates a new SHDispatch object that represents a command to be executed.
|
|
35
34
|
*
|
|
@@ -211,6 +210,34 @@ function* expBackoff(max = '60s', rand = '100ms') {
|
|
|
211
210
|
yield Math.min(2 ** n++, maxMs) + ms;
|
|
212
211
|
}
|
|
213
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Parses command-line arguments into an object.
|
|
215
|
+
*
|
|
216
|
+
* The function recognizes arguments that start with two dashes (`--`) as keys,
|
|
217
|
+
* and the subsequent value (if not another key) as the corresponding value.
|
|
218
|
+
* If a key does not have a value, it defaults to `true`.
|
|
219
|
+
* All unrecognized arguments are collected in an array under the `_` property.
|
|
220
|
+
*
|
|
221
|
+
* @param {string[]} args - An array of command-line arguments.
|
|
222
|
+
* @returns {object} An object where:
|
|
223
|
+
* - Each key corresponds to an argument that starts with `--`,
|
|
224
|
+
* - The value is either the next argument or `true` if no value is provided,
|
|
225
|
+
* - The `_` property contains an array of unbound arguments.
|
|
226
|
+
*/
|
|
227
|
+
const parseArgs = (args) => {
|
|
228
|
+
const result = { _: [] }; // Initialize result with an empty array for unbound values
|
|
229
|
+
for (let i = 0; i < args.length; i++) {
|
|
230
|
+
if (args[i].startsWith('--')) {
|
|
231
|
+
const key = args[i].substring(2);
|
|
232
|
+
const value = args[i + 1] && !args[i + 1].startsWith('--') ? args[i + 1] : true;
|
|
233
|
+
result[key] = value;
|
|
234
|
+
if (value !== true) i++; // Skip the next element as it is a value
|
|
235
|
+
} else {
|
|
236
|
+
result._.push(args[i]); // Add unbound value to the array
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return result;
|
|
240
|
+
}
|
|
214
241
|
export {
|
|
215
242
|
SH,
|
|
216
243
|
cd,
|
|
@@ -219,4 +246,5 @@ export {
|
|
|
219
246
|
readIn,
|
|
220
247
|
within,
|
|
221
248
|
expBackoff,
|
|
249
|
+
parseArgs
|
|
222
250
|
}
|
package/lib/SHDispatch.js
CHANGED
|
@@ -140,11 +140,11 @@ class SHDispatch {
|
|
|
140
140
|
// @ts-ignore
|
|
141
141
|
return new SHExec(this.#cmd, this.#options).runSync(payload);
|
|
142
142
|
}
|
|
143
|
-
async kill() {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
} catch (_e) { }
|
|
143
|
+
async kill(signal = 'SIGTERM') {
|
|
144
|
+
let res = [];
|
|
145
|
+
res = await this.#proc.kill(signal);
|
|
147
146
|
this.#proc = undefined;
|
|
147
|
+
return res;
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
150
|
|
package/lib/SHExecute.js
CHANGED
|
@@ -20,7 +20,7 @@ const killProcesses = (processPid, signal) => {
|
|
|
20
20
|
reject(new Error(stderr));
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
23
|
-
const pids = stdout.split(/\r?\n/).filter(pid => pid);
|
|
23
|
+
const pids = stdout.split(/\r?\n/).filter(pid => pid) || [];
|
|
24
24
|
// Kill each child process
|
|
25
25
|
try {
|
|
26
26
|
for (const pid of pids) {
|
|
@@ -48,6 +48,7 @@ class SHExecute {
|
|
|
48
48
|
/**
|
|
49
49
|
* @type {import('child_process').ChildProcess}
|
|
50
50
|
*/
|
|
51
|
+
#forcedKill = false;
|
|
51
52
|
#proc;
|
|
52
53
|
#command = '';
|
|
53
54
|
#options = {};
|
|
@@ -127,6 +128,11 @@ class SHExecute {
|
|
|
127
128
|
}
|
|
128
129
|
this.#proc.on('close', (code) => {
|
|
129
130
|
if (timeout) clearTimeout(timeout);
|
|
131
|
+
if (this.#forcedKill) {
|
|
132
|
+
// Resolve without content
|
|
133
|
+
resolve();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
130
136
|
if (code === 0) {
|
|
131
137
|
resolve(this.#stdout.trim());
|
|
132
138
|
} else {
|
|
@@ -140,13 +146,16 @@ class SHExecute {
|
|
|
140
146
|
});
|
|
141
147
|
}
|
|
142
148
|
/**
|
|
149
|
+
* @param {string} signal - kill signal
|
|
143
150
|
* @returns {Promise<number[]>}
|
|
144
151
|
*/
|
|
145
152
|
async kill(signal = 'SIGTERM') {
|
|
146
153
|
if (!this.#proc) throw new Error('Trying to kill a process without creating one.');
|
|
147
154
|
if (!this.#proc.pid) throw new Error('The process pid is undefined.');
|
|
148
|
-
|
|
149
|
-
|
|
155
|
+
this.#forcedKill = true;
|
|
156
|
+
const res = killProcesses(this.#proc.pid, signal);
|
|
157
|
+
this.#proc = undefined;
|
|
158
|
+
return res;
|
|
150
159
|
}
|
|
151
160
|
}
|
|
152
161
|
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@j-o-r/sh",
|
|
3
3
|
"author": "Jorrit Duin <j-o-r@duin.work>",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "1.0.
|
|
5
|
+
"version": "1.0.7",
|
|
6
6
|
"description": "Execute shell commands on Linux-based systems from javascript",
|
|
7
7
|
"main": "lib/SH.js",
|
|
8
8
|
"types": "types/SH.d.ts",
|
|
@@ -46,4 +46,4 @@
|
|
|
46
46
|
"process-promise",
|
|
47
47
|
"process-output"
|
|
48
48
|
]
|
|
49
|
-
}
|
|
49
|
+
}
|
package/types/SH.d.ts
CHANGED
|
@@ -68,4 +68,19 @@ export function within(callback: Function): void;
|
|
|
68
68
|
* @yields {number} The backoff time in milliseconds.
|
|
69
69
|
*/
|
|
70
70
|
export function expBackoff(max?: string | undefined, rand?: string | undefined): Generator<number, void, unknown>;
|
|
71
|
+
/**
|
|
72
|
+
* Parses command-line arguments into an object.
|
|
73
|
+
*
|
|
74
|
+
* The function recognizes arguments that start with two dashes (`--`) as keys,
|
|
75
|
+
* and the subsequent value (if not another key) as the corresponding value.
|
|
76
|
+
* If a key does not have a value, it defaults to `true`.
|
|
77
|
+
* All unrecognized arguments are collected in an array under the `_` property.
|
|
78
|
+
*
|
|
79
|
+
* @param {string[]} args - An array of command-line arguments.
|
|
80
|
+
* @returns {object} An object where:
|
|
81
|
+
* - Each key corresponds to an argument that starts with `--`,
|
|
82
|
+
* - The value is either the next argument or `true` if no value is provided,
|
|
83
|
+
* - The `_` property contains an array of unbound arguments.
|
|
84
|
+
*/
|
|
85
|
+
export function parseArgs(args: string[]): object;
|
|
71
86
|
import SHDispatch from './SHDispatch.js';
|
package/types/SHDispatch.d.ts
CHANGED