@j-o-r/sh 1.0.2 → 1.0.4
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 +8 -1
- package/lib/SH.js +296 -0
- package/lib/SHDispatch.js +151 -0
- package/lib/SHExecute.js +154 -0
- package/package.json +7 -10
- package/{lib/sh.d.ts → types/SH.d.ts} +28 -101
- package/types/SHDispatch.d.ts +82 -0
- package/types/SHExecute.d.ts +23 -0
- package/lib/sh.js +0 -538
|
@@ -1,60 +1,3 @@
|
|
|
1
|
-
export type SpawnSyncResponse = {
|
|
2
|
-
/**
|
|
3
|
-
* - The exit code of the child process. A value of `0` indicates success.
|
|
4
|
-
*/
|
|
5
|
-
status: number;
|
|
6
|
-
/**
|
|
7
|
-
* - The signal used to terminate the process, if any.
|
|
8
|
-
*/
|
|
9
|
-
signal: Buffer | null;
|
|
10
|
-
/**
|
|
11
|
-
* - An array containing the standard output and standard error of the child process.
|
|
12
|
-
*/
|
|
13
|
-
output: Array<string | null>;
|
|
14
|
-
/**
|
|
15
|
-
* - The process ID of the child process.
|
|
16
|
-
*/
|
|
17
|
-
pid: number;
|
|
18
|
-
/**
|
|
19
|
-
* - The standard output of the child process.
|
|
20
|
-
*/
|
|
21
|
-
stdout: Buffer | null;
|
|
22
|
-
/**
|
|
23
|
-
* - The standard error of the child process.
|
|
24
|
-
*/
|
|
25
|
-
stderr: Buffer | null;
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* Default options for the execution environment.
|
|
29
|
-
*/
|
|
30
|
-
export type SHOptions = {
|
|
31
|
-
/**
|
|
32
|
-
* - The current working directory.
|
|
33
|
-
*/
|
|
34
|
-
cwd?: string;
|
|
35
|
-
/**
|
|
36
|
-
* - The environment variables.
|
|
37
|
-
*/
|
|
38
|
-
env?: NodeJS.ProcessEnv;
|
|
39
|
-
/**
|
|
40
|
-
* - The shell to use for execution.
|
|
41
|
-
*/
|
|
42
|
-
shell?: string;
|
|
43
|
-
/**
|
|
44
|
-
* - The prefix commands to ensure a safe execution environment.
|
|
45
|
-
*/
|
|
46
|
-
prefix?: string;
|
|
47
|
-
/**
|
|
48
|
-
* - The stdio configuration.
|
|
49
|
-
*/
|
|
50
|
-
stdio?: StdioOptions | StdioOption;
|
|
51
|
-
/**
|
|
52
|
-
* - default 20000: a timeout error is triggerd when a process execution time is exceeded, 0 is no timeout
|
|
53
|
-
*/
|
|
54
|
-
timeout?: number;
|
|
55
|
-
};
|
|
56
|
-
export type StdioOption = ('pipe' | 'ignore' | 'inherit' | number);
|
|
57
|
-
export type StdioOptions = Array<StdioOption> | StdioOption;
|
|
58
1
|
/**
|
|
59
2
|
* Creates a new SHDispatch object that represents a command to be executed.
|
|
60
3
|
*/
|
|
@@ -63,26 +6,28 @@ export type Shell = Function;
|
|
|
63
6
|
export const SH: Shell & {
|
|
64
7
|
(pieces: TemplateStringsArray, ...args: any): SHDispatch;
|
|
65
8
|
};
|
|
9
|
+
/**
|
|
10
|
+
* Splits a command string into an array of arguments, handling quoted strings.
|
|
11
|
+
* This fixes a problem when a command is like this: SH`${command}`
|
|
12
|
+
*
|
|
13
|
+
* @param {string} command - The command string to split.
|
|
14
|
+
* @returns {string[]} - The array of command arguments.
|
|
15
|
+
*/
|
|
16
|
+
export function args(command: string): string[];
|
|
66
17
|
/**
|
|
67
18
|
* Change working directory
|
|
68
19
|
* @param {string} dir
|
|
69
20
|
*/
|
|
70
21
|
export function cd(dir: string): void;
|
|
71
22
|
/**
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
* @param {string} [max='60s'] - The maximum backoff time in a human-readable format (e.g., '60s' for 60 seconds).
|
|
76
|
-
* @param {string} [rand='100ms'] - The maximum random jitter time in a human-readable format (e.g., '100ms' for 100 milliseconds).
|
|
77
|
-
* @yields {number} The backoff time in milliseconds.
|
|
78
|
-
*/
|
|
79
|
-
export function expBackoff(max?: string, rand?: string): Generator<number, void, unknown>;
|
|
80
|
-
/**
|
|
81
|
-
* This function reads the standard input (stdin) from the current process.
|
|
23
|
+
* This function pauses or "sleeps" code execution for a specified duration.
|
|
24
|
+
* @param {string|number} duration - The duration to pause execution for, e.g., '100ms' or '3s'.
|
|
25
|
+
*
|
|
82
26
|
* @example
|
|
83
|
-
*
|
|
27
|
+
*
|
|
28
|
+
* const res = await sleep('5s');
|
|
84
29
|
*/
|
|
85
|
-
export function
|
|
30
|
+
export function sleep(duration: string | number): Promise<any>;
|
|
86
31
|
/**
|
|
87
32
|
* Retries a given asynchronous function a specified number of times with optional delays between attempts.
|
|
88
33
|
*
|
|
@@ -102,16 +47,13 @@ export function readIn(): Promise<string>;
|
|
|
102
47
|
* // Retry a command 3 times with irregular intervals using exponential backoff
|
|
103
48
|
* const p = await retry(3, expBackoff(), () => SH`curl -s https://flipwrsi`);
|
|
104
49
|
*/
|
|
105
|
-
export function retry(count: number, a: string | typeof expBackoff | Function, b?: Function): Promise<any>;
|
|
50
|
+
export function retry(count: number, a: string | typeof expBackoff | Function, b?: Function | undefined): Promise<any>;
|
|
106
51
|
/**
|
|
107
|
-
* This function
|
|
108
|
-
* @param {string|number} duration - The duration to pause execution for, e.g., '100ms' or '3s'.
|
|
109
|
-
*
|
|
52
|
+
* This function reads the standard input (stdin) from the current process.
|
|
110
53
|
* @example
|
|
111
|
-
*
|
|
112
|
-
* const res = await sleep('5s');
|
|
54
|
+
* const content = await stdin();
|
|
113
55
|
*/
|
|
114
|
-
export function
|
|
56
|
+
export function readIn(): Promise<string>;
|
|
115
57
|
/**
|
|
116
58
|
* Create a async context in an sync block
|
|
117
59
|
* @param {function} callback - async function
|
|
@@ -125,28 +67,13 @@ export function sleep(duration: string | number): Promise<any>;
|
|
|
125
67
|
* ]);
|
|
126
68
|
*/
|
|
127
69
|
export function within(callback: Function): void;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* @param {string} [payload]
|
|
140
|
-
* @returns {Promise<string>}
|
|
141
|
-
*/
|
|
142
|
-
run(payload?: string): Promise<string>;
|
|
143
|
-
/**
|
|
144
|
-
* Works for screen takeovers like editors
|
|
145
|
-
* @param {string} [payload]
|
|
146
|
-
* @returns {SpawnSyncResponse}
|
|
147
|
-
*/
|
|
148
|
-
runSync(payload?: string): SpawnSyncResponse;
|
|
149
|
-
kill(): Promise<void>;
|
|
150
|
-
#private;
|
|
151
|
-
}
|
|
152
|
-
export {};
|
|
70
|
+
/**
|
|
71
|
+
* Generates an exponential backoff time with a random jitter.
|
|
72
|
+
*
|
|
73
|
+
* @generator
|
|
74
|
+
* @param {string} [max='60s'] - The maximum backoff time in a human-readable format (e.g., '60s' for 60 seconds).
|
|
75
|
+
* @param {string} [rand='100ms'] - The maximum random jitter time in a human-readable format (e.g., '100ms' for 100 milliseconds).
|
|
76
|
+
* @yields {number} The backoff time in milliseconds.
|
|
77
|
+
*/
|
|
78
|
+
export function expBackoff(max?: string | undefined, rand?: string | undefined): Generator<number, void, unknown>;
|
|
79
|
+
import SHDispatch from './SHDispatch.js';
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export default SHDispatch;
|
|
2
|
+
export type SpawnSyncResponse = {
|
|
3
|
+
/**
|
|
4
|
+
* - The exit code of the child process. A value of `0` indicates success.
|
|
5
|
+
*/
|
|
6
|
+
status: number;
|
|
7
|
+
/**
|
|
8
|
+
* - The signal used to terminate the process, if any.
|
|
9
|
+
*/
|
|
10
|
+
signal: Buffer | null;
|
|
11
|
+
/**
|
|
12
|
+
* - An array containing the standard output and standard error of the child process.
|
|
13
|
+
*/
|
|
14
|
+
output: Array<string | null>;
|
|
15
|
+
/**
|
|
16
|
+
* - The process ID of the child process.
|
|
17
|
+
*/
|
|
18
|
+
pid: number;
|
|
19
|
+
/**
|
|
20
|
+
* - The standard output of the child process.
|
|
21
|
+
*/
|
|
22
|
+
stdout: Buffer | null;
|
|
23
|
+
/**
|
|
24
|
+
* - The standard error of the child process.
|
|
25
|
+
*/
|
|
26
|
+
stderr: Buffer | null;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Default options for the execution environment.
|
|
30
|
+
*/
|
|
31
|
+
export type SHOptions = {
|
|
32
|
+
/**
|
|
33
|
+
* - The current working directory.
|
|
34
|
+
*/
|
|
35
|
+
cwd?: string | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* - The environment variables.
|
|
38
|
+
*/
|
|
39
|
+
env?: any;
|
|
40
|
+
/**
|
|
41
|
+
* - The shell to use for execution.
|
|
42
|
+
*/
|
|
43
|
+
shell?: string | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* - The prefix commands to ensure a safe execution environment. e.g: prefix: 'set -euo pipefail;/usr/bin/env',
|
|
46
|
+
*/
|
|
47
|
+
prefix?: string | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* - The stdio configuration.
|
|
50
|
+
*/
|
|
51
|
+
stdio?: number | "pipe" | "ignore" | "inherit" | StdioOption[] | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* - default 20000: a timeout error is triggerd when a process execution time is exceeded, 0 is no timeout
|
|
54
|
+
*/
|
|
55
|
+
timeout?: number | undefined;
|
|
56
|
+
};
|
|
57
|
+
export type StdioOption = ("pipe" | "ignore" | "inherit" | number);
|
|
58
|
+
export type StdioOptions = Array<StdioOption> | StdioOption;
|
|
59
|
+
declare class SHDispatch {
|
|
60
|
+
/**
|
|
61
|
+
* @param {string} cmd - cmd to execute
|
|
62
|
+
*/
|
|
63
|
+
constructor(cmd: string);
|
|
64
|
+
/**
|
|
65
|
+
* @param {SHOptions} options
|
|
66
|
+
* @returns {SHDispatch}
|
|
67
|
+
*/
|
|
68
|
+
options(options: SHOptions): SHDispatch;
|
|
69
|
+
/**
|
|
70
|
+
* @param {string} [payload]
|
|
71
|
+
* @returns {Promise<string>}
|
|
72
|
+
*/
|
|
73
|
+
run(payload?: string | undefined): Promise<string>;
|
|
74
|
+
/**
|
|
75
|
+
* Works for screen takeovers like editors
|
|
76
|
+
* @param {string} [payload]
|
|
77
|
+
* @returns {SpawnSyncResponse}
|
|
78
|
+
*/
|
|
79
|
+
runSync(payload?: string | undefined): SpawnSyncResponse;
|
|
80
|
+
kill(): Promise<void>;
|
|
81
|
+
#private;
|
|
82
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export default SHExecute;
|
|
2
|
+
declare class SHExecute {
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} command - linux command to be executed
|
|
5
|
+
* @param {import('./SHDispatch').SHOptions} [options] - ChildProcess options
|
|
6
|
+
*/
|
|
7
|
+
constructor(command: string, options?: any);
|
|
8
|
+
/**
|
|
9
|
+
* @param {string} [payload] - data to write
|
|
10
|
+
* @retuns {Promise<object>}
|
|
11
|
+
*/
|
|
12
|
+
runSync(payload?: string | undefined): any;
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} [payload] - data to write
|
|
15
|
+
* @retuns {Promise<string>}
|
|
16
|
+
*/
|
|
17
|
+
run(payload?: string | undefined): Promise<any>;
|
|
18
|
+
/**
|
|
19
|
+
* @returns {Promise<number[]>}
|
|
20
|
+
*/
|
|
21
|
+
kill(signal?: string): Promise<number[]>;
|
|
22
|
+
#private;
|
|
23
|
+
}
|