@j-o-r/sh 0.0.2 → 0.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 +4 -10
- package/lib/sh.d.ts +222 -0
- package/lib/sh.js +1103 -0
- package/package.json +13 -8
- package/src/ProcessOutput.js +0 -113
- package/src/ProcessPromise.js +0 -341
- package/src/sh.js +0 -256
- package/src/utils.js +0 -422
package/README.md
CHANGED
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
Execute shell commands from JavaScript.
|
|
4
4
|
|
|
5
|
-

|
|
6
|
-
|
|
7
5
|
## Introduction
|
|
8
6
|
|
|
9
7
|
BETA
|
|
@@ -52,7 +50,7 @@ const ar = within(async () => {
|
|
|
52
50
|
]);
|
|
53
51
|
});
|
|
54
52
|
|
|
55
|
-
```
|
|
53
|
+
```
|
|
56
54
|
|
|
57
55
|
```javascript
|
|
58
56
|
const p = await retry(3, expBackoff(), () => SH`curl -s https://unreachable:`);
|
|
@@ -111,8 +109,8 @@ The `ProcessOutput` class collects output from the `ProcessPromise` shell proces
|
|
|
111
109
|
|
|
112
110
|
### Methods
|
|
113
111
|
|
|
114
|
-
- **toString()**:
|
|
115
|
-
- Returns a trimmed string combining `stderr` and `stdout`.
|
|
112
|
+
- **toString()**:
|
|
113
|
+
- Returns a trimmed string combining `stderr` and `stdout`.
|
|
116
114
|
- This method is automatically invoked by various JavaScript methods for output consolidation.
|
|
117
115
|
- **[inspect.custom]()**:
|
|
118
116
|
- Custom inspection method used for debugging.
|
|
@@ -120,13 +118,9 @@ The `ProcessOutput` class collects output from the `ProcessPromise` shell proces
|
|
|
120
118
|
|
|
121
119
|
### Examples
|
|
122
120
|
|
|
123
|
-
- Script for setting up
|
|
121
|
+
- Script for setting up a wokspace in [TMUX](./workspace.js)
|
|
124
122
|
- Take a look at the [test](./test/sh.js)
|
|
125
123
|
|
|
126
|
-
## Contributing
|
|
127
|
-
|
|
128
|
-
Contributions are welcome. Please submit issues and pull requests on our [GitHub repository](https://github.com/j-o-r/sh).
|
|
129
|
-
|
|
130
124
|
## License
|
|
131
125
|
|
|
132
126
|
This project is licensed under the Apache License, Version 2.0.
|
package/lib/sh.d.ts
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
+
export type resolver = Function;
|
|
4
|
+
export type rejecter = Function;
|
|
5
|
+
export type PromiseConstruct = Function;
|
|
6
|
+
export type StdioOption = ('pipe' | 'ignore' | 'inherit' | number);
|
|
7
|
+
export type StdioOptions = Array<StdioOption> | StdioOption;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new ProcessPromise object that represents a command to be executed.
|
|
10
|
+
*/
|
|
11
|
+
export type Shell = Function;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new ProcessPromise object that represents a command to be executed.
|
|
14
|
+
*
|
|
15
|
+
* @typedef {Function} Shell
|
|
16
|
+
* @property {function(Array, ...*): ProcessPromise} execute - The function to execute the command.
|
|
17
|
+
* @property {boolean} verbose - A property to control verbosity.
|
|
18
|
+
*
|
|
19
|
+
* @param {Array} pieces - An array of string literals from a template literal.
|
|
20
|
+
* @param {...*} args - The values to be interpolated into the string literals.
|
|
21
|
+
* @returns {ProcessPromise} A ProcessPromise object that represents the command.
|
|
22
|
+
* @throws {Error} Throws an error if any of the string literals in `pieces` is undefined.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const command = await SH`echo 'Hello, world!'`;
|
|
26
|
+
*/
|
|
27
|
+
/** @type {Shell & { (pieces: TemplateStringsArray, ...args: any[]): ProcessPromise }} */
|
|
28
|
+
export const SH: Shell & {
|
|
29
|
+
(pieces: TemplateStringsArray, ...args: any[]): ProcessPromise;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Change working directory
|
|
33
|
+
* @param {string} dir
|
|
34
|
+
*/
|
|
35
|
+
export function cd(dir: string): void;
|
|
36
|
+
export function expBackoff(max?: string, rand?: string): Generator<number, void, unknown>;
|
|
37
|
+
/**
|
|
38
|
+
* This function retries a command a specified number of times.
|
|
39
|
+
* @example
|
|
40
|
+
* // Retry a command 3 times
|
|
41
|
+
* const p = await retry(3, () => SH`curl -s https://flipwrsi`);
|
|
42
|
+
*
|
|
43
|
+
* // Retry a command 3 times with an interval of 1 second between each try
|
|
44
|
+
* const p = await retry(3, '1s', () => SH`curl -s https://flipwrsi`);
|
|
45
|
+
*
|
|
46
|
+
* // Retry a command 3 times with irregular intervals using exponential backoff
|
|
47
|
+
* const p = await retry(3, expBackoff(), () => SH`curl -s https://flipwrsi`);
|
|
48
|
+
*/
|
|
49
|
+
export function retry(count: any, a: any, b: any): Promise<any>;
|
|
50
|
+
/**
|
|
51
|
+
* This function pauses or "sleeps" code execution for a specified duration.
|
|
52
|
+
* @param {string|number} duration - The duration to pause execution for, e.g., '100ms' or '3s'.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
*
|
|
56
|
+
* const res = await Promise.all([
|
|
57
|
+
* SH`sleep 2; echo 2`, // Sleep for 2 seconds
|
|
58
|
+
* sleep(2), // Sleep for 2 seconds
|
|
59
|
+
* SH`sleep 3; echo 3` // Sleep for 3 seconds
|
|
60
|
+
* ]);
|
|
61
|
+
*/
|
|
62
|
+
export function sleep(duration: string | number): Promise<any>;
|
|
63
|
+
/**
|
|
64
|
+
* This function reads the standard input (stdin) for the current process.
|
|
65
|
+
* It is used to get piped content into a script.
|
|
66
|
+
* @example
|
|
67
|
+
* const content = await stdin();
|
|
68
|
+
*/
|
|
69
|
+
export function stdin(): Promise<string>;
|
|
70
|
+
/**
|
|
71
|
+
* Create a async context in an sync block
|
|
72
|
+
* @param {function} callback - async function
|
|
73
|
+
* @example
|
|
74
|
+
* const p = within(async () => {
|
|
75
|
+
* const res = await Promise.all([
|
|
76
|
+
* SH`sleep 1; echo 1`,
|
|
77
|
+
* SH`sleep 2; echo 2`,
|
|
78
|
+
* sleep(2),
|
|
79
|
+
* SH`sleep 3; echo 3`
|
|
80
|
+
* ]);
|
|
81
|
+
*/
|
|
82
|
+
export function within(callback: Function): any;
|
|
83
|
+
/**
|
|
84
|
+
* @typedef {Function} resolver
|
|
85
|
+
* @param {ProcessOutput} value
|
|
86
|
+
*/
|
|
87
|
+
/**
|
|
88
|
+
* @typedef {Function} rejecter
|
|
89
|
+
* @param {ProcessOutput} value
|
|
90
|
+
*/
|
|
91
|
+
/**
|
|
92
|
+
* @typedef {Function} PromiseConstruct
|
|
93
|
+
* @param {resolver} resolve
|
|
94
|
+
* @param {rejecter} reject
|
|
95
|
+
*/
|
|
96
|
+
/**
|
|
97
|
+
* @typedef {('pipe' | 'ignore' | 'inherit' | number)} StdioOption
|
|
98
|
+
* @description Defines the stdio configuration for each of the standard streams.
|
|
99
|
+
*
|
|
100
|
+
* - 'pipe' creates a pipe between the child process and the parent process.
|
|
101
|
+
* The parent end of the pipe is exposed as a property on the `ChildProcess` object.
|
|
102
|
+
* - 'ignore' indicates that the child process's corresponding stdio file descriptor will be ignored.
|
|
103
|
+
* - 'inherit' passes the corresponding stdio stream to/from the child process.
|
|
104
|
+
* - Stream object to be used for the stdio stream.
|
|
105
|
+
* - Positive integer representing a file descriptor to be used for the stdio stream.
|
|
106
|
+
*/
|
|
107
|
+
/**
|
|
108
|
+
* @typedef {Array<StdioOption>|StdioOption} StdioOptions
|
|
109
|
+
* @description
|
|
110
|
+
* Configures the stdio streams for the child process. This can be an array or a single StdioOption.
|
|
111
|
+
*
|
|
112
|
+
* Array Form: Specify the configuration for [stdin, stdout, stderr].
|
|
113
|
+
* - If array length is more than 3, additional positions correspond to extra streams.
|
|
114
|
+
* Single Value: This value will be applied to stdin, stdout, and stderr.
|
|
115
|
+
*
|
|
116
|
+
* Examples:
|
|
117
|
+
* - ['pipe', 'pipe', 'ignore']: Pipe stdin and stdout, ignore stderr.
|
|
118
|
+
* - 'inherit': Inherit all stdio streams from the parent.
|
|
119
|
+
*/
|
|
120
|
+
/**
|
|
121
|
+
* class extends promise
|
|
122
|
+
*/
|
|
123
|
+
declare class ProcessPromise extends Promise<any> {
|
|
124
|
+
/**
|
|
125
|
+
* @param {PromiseConstruct} p - A function that takes two arguments, resolve and reject.
|
|
126
|
+
*/
|
|
127
|
+
constructor(p: PromiseConstruct);
|
|
128
|
+
/**
|
|
129
|
+
* Set the environment
|
|
130
|
+
* and the
|
|
131
|
+
* @param {string} cmd - Command to execute
|
|
132
|
+
* @param {string} from - Position in the codfe where this is triggred from
|
|
133
|
+
* @param {function} resolve - Promise resolve method
|
|
134
|
+
* @param {function} reject - Reject method
|
|
135
|
+
* @param {object} options - Settings (options default)
|
|
136
|
+
*/
|
|
137
|
+
_bind(cmd: string, from: string, resolve: Function, reject: Function, options: object): void;
|
|
138
|
+
/**
|
|
139
|
+
* Run the promise
|
|
140
|
+
*/
|
|
141
|
+
run(): this;
|
|
142
|
+
child: import("child_process").ChildProcessWithoutNullStreams & import("child_process").ChildProcessByStdio<import("stream").Writable, import("stream").Readable, import("stream").Readable> & import("child_process").ChildProcessByStdio<import("stream").Writable, import("stream").Readable, null> & import("child_process").ChildProcessByStdio<import("stream").Writable, null, import("stream").Readable> & import("child_process").ChildProcessByStdio<null, import("stream").Readable, import("stream").Readable> & import("child_process").ChildProcessByStdio<import("stream").Writable, null, null> & import("child_process").ChildProcessByStdio<null, import("stream").Readable, null> & import("child_process").ChildProcessByStdio<null, null, import("stream").Readable> & import("child_process").ChildProcessByStdio<null, null, null> & import("child_process").ChildProcess;
|
|
143
|
+
/**
|
|
144
|
+
* stdin child stream
|
|
145
|
+
* @retruns {Writeable}
|
|
146
|
+
*/
|
|
147
|
+
get stdin(): null;
|
|
148
|
+
/**
|
|
149
|
+
* stdout child stream
|
|
150
|
+
* @retruns {Readable}
|
|
151
|
+
*/
|
|
152
|
+
get stdout(): null;
|
|
153
|
+
/**
|
|
154
|
+
* stderr child stream
|
|
155
|
+
* @retruns {Readable}
|
|
156
|
+
*/
|
|
157
|
+
get stderr(): null;
|
|
158
|
+
/**
|
|
159
|
+
* process exit code
|
|
160
|
+
* @returns {Promise<number>}
|
|
161
|
+
*/
|
|
162
|
+
get exitCode(): Promise<number>;
|
|
163
|
+
catch(onrejected: any): Promise<any>;
|
|
164
|
+
/**
|
|
165
|
+
* Pipe the output to the input to the next Promise
|
|
166
|
+
* @example
|
|
167
|
+
* const res = await SH`ls -FLa`.pipe(SH`grep package.json`);
|
|
168
|
+
*/
|
|
169
|
+
pipe(dest: any): ProcessPromise;
|
|
170
|
+
/**
|
|
171
|
+
* @private
|
|
172
|
+
* Set a postrun action, internal use only
|
|
173
|
+
* @param {function} f
|
|
174
|
+
*/
|
|
175
|
+
private set _postrun(f);
|
|
176
|
+
/**
|
|
177
|
+
* Send a KILL signal to the child process
|
|
178
|
+
* @returns {Promise<number[]>} the pid numbers that has been killed
|
|
179
|
+
*/
|
|
180
|
+
kill(signal?: string): Promise<number[]>;
|
|
181
|
+
stdio(stdin: any, stdout?: string, stderr?: string): this;
|
|
182
|
+
/**
|
|
183
|
+
* Do not throw
|
|
184
|
+
*/
|
|
185
|
+
nothrow(): this;
|
|
186
|
+
/**
|
|
187
|
+
* supress log output
|
|
188
|
+
* SH.verbose = false; does the same
|
|
189
|
+
*/
|
|
190
|
+
quiet(): this;
|
|
191
|
+
/**
|
|
192
|
+
* Show log output in the console
|
|
193
|
+
*/
|
|
194
|
+
verbose(): this;
|
|
195
|
+
_quiet: boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Set a timeout to kill a process
|
|
198
|
+
*
|
|
199
|
+
* @param {string} d - 10s, 1000ms
|
|
200
|
+
* @param {string} [signal] - default "SIGTERM" Signal to send to kill the proces
|
|
201
|
+
*/
|
|
202
|
+
timeout(d: string, signal?: string): this;
|
|
203
|
+
_timeout: number;
|
|
204
|
+
_timeoutSignal: string;
|
|
205
|
+
/**
|
|
206
|
+
* stop execution for the next step
|
|
207
|
+
*/
|
|
208
|
+
halt(): this;
|
|
209
|
+
/**
|
|
210
|
+
* @private
|
|
211
|
+
* Set a prerun action, internal use only
|
|
212
|
+
* @param {function} f
|
|
213
|
+
*/
|
|
214
|
+
private set _prerun(f);
|
|
215
|
+
/**
|
|
216
|
+
* Is this promise halted?
|
|
217
|
+
* @returns {boolean}
|
|
218
|
+
*/
|
|
219
|
+
get isHalted(): boolean;
|
|
220
|
+
#private;
|
|
221
|
+
}
|
|
222
|
+
export {};
|