@j-o-r/sh 1.1.28 → 1.1.31
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 +145 -151
- package/TODO.md +8 -0
- package/lib/AsyncTracker.js +126 -107
- package/lib/SH.js +281 -224
- package/lib/SHDispatch.js +122 -94
- package/lib/SHExecute.js +83 -41
- package/lib/Test.js +153 -123
- package/package.json +2 -2
- package/types/AsyncTracker.d.ts +65 -31
- package/types/SH.d.ts +190 -168
- package/types/SHDispatch.d.ts +81 -66
- package/types/SHExecute.d.ts +44 -23
- package/types/Test.d.ts +66 -30
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;
|
package/types/Test.d.ts
CHANGED
|
@@ -1,68 +1,104 @@
|
|
|
1
1
|
export default Test;
|
|
2
|
-
export type AsyncFunction = (
|
|
3
|
-
export type
|
|
2
|
+
export type AsyncFunction = () => Promise<any>;
|
|
3
|
+
export type TestDefinition = {
|
|
4
|
+
/**
|
|
5
|
+
* - Test name/description.
|
|
6
|
+
*/
|
|
4
7
|
description: string;
|
|
5
8
|
/**
|
|
6
|
-
* -
|
|
9
|
+
* - Sync/async test function.
|
|
7
10
|
*/
|
|
8
11
|
callback: Function | AsyncFunction;
|
|
9
12
|
};
|
|
10
|
-
export type
|
|
13
|
+
export type TestReport = {
|
|
14
|
+
/**
|
|
15
|
+
* - Test description.
|
|
16
|
+
*/
|
|
11
17
|
description: string;
|
|
12
18
|
/**
|
|
13
|
-
* -
|
|
19
|
+
* - Execution duration (ms).
|
|
14
20
|
*/
|
|
15
21
|
duration: number;
|
|
16
22
|
/**
|
|
17
|
-
* -
|
|
23
|
+
* - Whether the test ran.
|
|
18
24
|
*/
|
|
19
25
|
executed: boolean;
|
|
20
26
|
};
|
|
21
|
-
export type
|
|
27
|
+
export type TestReportSummary = {
|
|
28
|
+
/**
|
|
29
|
+
* - Total tests defined.
|
|
30
|
+
*/
|
|
22
31
|
tests: number;
|
|
23
32
|
/**
|
|
24
|
-
* -
|
|
33
|
+
* - Total execution time (ms).
|
|
25
34
|
*/
|
|
26
35
|
duration: number;
|
|
27
36
|
/**
|
|
28
|
-
* -
|
|
37
|
+
* - Number of failures.
|
|
29
38
|
*/
|
|
30
39
|
errors: number;
|
|
31
40
|
/**
|
|
32
|
-
* -
|
|
41
|
+
* - Number of tests run.
|
|
33
42
|
*/
|
|
34
43
|
executed: number;
|
|
35
44
|
};
|
|
36
45
|
declare class Test {
|
|
37
46
|
/**
|
|
38
|
-
|
|
39
|
-
|
|
47
|
+
* Creates a test runner instance.
|
|
48
|
+
*
|
|
49
|
+
* Tracks tests, errors, unresolved promises via {@link AsyncTracker}.
|
|
50
|
+
* Supports sync/async callbacks; detects global errors.
|
|
51
|
+
*
|
|
52
|
+
* @param {boolean} [quiet=false] - Suppress console reports.
|
|
53
|
+
* @example
|
|
54
|
+
* const t = new Test();
|
|
55
|
+
* t.add('basic', () => { throw new Error('fail'); });
|
|
56
|
+
* const report = await t.run();
|
|
57
|
+
*/
|
|
40
58
|
constructor(quiet?: boolean);
|
|
41
59
|
/**
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
60
|
+
* Sets timeout for settling async in sync tests (hack for late errors).
|
|
61
|
+
*
|
|
62
|
+
* @param {number} timeout - Timeout (ms); default 50.
|
|
63
|
+
* @example
|
|
64
|
+
* t.syncTimeout(100);
|
|
65
|
+
*/
|
|
47
66
|
syncTimeout(timeout: number): void;
|
|
48
67
|
/**
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
68
|
+
* Adds a test case.
|
|
69
|
+
*
|
|
70
|
+
* @param {string} description - Test name.
|
|
71
|
+
* @param {Function | AsyncFunction} callback - Test function.
|
|
72
|
+
* @returns {Test} Self for chaining.
|
|
73
|
+
* @throws {Error} Invalid description (non-string) or callback (not function).
|
|
74
|
+
* @example
|
|
75
|
+
* t.add('check 1+1', () => expect(1+1).toBe(2));
|
|
76
|
+
*/
|
|
55
77
|
add(description: string, callback: Function | AsyncFunction): Test;
|
|
56
78
|
/**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
79
|
+
* Runs tests (all or selected); returns summary.
|
|
80
|
+
*
|
|
81
|
+
* Prints progress/errors; checks unresolved promises post-run.
|
|
82
|
+
*
|
|
83
|
+
* @param {number[]} [execute] - Indices of tests to run (default: all).
|
|
84
|
+
* @returns {Promise<TestReportSummary>} Summary stats.
|
|
85
|
+
* @example
|
|
86
|
+
* await t.run([0, 2]); // Run tests 0 and 2
|
|
87
|
+
*/
|
|
88
|
+
run(execute?: number[]): Promise<TestReportSummary>;
|
|
89
|
+
/**
|
|
90
|
+
* Prints unresolved promises report (if any).
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* t.unresolved();
|
|
94
|
+
*/
|
|
62
95
|
unresolved(): void;
|
|
63
96
|
/**
|
|
64
|
-
|
|
65
|
-
|
|
97
|
+
* Resets all tests, reports, errors, tracker.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* t.reset();
|
|
101
|
+
*/
|
|
66
102
|
reset(): void;
|
|
67
103
|
#private;
|
|
68
104
|
}
|