@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.
@@ -1,39 +1,60 @@
1
1
  export default SHExecute;
2
+ export type SHExecuteOptions = any & {
3
+ maxBuffer?: number;
4
+ };
2
5
  /**
3
- * SHExecute
4
- * Low-level process runner used by SHDispatch.
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
- * Features:
7
- * - Optional shell execution: if options.shell is a string (e.g., 'bash') or true, runs `${prefix}; ${command}` via that shell; otherwise runs without a shell using `/usr/bin/env -S`.
8
- * - Timeout: options.timeout may be a number (ms) or string like '1500ms' or '2s'. On timeout, run() rejects with a descriptive error.
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 - linux command to be executed
17
- * @param {string} prefix - command prefix (shell prelude, e.g. 'set -euo pipefail')
18
- * @param {import('child_process').SpawnOptions & { maxBuffer?: number }} options
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?: import("child_process").SpawnOptions & {
21
- maxBuffer?: number;
22
- });
33
+ constructor(command: string, prefix: string, options?: SHExecuteOptions);
23
34
  /**
24
- * @param {string} [payload] - data to write
25
- * @returns {import('child_process').SpawnSyncReturns}
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<any>;
41
+ runSync(payload?: string): import("child_process").SpawnSyncReturns<Buffer>;
28
42
  /**
29
- * @param {string} [payload] - data to write
30
- * @returns {Promise<string>}
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
- * Kill this process and possible child processes
35
- * @param {number | string} signal - kill signal
36
- * @returns {Promise<number[]>}
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 = (() => Promise<any>);
3
- export type testDefinition = {
2
+ export type AsyncFunction = () => Promise<any>;
3
+ export type TestDefinition = {
4
+ /**
5
+ * - Test name/description.
6
+ */
4
7
  description: string;
5
8
  /**
6
- * - syc/ async function
9
+ * - Sync/async test function.
7
10
  */
8
11
  callback: Function | AsyncFunction;
9
12
  };
10
- export type testReport = {
13
+ export type TestReport = {
14
+ /**
15
+ * - Test description.
16
+ */
11
17
  description: string;
12
18
  /**
13
- * - start time in MS
19
+ * - Execution duration (ms).
14
20
  */
15
21
  duration: number;
16
22
  /**
17
- * - Has it been called?
23
+ * - Whether the test ran.
18
24
  */
19
25
  executed: boolean;
20
26
  };
21
- export type Report = {
27
+ export type TestReportSummary = {
28
+ /**
29
+ * - Total tests defined.
30
+ */
22
31
  tests: number;
23
32
  /**
24
- * - start time in MS
33
+ * - Total execution time (ms).
25
34
  */
26
35
  duration: number;
27
36
  /**
28
- * - number of errors
37
+ * - Number of failures.
29
38
  */
30
39
  errors: number;
31
40
  /**
32
- * - number of tests executed
41
+ * - Number of tests run.
33
42
  */
34
43
  executed: number;
35
44
  };
36
45
  declare class Test {
37
46
  /**
38
- * @param {boolean} [quiet] - does not output a report when true, default `false`
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
- * Set the timeout when a synced function is called.
43
- * This settles async code used in a sync function
44
- * and give some time to catch errors (#HACK)
45
- * @param {number} timeout - in MS, default 50
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
- * @param {string} description
51
- * @param {Function|AsyncFunction} callback - sync / async function
52
- * @throws Error when conditions are not met
53
- * @returns {Test}
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
- * Execute tests
58
- * @param {number[]} [execute] - limit the execution tests
59
- * @returns {Promise<Report>}
60
- */
61
- run(execute?: number[]): Promise<Report>;
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
- * Empty tests
65
- */
97
+ * Resets all tests, reports, errors, tracker.
98
+ *
99
+ * @example
100
+ * t.reset();
101
+ */
66
102
  reset(): void;
67
103
  #private;
68
104
  }