@j-o-r/sh 1.0.5 → 1.0.6
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/package.json +2 -2
- package/types/SH.d.ts +15 -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/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.6",
|
|
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';
|