@j-o-r/sh 1.1.6 → 1.1.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/lib/SH.js CHANGED
@@ -31,12 +31,11 @@ import SHDispatch from './SHDispatch.js';
31
31
  import Test from './Test.js'
32
32
 
33
33
  /**
34
- * @typedef {Object.<string, string>} ArgsObject
35
- * @property {string} [key: string] - Any string key maps to an object
34
+ * @typedef {Object.<string, string | string[]>} ArgsObject
35
+ * @property {string} [key: string] - Any string key maps to a string value
36
36
  * @property {string[]} _ - Array of strings, unnamed parameters
37
37
  * @description Parsed parameters result.
38
38
  */
39
-
40
39
  /**
41
40
  * @typedef {Function} RejectCallback
42
41
  * @param {Error} error - The error object passed to the callback.
@@ -271,7 +270,7 @@ function* expBackoff(max = '60s', rand = '100ms') {
271
270
  * - The value is either the next argument or `true` if no value is provided,
272
271
  * - The `_` property contains an array of unbound arguments.
273
272
  */
274
- const parseArgs = (args) => {
273
+ const parseArgsOld = (args) => {
275
274
  const result = { _: [] }; // Initialize result with an empty array for unbound values
276
275
  for (let i = 0; i < args.length; i++) {
277
276
  if (args[i].startsWith('--')) {
@@ -285,7 +284,49 @@ const parseArgs = (args) => {
285
284
  }
286
285
  return result;
287
286
  }
288
-
287
+ /**
288
+ * Parses command-line arguments into an object.
289
+ *
290
+ * The function recognizes arguments that start with two dashes (`--`) or one dash (`-`) as keys,
291
+ * and the subsequent value (if not another key) as the corresponding value.
292
+ * If a key does not have a value, it defaults to `true`.
293
+ * All unrecognized arguments are collected in an array under the `_` property.
294
+ *
295
+ * @param {string[]} [args] - An array of command-line arguments (process.argv.slice(2)).
296
+ * @returns {ArgsObject} An object where:
297
+ * - If args is not passed, process.argv.slice(2) will be the default
298
+ * - Each key corresponds to an argument that starts with `--` or `-`,
299
+ * - The value is either the next argument or `true` if no value is provided,
300
+ * - The `_` property contains an array of unbound arguments.
301
+ */
302
+ const parseArgs = (args) => {
303
+ if (!args) args = process.argv.slice(2);
304
+ const result = { _: [] };
305
+ const seenKeys = new Set();
306
+ for (let i = 0; i < args.length; i++) {
307
+ if (args[i].startsWith('--') || args[i].startsWith('-')) {
308
+ if (args[i].startsWith('-') && !args[i].startsWith('--') && args[i].length > 2) {
309
+ throw new Error(`Invalid argument: ${args[i]}. Use '--' for long options.`);
310
+ }
311
+ const key = args[i].startsWith('--') ? args[i].substring(2) : args[i].substring(1);
312
+ if (seenKeys.has(key)) {
313
+ throw new Error(`Duplicate argument: ${args[i]}`);
314
+ }
315
+ seenKeys.add(key);
316
+ let value;
317
+ if (args[i + 1] && !args[i + 1].startsWith('-')) {
318
+ value = args[i + 1];
319
+ i++; // Skip next element as it is a value
320
+ } else {
321
+ value = true;
322
+ }
323
+ result[key] = value;
324
+ } else {
325
+ result._.push(args[i]);
326
+ }
327
+ }
328
+ return result;
329
+ };
289
330
 
290
331
  export {
291
332
  SH,
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.1.6",
5
+ "version": "1.1.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",
package/types/SH.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export type ArgsObject = {
2
- [x: string]: string;
2
+ [x: string]: string | string[];
3
3
  };
4
4
  export type RejectCallback = Function;
5
5
  export type ResolveCallback = Function;
@@ -44,7 +44,7 @@ export function sleep(duration: string | number): Promise<any>;
44
44
  * // Retry a command 3 times with irregular intervals using exponential backoff
45
45
  * const p = await retry(3, expBackoff(), () => SH`curl -s https://flipwrsi`);
46
46
  */
47
- export function retry(count: number, a: string | typeof expBackoff | Function, b?: Function | undefined): Promise<any>;
47
+ export function retry(count: number, a: string | typeof expBackoff | Function, b?: Function): Promise<any>;
48
48
  /**
49
49
  * This function reads the standard input (stdin) from the current process.
50
50
  * @example
@@ -67,7 +67,7 @@ export function readIn(): Promise<string>;
67
67
  * return 'res';
68
68
  * });
69
69
  */
70
- export function within(callback: Function, resolve?: Function | undefined, reject?: Function | undefined): void;
70
+ export function within(callback: Function, resolve?: ResolveCallback, reject?: RejectCallback): void;
71
71
  /**
72
72
  * Generates an exponential backoff time with a random jitter.
73
73
  *
@@ -76,25 +76,26 @@ export function within(callback: Function, resolve?: Function | undefined, rejec
76
76
  * @param {string} [rand='100ms'] - The maximum random jitter time in a human-readable format (e.g., '100ms' for 100 milliseconds).
77
77
  * @yields {number} The backoff time in milliseconds.
78
78
  */
79
- export function expBackoff(max?: string | undefined, rand?: string | undefined): Generator<number, void, unknown>;
79
+ export function expBackoff(max?: string, rand?: string): Generator<number, void, unknown>;
80
80
  /**
81
81
  * Parses command-line arguments into an object.
82
82
  *
83
- * The function recognizes arguments that start with two dashes (`--`) as keys,
83
+ * The function recognizes arguments that start with two dashes (`--`) or one dash (`-`) as keys,
84
84
  * and the subsequent value (if not another key) as the corresponding value.
85
85
  * If a key does not have a value, it defaults to `true`.
86
86
  * All unrecognized arguments are collected in an array under the `_` property.
87
87
  *
88
- * @param {string[]} args - An array of command-line arguments.
88
+ * @param {string[]} [args] - An array of command-line arguments (process.argv.slice(2)).
89
89
  * @returns {ArgsObject} An object where:
90
- * - Each key corresponds to an argument that starts with `--`,
90
+ * - If args is not passed, process.argv.slice(2) will be the default
91
+ * - Each key corresponds to an argument that starts with `--` or `-`,
91
92
  * - The value is either the next argument or `true` if no value is provided,
92
93
  * - The `_` property contains an array of unbound arguments.
93
94
  */
94
- export function parseArgs(args: string[]): ArgsObject;
95
+ export function parseArgs(args?: string[]): ArgsObject;
95
96
  /**
96
- * @typedef {Object.<string, string>} ArgsObject
97
- * @property {string} [key: string] - Any string key maps to an object
97
+ * @typedef {Object.<string, string | string[]>} ArgsObject
98
+ * @property {string} [key: string] - Any string key maps to a string value
98
99
  * @property {string[]} _ - Array of strings, unnamed parameters
99
100
  * @description Parsed parameters result.
100
101
  */
@@ -62,18 +62,18 @@ declare class SHDispatch {
62
62
  * @param {string} [prefix] - command prefix e.g (default) '/usr/bin/env'
63
63
  * @returns {SHDispatch}
64
64
  */
65
- options(options: import("child_process").SpawnOptions | import("child_process").SpawnSyncOptions, prefix?: string | undefined): SHDispatch;
65
+ options(options: import("child_process").SpawnOptions | import("child_process").SpawnSyncOptions, prefix?: string): SHDispatch;
66
66
  /**
67
67
  * @param {string} [payload]
68
68
  * @returns {Promise<string>}
69
69
  */
70
- run(payload?: string | undefined): Promise<string>;
70
+ run(payload?: string): Promise<string>;
71
71
  /**
72
72
  * Works for screen takeovers like editors
73
73
  * @param {string} [payload]
74
74
  * @returns {import('child_process').SpawnSyncReturns}
75
75
  */
76
- runSync(payload?: string | undefined): import("child_process").SpawnSyncReturns<any>;
76
+ runSync(payload?: string): import("child_process").SpawnSyncReturns<any>;
77
77
  kill(signal?: string): Promise<number[]>;
78
78
  #private;
79
79
  }
@@ -10,12 +10,12 @@ declare class SHExecute {
10
10
  * @param {string} [payload] - data to write
11
11
  * @retuns {Promise<object>}
12
12
  */
13
- runSync(payload?: string | undefined): import("child_process").SpawnSyncReturns<string | Buffer>;
13
+ runSync(payload?: string): import("child_process").SpawnSyncReturns<string | Buffer<ArrayBufferLike>>;
14
14
  /**
15
15
  * @param {string} [payload] - data to write
16
16
  * @retuns {Promise<string>}
17
17
  */
18
- run(payload?: string | undefined): Promise<any>;
18
+ run(payload?: string): Promise<any>;
19
19
  /**
20
20
  * Kill this process and possible child processes
21
21
  * @param {number | string} signal - kill signal
package/types/Test.d.ts CHANGED
@@ -37,7 +37,7 @@ declare class Test {
37
37
  /**
38
38
  * @param {boolean} [quiet] - does not output a report when true, default `false`
39
39
  */
40
- constructor(quiet?: boolean | undefined);
40
+ constructor(quiet?: boolean);
41
41
  /**
42
42
  * Set the timeout when a synced function is called.
43
43
  * This settles async code used in a sync function
@@ -58,7 +58,7 @@ declare class Test {
58
58
  * @param {number[]} [execute] - limit the execution tests
59
59
  * @returns {Promise<Report>}
60
60
  */
61
- run(execute?: number[] | undefined): Promise<Report>;
61
+ run(execute?: number[]): Promise<Report>;
62
62
  /**
63
63
  * Empty tests
64
64
  */