@j-o-r/sh 1.1.28 → 1.1.29
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 +119 -154
- package/TODO.md +18 -0
- package/lib/AsyncTracker.js +126 -107
- package/lib/SH.js +158 -154
- package/lib/SHDispatch.js +105 -91
- package/lib/SHExecute.js +83 -41
- package/lib/Test.js +153 -123
- package/package.json +1 -1
- package/types/AsyncTracker.d.ts +65 -31
- package/types/SH.d.ts +157 -164
- package/types/SHDispatch.d.ts +55 -78
- package/types/SHExecute.d.ts +44 -23
- package/types/Test.d.ts +66 -30
package/types/SH.d.ts
CHANGED
|
@@ -3,196 +3,189 @@ export type ArgsObject = {
|
|
|
3
3
|
};
|
|
4
4
|
export type RejectCallback = Function;
|
|
5
5
|
export type ResolveCallback = Function;
|
|
6
|
+
export type SHOptions = import("./SHDispatch.js").SHOptions;
|
|
7
|
+
export type AbortableInput = {
|
|
8
|
+
/**
|
|
9
|
+
* - Promise that resolves to user input or void if aborted.
|
|
10
|
+
*/
|
|
11
|
+
input: Promise<string | void>;
|
|
12
|
+
/**
|
|
13
|
+
* - Function to abort input collection.
|
|
14
|
+
*/
|
|
15
|
+
abort: () => void;
|
|
16
|
+
};
|
|
17
|
+
export type ExpBackoffGenerator = Object;
|
|
6
18
|
/**
|
|
7
|
-
*
|
|
19
|
+
* Template tag for building and executing shell commands.
|
|
20
|
+
*
|
|
21
|
+
* Returns an {@link SHDispatch} instance for configuration (.options()) and execution (.run(), .runSync()).
|
|
22
|
+
*
|
|
23
|
+
* Interpolation rules:
|
|
24
|
+
* - Arrays: Elements trimmed, newlines/tabs escaped, shell-special chars single-quoted with '\'' escape.
|
|
25
|
+
* - Other values: String(value) inserted as-is (NOT auto-escaped; use {@link bashEscape} for safety).
|
|
26
|
+
*
|
|
27
|
+
* SH acts as both template tag and options setter: `SH.timeout = 5000; SH`cmd``
|
|
28
|
+
*
|
|
29
|
+
* @param {TemplateStringsArray} pieces - String literals from template.
|
|
30
|
+
* @param {...unknown[]} args - Values to interpolate.
|
|
31
|
+
* @returns {SHDispatch} Command dispatcher.
|
|
32
|
+
* @throws {Error} If pieces contain undefined.
|
|
33
|
+
* @example
|
|
34
|
+
* const cmd = SH`echo ${'Hello'}`;
|
|
35
|
+
* await cmd.run(); // Executes: echo Hello
|
|
36
|
+
*
|
|
37
|
+
* // Array interpolation
|
|
38
|
+
* SH`ls ${['-la', '/']}`.run(); // ls '-la' '/'
|
|
8
39
|
*/
|
|
9
|
-
export
|
|
40
|
+
export const SH: any;
|
|
10
41
|
/**
|
|
11
|
-
*
|
|
42
|
+
* Changes the current working directory.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} dir - Path to new directory.
|
|
45
|
+
* @example
|
|
46
|
+
* cd('/tmp');
|
|
12
47
|
*/
|
|
13
|
-
export
|
|
14
|
-
/**
|
|
15
|
-
* A template-tag that returns an SHDispatch.
|
|
16
|
-
* @typedef {(pieces: TemplateStringsArray, ...args: unknown[]) => SHDispatch} SHTag
|
|
17
|
-
*/
|
|
48
|
+
export function cd(dir: string): void;
|
|
18
49
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
* @returns {SHDispatch}
|
|
28
|
-
*/
|
|
29
|
-
export const SH: Shell & SHTag & import("./SHDispatch.js").SHOptions;
|
|
50
|
+
* Sleeps for a specified duration.
|
|
51
|
+
*
|
|
52
|
+
* @param {string|number} duration - Duration as '5s', '100ms', or ms number.
|
|
53
|
+
* @returns {Promise<void>}
|
|
54
|
+
* @example
|
|
55
|
+
* await sleep('2s');
|
|
56
|
+
*/
|
|
57
|
+
export function sleep(duration: string | number): Promise<void>;
|
|
30
58
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
|
|
34
|
-
|
|
59
|
+
* Retries an async function up to N times with optional delays.
|
|
60
|
+
*
|
|
61
|
+
* Delay can be fixed ('1s'), generator (expBackoff()), or none.
|
|
62
|
+
*
|
|
63
|
+
* @param {number} count - Number of attempts.
|
|
64
|
+
* @param {string|number|ExpBackoffGenerator|Function} delayOrFn - Delay or callback if no separate fn.
|
|
65
|
+
* @param {Function} [fn] - Callback to retry (if delayOrFn not function).
|
|
66
|
+
* @returns {Promise<any>} Successful result.
|
|
67
|
+
* @throws {Error} Last error if all attempts fail.
|
|
68
|
+
* @example
|
|
69
|
+
* await retry(3, '1s', () => SH`curl http://unreliable`.run());
|
|
70
|
+
* await retry(3, expBackoff(), () => flakyOp());
|
|
71
|
+
*/
|
|
72
|
+
export function retry(count: number, a: any, b: any): Promise<any>;
|
|
35
73
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
*
|
|
41
|
-
* await sleep('5s');
|
|
42
|
-
*/
|
|
43
|
-
export function sleep(duration: string | number): Promise<any>;
|
|
44
|
-
/**
|
|
45
|
-
* Retries a given asynchronous function a specified number of times with optional delays between attempts.
|
|
46
|
-
*
|
|
47
|
-
* @param {number} count - The number of retry attempts.
|
|
48
|
-
* @param {string|number|expBackoff|Function} a - Either a delay duration as a string, a delay generator object, or the callback function.
|
|
49
|
-
* @param {Function} [b] - The callback function to retry, required if `a` is not a function.
|
|
50
|
-
* @returns {Promise<*>} - The result of the callback function if it succeeds within the retry attempts.
|
|
51
|
-
* @throws {Error} - The last error encountered if all retry attempts fail.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* // Retry a command 3 times
|
|
55
|
-
* const p = await retry(3, () => SH`curl -s https://flipwrsi`);
|
|
56
|
-
*
|
|
57
|
-
* // Retry a command 3 times with an interval of 1 second between each try
|
|
58
|
-
* const p = await retry(3, '1s', () => SH`curl -s https://flipwrsi`);
|
|
59
|
-
*
|
|
60
|
-
* // Retry a command 3 times with irregular intervals using exponential backoff
|
|
61
|
-
* const p = await retry(3, expBackoff(), () => SH`curl -s https://flipwrsi`);
|
|
62
|
-
*/
|
|
63
|
-
export function retry(count: number, a: string | number | typeof expBackoff | Function, b?: Function): Promise<any>;
|
|
64
|
-
/**
|
|
65
|
-
* This function reads the standard input (stdin) from the current process.
|
|
66
|
-
* @returns {Promise<string>}
|
|
67
|
-
* @example
|
|
68
|
-
* const content = await readIn();
|
|
69
|
-
*/
|
|
70
|
-
/**
|
|
71
|
-
* Read entire stdin as UTF-8. If stdin is a TTY, resolves to an empty string.
|
|
72
|
-
* @returns {Promise<string|undefined>}
|
|
74
|
+
* Reads entire stdin as UTF-8 string. Resolves to empty string if TTY (no pipe).
|
|
75
|
+
*
|
|
76
|
+
* @returns {Promise<string|undefined>} Stdin content or undefined if TTY.
|
|
77
|
+
* @example
|
|
78
|
+
* const input = await readIn(); // Use in piped scripts
|
|
73
79
|
*/
|
|
74
80
|
export function readIn(): Promise<string | undefined>;
|
|
75
81
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* @
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
};
|
|
82
|
+
* Prompts user for input on stdin with custom prompt.
|
|
83
|
+
*
|
|
84
|
+
* Supports aborting input collection.
|
|
85
|
+
*
|
|
86
|
+
* @param {string} prompt - Prompt text to display.
|
|
87
|
+
* @returns {AbortableInput} Object with input Promise and abort function.
|
|
88
|
+
* @example
|
|
89
|
+
* const {input, abort} = userIn('Enter name: ');
|
|
90
|
+
* const name = await input;
|
|
91
|
+
* // abort(); // Cancel anytime
|
|
92
|
+
*/
|
|
93
|
+
export function userIn(prompt: string): AbortableInput;
|
|
89
94
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Generates an exponential backoff time with a random jitter.
|
|
95
|
+
* Executes a callback in a new async context (fresh callstack).
|
|
96
|
+
*
|
|
97
|
+
* Useful for parallel operations without nesting.
|
|
98
|
+
*
|
|
99
|
+
* @param {() => Promise<any>} callback - Async function to execute.
|
|
100
|
+
* @returns {Promise<any>} Result of callback.
|
|
101
|
+
* @example
|
|
102
|
+
* const results = await within(async () => {
|
|
103
|
+
* return Promise.all([SH`sleep 1; echo 1`.run(), sleep(2)]);
|
|
104
|
+
* });
|
|
105
|
+
*/
|
|
106
|
+
export function within(callback: () => Promise<any>): Promise<any>;
|
|
107
|
+
/**
|
|
108
|
+
* Generator for exponential backoff delays with jitter.
|
|
106
109
|
*
|
|
107
110
|
* @generator
|
|
108
|
-
* @param {string} [max='60s'] -
|
|
109
|
-
* @param {string} [rand='100ms'] -
|
|
110
|
-
* @yields {number}
|
|
111
|
+
* @param {string} [max='60s'] - Max backoff duration.
|
|
112
|
+
* @param {string} [rand='100ms'] - Max jitter.
|
|
113
|
+
* @yields {number} Next backoff ms.
|
|
114
|
+
* @example
|
|
115
|
+
* const backoff = expBackoff();
|
|
116
|
+
* await sleep(backoff.next().value);
|
|
111
117
|
*/
|
|
112
118
|
export function expBackoff(max?: string, rand?: string): Generator<number, void, unknown>;
|
|
113
119
|
/**
|
|
114
|
-
* Parses
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* @param {string[]} [args
|
|
122
|
-
* @returns {ArgsObject} An object where:
|
|
123
|
-
* - If args is not passed, process.argv.slice(2) will be the default
|
|
124
|
-
* - Each key corresponds to an argument that starts with `--` or `-`,
|
|
125
|
-
* - The value is either the next argument or `true` if no value is provided,
|
|
126
|
-
* - The `_` property contains an array of unbound arguments.
|
|
127
|
-
*/
|
|
128
|
-
/**
|
|
129
|
-
* Parse command-line args into an object.
|
|
130
|
-
*
|
|
131
|
-
* Supported:
|
|
132
|
-
* - --key value, -k value (no grouped short flags)
|
|
133
|
-
* - Bare values collected under `_.`
|
|
134
|
-
* - Duplicate keys throw an error; keys without a following value become true.
|
|
135
|
-
*
|
|
136
|
-
* Not supported:
|
|
137
|
-
* - --key=value syntax
|
|
138
|
-
* - Grouped short flags like -abc
|
|
139
|
-
*
|
|
140
|
-
* @param {string[]} [args]
|
|
120
|
+
* Parses CLI arguments into an object.
|
|
121
|
+
*
|
|
122
|
+
* Supports --key value, -k value (no shorts grouped).
|
|
123
|
+
* Bares go to _[]. Duplicates error. No = syntax.
|
|
124
|
+
*
|
|
125
|
+
* Defaults to process.argv.slice(2).
|
|
126
|
+
*
|
|
127
|
+
* @param {string[]} [args=process.argv.slice(2)] - Args array.
|
|
141
128
|
* @returns {ArgsObject}
|
|
129
|
+
* @throws {Error} On invalid/dupe args.
|
|
130
|
+
* @example
|
|
131
|
+
* parseArgs(['--port', '8080', 'file.txt']); // { port: '8080', _: ['file.txt'] }
|
|
142
132
|
*/
|
|
143
133
|
export function parseArgs(args?: string[]): ArgsObject;
|
|
144
134
|
/**
|
|
145
|
-
* @typedef {Object.<string, string>} ArgsObject
|
|
146
|
-
* @property {string}
|
|
147
|
-
* @property {string[]
|
|
148
|
-
* @description Parsed parameters result.
|
|
149
|
-
*/
|
|
150
|
-
/**
|
|
151
|
-
* @typedef {Function} RejectCallback
|
|
152
|
-
* @param {Error} error - The error object passed to the callback.
|
|
153
|
-
*/
|
|
154
|
-
/**
|
|
155
|
-
* @typedef {Function} ResolveCallback
|
|
156
|
-
* @param {any} [param] - Optional callback any value
|
|
157
|
-
*/
|
|
158
|
-
/**
|
|
159
|
-
*
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
* @
|
|
163
|
-
*
|
|
164
|
-
* @
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
* @
|
|
168
|
-
*
|
|
169
|
-
* @
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
* @
|
|
176
|
-
* @
|
|
177
|
-
|
|
135
|
+
* @typedef {Object.<string, string>} ArgsObject
|
|
136
|
+
* @property {string[]} _ - Array of strings, unnamed parameters
|
|
137
|
+
* @property {string} [key: string] - Any string key maps to a string value
|
|
138
|
+
* @description Parsed parameters result.
|
|
139
|
+
*/
|
|
140
|
+
/**
|
|
141
|
+
* @typedef {Function} RejectCallback
|
|
142
|
+
* @param {Error} error - The error object passed to the callback.
|
|
143
|
+
*/
|
|
144
|
+
/**
|
|
145
|
+
* @typedef {Function} ResolveCallback
|
|
146
|
+
* @param {any} [param] - Optional callback any value
|
|
147
|
+
*/
|
|
148
|
+
/**
|
|
149
|
+
* @typedef {import('./SHDispatch.js').SHOptions} SHOptions
|
|
150
|
+
*/
|
|
151
|
+
/**
|
|
152
|
+
* @typedef {Object} AbortableInput
|
|
153
|
+
* @property {Promise<string|void>} input - Promise that resolves to user input or void if aborted.
|
|
154
|
+
* @property {() => void} abort - Function to abort input collection.
|
|
155
|
+
*/
|
|
156
|
+
/**
|
|
157
|
+
* @typedef {Object} ExpBackoffGenerator
|
|
158
|
+
* @generator
|
|
159
|
+
* @yields {number} Backoff time in ms.
|
|
160
|
+
*/
|
|
161
|
+
/**
|
|
162
|
+
* Utility to determine the JavaScript type of a value.
|
|
163
|
+
*
|
|
164
|
+
* @param {any} fn - Any value to inspect.
|
|
165
|
+
* @returns {string} The "real" object type name (e.g., 'Array', 'Promise') or primitive typeof.
|
|
166
|
+
* @example
|
|
167
|
+
* jsType([]); // 'Array'
|
|
168
|
+
* jsType(Promise.resolve()); // 'Promise'
|
|
169
|
+
*/
|
|
178
170
|
export function jsType(fn: any): string;
|
|
179
171
|
/**
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
* @param {any} o -
|
|
183
|
-
* @param {string} p -
|
|
184
|
-
* @returns {boolean}
|
|
185
|
-
*/
|
|
172
|
+
* Checks if an object has a specific own property (code-safe).
|
|
173
|
+
*
|
|
174
|
+
* @param {any} o - Object to examine.
|
|
175
|
+
* @param {string} p - Property name to check.
|
|
176
|
+
* @returns {boolean} True if the object has the own property.
|
|
177
|
+
*/
|
|
186
178
|
export function hasProp(o: any, p: string): boolean;
|
|
187
179
|
import Test from './Test.js';
|
|
188
180
|
import assert from 'node:assert';
|
|
189
181
|
import AsyncTracker from './AsyncTracker.js';
|
|
190
182
|
/**
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
|
|
183
|
+
* Escapes a string for safe use as a bash command-line argument.
|
|
184
|
+
*
|
|
185
|
+
* Handles quotes, backticks, $, newlines, etc.
|
|
186
|
+
*
|
|
187
|
+
* @param {string} x - Input string to escape.
|
|
188
|
+
* @returns {string} Bash-escaped string.
|
|
189
|
+
*/
|
|
196
190
|
export function bashEscape(x: string): string;
|
|
197
|
-
import SHDispatch from './SHDispatch.js';
|
|
198
191
|
export { Test, assert, AsyncTracker };
|
package/types/SHDispatch.d.ts
CHANGED
|
@@ -1,113 +1,90 @@
|
|
|
1
1
|
export default SHDispatch;
|
|
2
2
|
export type SpawnSyncResponse = {
|
|
3
3
|
/**
|
|
4
|
-
* - Exit code
|
|
4
|
+
* - Exit code (null if signal).
|
|
5
5
|
*/
|
|
6
6
|
status: number | null;
|
|
7
7
|
/**
|
|
8
|
-
* -
|
|
8
|
+
* - Terminating signal.
|
|
9
9
|
*/
|
|
10
10
|
signal: string | null;
|
|
11
11
|
/**
|
|
12
|
-
* - [stdin, stdout, stderr]
|
|
12
|
+
* - [stdin, stdout, stderr].
|
|
13
13
|
*/
|
|
14
14
|
output: (string | Buffer | null)[];
|
|
15
15
|
/**
|
|
16
|
-
* -
|
|
16
|
+
* - Process ID.
|
|
17
17
|
*/
|
|
18
18
|
pid: number;
|
|
19
19
|
/**
|
|
20
|
-
* -
|
|
20
|
+
* - Captured stdout.
|
|
21
21
|
*/
|
|
22
22
|
stdout: string | Buffer | null;
|
|
23
23
|
/**
|
|
24
|
-
* -
|
|
24
|
+
* - Captured stderr.
|
|
25
25
|
*/
|
|
26
26
|
stderr: string | Buffer | null;
|
|
27
27
|
};
|
|
28
|
-
export type SHOptions = {
|
|
29
|
-
/**
|
|
30
|
-
* - Current working directory of the child process.
|
|
31
|
-
*/
|
|
32
|
-
cwd?: string | undefined;
|
|
33
|
-
/**
|
|
34
|
-
* - Environment key-value pairs.
|
|
35
|
-
*/
|
|
36
|
-
env?: Object | undefined;
|
|
37
|
-
/**
|
|
38
|
-
* - Explicitly set the value of `argv[0]` sent to the child process.
|
|
39
|
-
*/
|
|
40
|
-
argv0?: string | undefined;
|
|
41
|
-
/**
|
|
42
|
-
* - If true, the child will be a process group leader.
|
|
43
|
-
*/
|
|
44
|
-
detached?: boolean | undefined;
|
|
45
|
-
/**
|
|
46
|
-
* - Sets the user identity of the process.
|
|
47
|
-
*/
|
|
48
|
-
uid?: number | undefined;
|
|
49
|
-
/**
|
|
50
|
-
* - Sets the group identity of the process.
|
|
51
|
-
*/
|
|
52
|
-
gid?: number | undefined;
|
|
53
|
-
/**
|
|
54
|
-
* - Child's stdio configuration.
|
|
55
|
-
*/
|
|
56
|
-
stdio?: number | "pipe" | "ignore" | "inherit" | StdioOption[] | undefined;
|
|
57
|
-
/**
|
|
58
|
-
* - If string or true, runs the command via that shell ('bash' if true).
|
|
59
|
-
*/
|
|
60
|
-
shell?: string | boolean | undefined;
|
|
61
|
-
/**
|
|
62
|
-
* - Milliseconds before sending SIGTERM (0 = no timeout).
|
|
63
|
-
*/
|
|
64
|
-
timeout?: number | undefined;
|
|
65
|
-
/**
|
|
66
|
-
* - Optional stdin payload for spawnSync. Use .run(payload) for async.
|
|
67
|
-
*/
|
|
68
|
-
input?: string | Uint8Array<ArrayBufferLike> | Buffer<ArrayBufferLike> | undefined;
|
|
69
|
-
};
|
|
70
28
|
export type StdioOption = ("pipe" | "ignore" | "inherit" | number);
|
|
71
29
|
export type StdioOptions = Array<StdioOption> | StdioOption;
|
|
72
30
|
/**
|
|
73
|
-
*
|
|
31
|
+
* High-level command dispatcher.
|
|
32
|
+
*
|
|
33
|
+
* Created by {@link SH`cmd`}; chain `.options()` then `.run()`.
|
|
74
34
|
*
|
|
75
|
-
*
|
|
76
|
-
* - cwd: process.cwd()
|
|
77
|
-
* - env: process.env
|
|
78
|
-
* - shell: 'bash' (string). If a string, that shell is used. If true, 'bash' is used. If false/undefined, no shell is used.
|
|
79
|
-
* - stdio: ['inherit', 'pipe', 'pipe'] — inherit stdin, capture stdout/stderr
|
|
80
|
-
* - timeout: 0 — no timeout
|
|
81
|
-
* - maxBuffer?: number — optional (bytes per stream). Passed through to SHExecute.
|
|
35
|
+
* Delegates to {@link SHExecute} for exec/timeout/buffer/kill.
|
|
82
36
|
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
37
|
+
* @example
|
|
38
|
+
* const dispatch = SH`ls -la`.options({ timeout: '2s' });
|
|
39
|
+
* const out = await dispatch.run();
|
|
86
40
|
*/
|
|
87
41
|
declare class SHDispatch {
|
|
88
42
|
/**
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
43
|
+
* @param {string} cmd - Command string.
|
|
44
|
+
* @param {Partial<typeof SHOptions>} [options] - Initial options.
|
|
45
|
+
* @param {string} [prefix] - Shell prefix (e.g., 'set -euo pipefail').
|
|
46
|
+
* @throws {Error} Invalid/empty cmd.
|
|
47
|
+
*/
|
|
48
|
+
constructor(cmd: string, options?: Partial<typeof SHOptions>, prefix?: string);
|
|
94
49
|
/**
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
50
|
+
* Updates options/prefix; resets to defaults + user overrides (non-cumulative).
|
|
51
|
+
*
|
|
52
|
+
* Strings for `stdio` → array fill.
|
|
53
|
+
*
|
|
54
|
+
* @param {Partial<typeof SHOptions>} [options] - New options.
|
|
55
|
+
* @param {string} [prefix] - New prefix.
|
|
56
|
+
* @returns {SHDispatch} Self for chaining.
|
|
57
|
+
*/
|
|
58
|
+
options(options?: Partial<typeof SHOptions>, prefix?: string): SHDispatch;
|
|
100
59
|
/**
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
60
|
+
* Async run: Captures stdout; rejects on error/timeout.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} [payload] - Stdin payload.
|
|
63
|
+
* @returns {Promise<string>} Stdout.
|
|
64
|
+
*/
|
|
104
65
|
run(payload?: string): Promise<string>;
|
|
105
66
|
/**
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
67
|
+
* Sync run: Full Node SpawnSyncReturns.
|
|
68
|
+
*
|
|
69
|
+
* Good for TTY takeovers (e.g., vim).
|
|
70
|
+
*
|
|
71
|
+
* @param {string} [payload] - Stdin payload.
|
|
72
|
+
* @returns {import('child_process').SpawnSyncReturns<Buffer>}
|
|
73
|
+
*/
|
|
74
|
+
runSync(payload?: string): import("child_process").SpawnSyncReturns<Buffer>;
|
|
75
|
+
/**
|
|
76
|
+
* Kills running process + children.
|
|
77
|
+
*
|
|
78
|
+
* @param {number | string} [signal='SIGTERM'] - Signal.
|
|
79
|
+
* @returns {Promise<number[]>} Killed PIDs.
|
|
80
|
+
*/
|
|
81
|
+
kill(signal?: number | string): Promise<number[]>;
|
|
112
82
|
#private;
|
|
113
83
|
}
|
|
84
|
+
declare namespace SHOptions {
|
|
85
|
+
let cwd: string;
|
|
86
|
+
let env: NodeJS.ProcessEnv;
|
|
87
|
+
let shell: string;
|
|
88
|
+
let stdio: string[];
|
|
89
|
+
let timeout: number;
|
|
90
|
+
}
|
package/types/SHExecute.d.ts
CHANGED
|
@@ -1,39 +1,60 @@
|
|
|
1
1
|
export default SHExecute;
|
|
2
|
+
export type SHExecuteOptions = any & {
|
|
3
|
+
maxBuffer?: number;
|
|
4
|
+
};
|
|
2
5
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
6
|
+
* @typedef {import('../SH.js').SHOptions & { maxBuffer?: number }} SHExecuteOptions
|
|
7
|
+
* @description Extended options for SHExecute: adds `maxBuffer` (bytes per stream, default 1MB).
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Low-level process executor for shell commands.
|
|
11
|
+
*
|
|
12
|
+
* Used internally by {@link SHDispatch}.
|
|
13
|
+
*
|
|
14
|
+
* Key features:
|
|
15
|
+
* - **Shell mode**: If `options.shell` is string/true, runs `${prefix}; ${command}` via shell ('bash' default).
|
|
16
|
+
* - **No-shell mode**: Uses `/usr/bin/env -S ${command}` for direct exec (ignores prefix).
|
|
17
|
+
* - **Rolling timeout**: `options.timeout` (ms/'2s'); resets on stdout/stderr data. SIGTERM on expiry.
|
|
18
|
+
* - **Buffering**: Captures stdout/stderr up to `maxBuffer` (1MB default); appends truncation markers.
|
|
19
|
+
* - **Payload**: `run(payload)` writes string to stdin (forces pipe).
|
|
20
|
+
* - **Detached**: If `options.detached`, resolves early (~1s) and unrefs.
|
|
21
|
+
* - **Kill**: Terminates process + children via pgrep.
|
|
5
22
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* - Buffering: Captures stdout/stderr up to maxBuffer bytes per stream (default 40 MiB). Appends "[stdout truncated]" / "[stderr truncated]" markers if exceeded.
|
|
10
|
-
* - Payload: Passing a payload writes it to stdin and forces stdin to be a pipe.
|
|
11
|
-
* - Detached mode: If options.detached is true, run() resolves to '' after ~1s and unrefs the process.
|
|
12
|
-
* - Kill support: kill(signal) attempts to terminate the process (and some children) and causes run() to reject with "Process killed (forced).".
|
|
23
|
+
* @example
|
|
24
|
+
* const exec = new SHExecute('ls', 'set -euo pipefail', { timeout: '5s' });
|
|
25
|
+
* const out = await exec.run();
|
|
13
26
|
*/
|
|
14
27
|
declare class SHExecute {
|
|
15
28
|
/**
|
|
16
|
-
* @param {string} command -
|
|
17
|
-
* @param {string} prefix
|
|
18
|
-
* @param {
|
|
29
|
+
* @param {string} command - Command to execute.
|
|
30
|
+
* @param {string} prefix - Shell prelude (e.g., 'set -euo pipefail'); ignored in no-shell.
|
|
31
|
+
* @param {SHExecuteOptions} [options] - Spawn options + maxBuffer/timeout.
|
|
19
32
|
*/
|
|
20
|
-
constructor(command: string, prefix: string, options?:
|
|
21
|
-
maxBuffer?: number;
|
|
22
|
-
});
|
|
33
|
+
constructor(command: string, prefix: string, options?: SHExecuteOptions);
|
|
23
34
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
35
|
+
* Synchronous execution.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} [payload] - Stdin data (forces pipe).
|
|
38
|
+
* @returns {import('child_process').SpawnSyncReturns<Buffer>}
|
|
39
|
+
* @throws {Error} Invalid payload type.
|
|
26
40
|
*/
|
|
27
|
-
runSync(payload?: string): import("child_process").SpawnSyncReturns<
|
|
41
|
+
runSync(payload?: string): import("child_process").SpawnSyncReturns<Buffer>;
|
|
28
42
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
43
|
+
* Asynchronous execution with buffering/timeout/kill.
|
|
44
|
+
*
|
|
45
|
+
* Resolves stdout (trimmed) on success; rejects on error/timeout/kill.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} [payload] - Stdin data (forces pipe).
|
|
48
|
+
* @returns {Promise<string>} Trimmed UTF-8 stdout (+ truncation marker if exceeded).
|
|
49
|
+
* @throws {Error} Command failure (incl. code, stderr), timeout, kill, spawn error.
|
|
31
50
|
*/
|
|
32
51
|
run(payload?: string): Promise<string>;
|
|
33
52
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* @
|
|
53
|
+
* Terminates process and its children (via pgrep).
|
|
54
|
+
*
|
|
55
|
+
* @param {number | string} [signal='SIGTERM'] - Signal to send.
|
|
56
|
+
* @returns {Promise<number[]>} Killed PIDs.
|
|
57
|
+
* @throws {Error} No process/PID.
|
|
37
58
|
*/
|
|
38
59
|
kill(signal?: number | string): Promise<number[]>;
|
|
39
60
|
#private;
|