@j-o-r/sh 0.0.3 → 1.0.0
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 +55 -63
- package/lib/sh.d.ts +112 -39
- package/lib/sh.js +354 -106
- package/package.json +4 -3
- package/lib/ProcessOutput.d.ts +0 -44
- package/lib/ProcessOutput.js +0 -113
- package/lib/ProcessPromise.d.ts +0 -147
- package/lib/ProcessPromise.js +0 -354
- package/lib/utils.d.ts +0 -20
- package/lib/utils.js +0 -422
- package/release/j-o-r-sh-0.0.3.tgz +0 -0
package/lib/ProcessOutput.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
// Copyright 2021 Google LLC
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
// Original Source: zx
|
|
17
|
-
// Link to Original Source: https://github.com/google/zx
|
|
18
|
-
// Reason for Using This Code:
|
|
19
|
-
// The core functionality of this code is highly beneficial. However, certain parts of the original code
|
|
20
|
-
// were overwriting the global namespace with core libraries and variables. This was causing conflicts with
|
|
21
|
-
// other packages (for instance, fetch) and introducing unexpected elements into my code base.
|
|
22
|
-
// Changes Made:
|
|
23
|
-
// - The code has been or is being reformatted to comply with ES2020 standards.
|
|
24
|
-
// - Some methods were added and existing ones were modified to enhance usability.
|
|
25
|
-
// - The namespace has been changed from '$' to 'SH'.
|
|
26
|
-
// Modified by: jorrit.duin+sh[AT]gmail.com
|
|
27
|
-
|
|
28
|
-
import { inspect } from 'node:util';
|
|
29
|
-
import { exitCodeInfo} from './utils.js';
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* The ProcessPromise returns a ProcessOutput even when it fails, by rejecting it.
|
|
33
|
-
* The extension of the Error class is implemented to ensure compatibility when an Error is expected upon rejection.
|
|
34
|
-
*/
|
|
35
|
-
class ProcessOutput extends Error {
|
|
36
|
-
#code = 0;
|
|
37
|
-
#signal;
|
|
38
|
-
#stdout = '';
|
|
39
|
-
#stderr = '';
|
|
40
|
-
#combined = '';
|
|
41
|
-
/**
|
|
42
|
-
* @param {number} code - exit code
|
|
43
|
-
* @param {string} signal - SIGTERM ...
|
|
44
|
-
* @param {string} stdout - std reponse string
|
|
45
|
-
* @param {string} stderr - error reponse string
|
|
46
|
-
* @param {string} combined - stderr + stdout
|
|
47
|
-
* @param {string} message - Error message
|
|
48
|
-
*/
|
|
49
|
-
constructor(code, signal, stdout = '', stderr = '', combined = '', message = '') {
|
|
50
|
-
super(message);
|
|
51
|
-
this.#code = code;
|
|
52
|
-
this.#signal = signal;
|
|
53
|
-
this.#stdout = stdout;
|
|
54
|
-
this.#stderr = stderr;
|
|
55
|
-
this.#combined = combined;
|
|
56
|
-
this.name = 'ProcessOutput';
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* This string represents the standard output (stdout) from the child process.
|
|
60
|
-
* @returns {string}
|
|
61
|
-
*/
|
|
62
|
-
get stdout() {
|
|
63
|
-
return this.#stdout;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* This string represents the error output (stderr) from the child process.
|
|
67
|
-
* @returns {string}
|
|
68
|
-
*/
|
|
69
|
-
get stderr() {
|
|
70
|
-
return this.#stderr;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* This is an internal method that is often invoked automatically
|
|
75
|
-
* by various JavaScript methods.
|
|
76
|
-
* It consolidates the entire output for completeness and facilitates further processing.
|
|
77
|
-
*
|
|
78
|
-
* @returns {string} The consolidated output as a
|
|
79
|
-
*/
|
|
80
|
-
toString() {
|
|
81
|
-
return this.#combined.trim();
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* This represents the exit code returned by the external process.
|
|
85
|
-
* @returns {number} The exit
|
|
86
|
-
*/
|
|
87
|
-
get exitCode() {
|
|
88
|
-
return this.#code;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* This represents the exit signal, for example, "SIGTERM", received from the child process.
|
|
92
|
-
* @returns {string} The exit signal from the child
|
|
93
|
-
*/
|
|
94
|
-
get signal() {
|
|
95
|
-
return this.#signal;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* This method is used for debugging purposes. It displays the current state of the object
|
|
99
|
-
* when passed to the console.log function.
|
|
100
|
-
*/
|
|
101
|
-
[inspect.custom]() {
|
|
102
|
-
let stringify = (s) => s.length === 0 ? "''" : inspect(s);
|
|
103
|
-
return `ProcessOutput {
|
|
104
|
-
stdout: ${stringify(this.stdout)},
|
|
105
|
-
stderr: ${stringify(this.stderr)},
|
|
106
|
-
signal: ${inspect(this.signal)},
|
|
107
|
-
exitCode: ${(this.exitCode)}${exitCodeInfo(this.exitCode)
|
|
108
|
-
? ' (' + exitCodeInfo(this.exitCode) + ')'
|
|
109
|
-
: ''}
|
|
110
|
-
}`;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
export default ProcessOutput;
|
package/lib/ProcessPromise.d.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
export default ProcessPromise;
|
|
4
|
-
export type resolver = Function;
|
|
5
|
-
export type rejecter = Function;
|
|
6
|
-
export type PromiseConstruct = Function;
|
|
7
|
-
export type StdioOption = ('pipe' | 'ignore' | 'inherit' | number);
|
|
8
|
-
export type StdioOptions = Array<StdioOption> | StdioOption;
|
|
9
|
-
/**
|
|
10
|
-
* @typedef {Function} resolver
|
|
11
|
-
* @param {ProcessOutput} value
|
|
12
|
-
*/
|
|
13
|
-
/**
|
|
14
|
-
* @typedef {Function} rejecter
|
|
15
|
-
* @param {ProcessOutput} value
|
|
16
|
-
*/
|
|
17
|
-
/**
|
|
18
|
-
* @typedef {Function} PromiseConstruct
|
|
19
|
-
* @param {resolver} resolve
|
|
20
|
-
* @param {rejecter} reject
|
|
21
|
-
*/
|
|
22
|
-
/**
|
|
23
|
-
* @typedef {('pipe' | 'ignore' | 'inherit' | number)} StdioOption
|
|
24
|
-
* @description Defines the stdio configuration for each of the standard streams.
|
|
25
|
-
*
|
|
26
|
-
* - 'pipe' creates a pipe between the child process and the parent process.
|
|
27
|
-
* The parent end of the pipe is exposed as a property on the `ChildProcess` object.
|
|
28
|
-
* - 'ignore' indicates that the child process's corresponding stdio file descriptor will be ignored.
|
|
29
|
-
* - 'inherit' passes the corresponding stdio stream to/from the child process.
|
|
30
|
-
* - Stream object to be used for the stdio stream.
|
|
31
|
-
* - Positive integer representing a file descriptor to be used for the stdio stream.
|
|
32
|
-
*/
|
|
33
|
-
/**
|
|
34
|
-
* @typedef {Array<StdioOption>|StdioOption} StdioOptions
|
|
35
|
-
* @description
|
|
36
|
-
* Configures the stdio streams for the child process. This can be an array or a single StdioOption.
|
|
37
|
-
*
|
|
38
|
-
* Array Form: Specify the configuration for [stdin, stdout, stderr].
|
|
39
|
-
* - If array length is more than 3, additional positions correspond to extra streams.
|
|
40
|
-
* Single Value: This value will be applied to stdin, stdout, and stderr.
|
|
41
|
-
*
|
|
42
|
-
* Examples:
|
|
43
|
-
* - ['pipe', 'pipe', 'ignore']: Pipe stdin and stdout, ignore stderr.
|
|
44
|
-
* - 'inherit': Inherit all stdio streams from the parent.
|
|
45
|
-
*/
|
|
46
|
-
/**
|
|
47
|
-
* class extends promise
|
|
48
|
-
*/
|
|
49
|
-
declare class ProcessPromise extends Promise<any> {
|
|
50
|
-
/**
|
|
51
|
-
* @param {PromiseConstruct} p - A function that takes two arguments, resolve and reject.
|
|
52
|
-
*/
|
|
53
|
-
constructor(p: PromiseConstruct);
|
|
54
|
-
/**
|
|
55
|
-
* Set the environment
|
|
56
|
-
* and the
|
|
57
|
-
* @param {string} cmd - Command to execute
|
|
58
|
-
* @param {string} from - Position in the codfe where this is triggred from
|
|
59
|
-
* @param {function} resolve - Promise resolve method
|
|
60
|
-
* @param {function} reject - Reject method
|
|
61
|
-
* @param {object} options - Settings (options default)
|
|
62
|
-
*/
|
|
63
|
-
_bind(cmd: string, from: string, resolve: Function, reject: Function, options: object): void;
|
|
64
|
-
/**
|
|
65
|
-
* Run the promise
|
|
66
|
-
*/
|
|
67
|
-
run(): this;
|
|
68
|
-
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;
|
|
69
|
-
/**
|
|
70
|
-
* stdin child stream
|
|
71
|
-
* @retruns {Writeable}
|
|
72
|
-
*/
|
|
73
|
-
get stdin(): null;
|
|
74
|
-
/**
|
|
75
|
-
* stdout child stream
|
|
76
|
-
* @retruns {Readable}
|
|
77
|
-
*/
|
|
78
|
-
get stdout(): null;
|
|
79
|
-
/**
|
|
80
|
-
* stderr child stream
|
|
81
|
-
* @retruns {Readable}
|
|
82
|
-
*/
|
|
83
|
-
get stderr(): null;
|
|
84
|
-
/**
|
|
85
|
-
* process exit code
|
|
86
|
-
* @returns {Promise<number>}
|
|
87
|
-
*/
|
|
88
|
-
get exitCode(): Promise<number>;
|
|
89
|
-
catch(onrejected: any): Promise<any>;
|
|
90
|
-
/**
|
|
91
|
-
* Pipe the output to the input to the next Promise
|
|
92
|
-
* @example
|
|
93
|
-
* const res = await SH`ls -FLa`.pipe(SH`grep package.json`);
|
|
94
|
-
*/
|
|
95
|
-
pipe(dest: any): ProcessPromise;
|
|
96
|
-
/**
|
|
97
|
-
* @private
|
|
98
|
-
* Set a postrun action, internal use only
|
|
99
|
-
* @param {function} f
|
|
100
|
-
*/
|
|
101
|
-
private set _postrun(f);
|
|
102
|
-
/**
|
|
103
|
-
* Send a KILL signal to the child process
|
|
104
|
-
* @returns {Promise<number[]>} the pid numbers that has been killed
|
|
105
|
-
*/
|
|
106
|
-
kill(signal?: string): Promise<number[]>;
|
|
107
|
-
stdio(stdin: any, stdout?: string, stderr?: string): this;
|
|
108
|
-
/**
|
|
109
|
-
* Do not throw
|
|
110
|
-
*/
|
|
111
|
-
nothrow(): this;
|
|
112
|
-
/**
|
|
113
|
-
* supress log output
|
|
114
|
-
* SH.verbose = false; does the same
|
|
115
|
-
*/
|
|
116
|
-
quiet(): this;
|
|
117
|
-
/**
|
|
118
|
-
* Show log output in the console
|
|
119
|
-
*/
|
|
120
|
-
verbose(): this;
|
|
121
|
-
_quiet: boolean;
|
|
122
|
-
/**
|
|
123
|
-
* Set a timeout to kill a process
|
|
124
|
-
*
|
|
125
|
-
* @param {string} d - 10s, 1000ms
|
|
126
|
-
* @param {string} [signal] - default "SIGTERM" Signal to send to kill the proces
|
|
127
|
-
*/
|
|
128
|
-
timeout(d: string, signal?: string): this;
|
|
129
|
-
_timeout: number;
|
|
130
|
-
_timeoutSignal: string;
|
|
131
|
-
/**
|
|
132
|
-
* stop execution for the next step
|
|
133
|
-
*/
|
|
134
|
-
halt(): this;
|
|
135
|
-
/**
|
|
136
|
-
* @private
|
|
137
|
-
* Set a prerun action, internal use only
|
|
138
|
-
* @param {function} f
|
|
139
|
-
*/
|
|
140
|
-
private set _prerun(f);
|
|
141
|
-
/**
|
|
142
|
-
* Is this promise halted?
|
|
143
|
-
* @returns {boolean}
|
|
144
|
-
*/
|
|
145
|
-
get isHalted(): boolean;
|
|
146
|
-
#private;
|
|
147
|
-
}
|
package/lib/ProcessPromise.js
DELETED
|
@@ -1,354 +0,0 @@
|
|
|
1
|
-
// Copyright 2021 Google LLC
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
// Original Source: zx
|
|
17
|
-
// Link to Original Source: https://github.com/google/zx
|
|
18
|
-
// Reason for Using This Code:
|
|
19
|
-
// The core functionality of this code is highly beneficial. However, certain parts of the original code
|
|
20
|
-
// were overwriting the global namespace with core libraries and variables. This was causing conflicts with
|
|
21
|
-
// other packages (for instance, fetch) and introducing unexpected elements into my code base.
|
|
22
|
-
// Changes Made:
|
|
23
|
-
// - The code has been or is being reformatted to comply with ES2020 standards.
|
|
24
|
-
// - Some methods were added and existing ones were modified to enhance usability.
|
|
25
|
-
// - The namespace has been changed from '$' to 'SH'.
|
|
26
|
-
// Modified by: jorrit.duin+sh[AT]gmail.com
|
|
27
|
-
|
|
28
|
-
// import { spawn, exec } from 'node:child_process';
|
|
29
|
-
import assert from 'node:assert';
|
|
30
|
-
import { killProcesses, spawn, log, errnoMessage, exitCodeInfo, noop, parseDuration } from './utils.js';
|
|
31
|
-
import ProcessOutput from './ProcessOutput.js';
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* @typedef {Function} resolver
|
|
35
|
-
* @param {ProcessOutput} value
|
|
36
|
-
*/
|
|
37
|
-
/**
|
|
38
|
-
* @typedef {Function} rejecter
|
|
39
|
-
* @param {ProcessOutput} value
|
|
40
|
-
*/
|
|
41
|
-
/**
|
|
42
|
-
* @typedef {Function} PromiseConstruct
|
|
43
|
-
* @param {resolver} resolve
|
|
44
|
-
* @param {rejecter} reject
|
|
45
|
-
*/
|
|
46
|
-
/**
|
|
47
|
-
* @typedef {('pipe' | 'ignore' | 'inherit' | number)} StdioOption
|
|
48
|
-
* @description Defines the stdio configuration for each of the standard streams.
|
|
49
|
-
*
|
|
50
|
-
* - 'pipe' creates a pipe between the child process and the parent process.
|
|
51
|
-
* The parent end of the pipe is exposed as a property on the `ChildProcess` object.
|
|
52
|
-
* - 'ignore' indicates that the child process's corresponding stdio file descriptor will be ignored.
|
|
53
|
-
* - 'inherit' passes the corresponding stdio stream to/from the child process.
|
|
54
|
-
* - Stream object to be used for the stdio stream.
|
|
55
|
-
* - Positive integer representing a file descriptor to be used for the stdio stream.
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @typedef {Array<StdioOption>|StdioOption} StdioOptions
|
|
60
|
-
* @description
|
|
61
|
-
* Configures the stdio streams for the child process. This can be an array or a single StdioOption.
|
|
62
|
-
*
|
|
63
|
-
* Array Form: Specify the configuration for [stdin, stdout, stderr].
|
|
64
|
-
* - If array length is more than 3, additional positions correspond to extra streams.
|
|
65
|
-
* Single Value: This value will be applied to stdin, stdout, and stderr.
|
|
66
|
-
*
|
|
67
|
-
* Examples:
|
|
68
|
-
* - ['pipe', 'pipe', 'ignore']: Pipe stdin and stdout, ignore stderr.
|
|
69
|
-
* - 'inherit': Inherit all stdio streams from the parent.
|
|
70
|
-
*/
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* class extends promise
|
|
74
|
-
*/
|
|
75
|
-
class ProcessPromise extends Promise {
|
|
76
|
-
#command = '';
|
|
77
|
-
#from = '';
|
|
78
|
-
/** @type {resolver} */
|
|
79
|
-
#resolve = () => { };
|
|
80
|
-
/** @type {rejecter} */
|
|
81
|
-
#reject = () => { };
|
|
82
|
-
#snapshot = {};
|
|
83
|
-
/** @type {StdioOptions} */
|
|
84
|
-
#stdio = ['inherit', 'pipe', 'pipe'];
|
|
85
|
-
#nothrow = false;
|
|
86
|
-
#quiet = false;
|
|
87
|
-
#resolved = false;
|
|
88
|
-
#halted = false;
|
|
89
|
-
#piped = false;
|
|
90
|
-
#prerun = noop;
|
|
91
|
-
#postrun = noop;
|
|
92
|
-
/**
|
|
93
|
-
* @param {PromiseConstruct} p - A function that takes two arguments, resolve and reject.
|
|
94
|
-
*/
|
|
95
|
-
constructor(p) {
|
|
96
|
-
// @ts-ignore
|
|
97
|
-
super(p)
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Set the environment
|
|
101
|
-
* and the
|
|
102
|
-
* @param {string} cmd - Command to execute
|
|
103
|
-
* @param {string} from - Position in the codfe where this is triggred from
|
|
104
|
-
* @param {function} resolve - Promise resolve method
|
|
105
|
-
* @param {function} reject - Reject method
|
|
106
|
-
* @param {object} options - Settings (options default)
|
|
107
|
-
*/
|
|
108
|
-
_bind(cmd, from, resolve, reject, options) {
|
|
109
|
-
this.#command = cmd;
|
|
110
|
-
this.#from = from;
|
|
111
|
-
this.#resolve = resolve;
|
|
112
|
-
this.#reject = reject;
|
|
113
|
-
this.#snapshot = { ...options };
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Run the promise
|
|
117
|
-
*/
|
|
118
|
-
run() {
|
|
119
|
-
const ENV = this.#snapshot;
|
|
120
|
-
if (this.child) return this; // The _run() can be called from a few places.
|
|
121
|
-
this.#prerun(); // In case $1.pipe($2), the $2 returned, and on $2._run() invoke $1._run().
|
|
122
|
-
log({
|
|
123
|
-
kind: 'cmd',
|
|
124
|
-
cmd: this.#command,
|
|
125
|
-
verbose: ENV.verbose && !this.#quiet,
|
|
126
|
-
});
|
|
127
|
-
const cwd = ENV['processCwd'];
|
|
128
|
-
const shell = ENV['shell'];
|
|
129
|
-
this.child = spawn(ENV.prefix, [this.#command], {
|
|
130
|
-
cwd,
|
|
131
|
-
shell,
|
|
132
|
-
// @ts-ignore
|
|
133
|
-
stdio: this.#stdio,
|
|
134
|
-
windowsHide: true,
|
|
135
|
-
env: ENV.env,
|
|
136
|
-
});
|
|
137
|
-
this.child.on('close', (code, signal) => {
|
|
138
|
-
let message = `exit code: ${code}`;
|
|
139
|
-
if (code != 0 || signal != null) {
|
|
140
|
-
message = `${stderr || '\n'} at ${this.#from}`;
|
|
141
|
-
message += `\n exit code: ${code}${exitCodeInfo(code) ? ' (' + exitCodeInfo(code) + ')' : ''}`;
|
|
142
|
-
if (signal != null) {
|
|
143
|
-
message += `\n signal: ${signal}`;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
let output = new ProcessOutput(code, signal, stdout, stderr, combined, message);
|
|
147
|
-
if (code === 0 || this.#nothrow) {
|
|
148
|
-
this.#resolve(output);
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
this.#reject(output);
|
|
152
|
-
}
|
|
153
|
-
this.#resolved = true;
|
|
154
|
-
});
|
|
155
|
-
this.child.on('error', (err) => {
|
|
156
|
-
const message = `${err.message}\n` +
|
|
157
|
-
// @ts-ignore
|
|
158
|
-
` errno: ${err.errno} (${errnoMessage(err.errno)})\n` +
|
|
159
|
-
// @ts-ignore
|
|
160
|
-
` code: ${err.code}\n` +
|
|
161
|
-
` at ${this.#from}`;
|
|
162
|
-
this.#reject(new ProcessOutput(null, null, stdout, stderr, combined, message));
|
|
163
|
-
this.#resolved = true;
|
|
164
|
-
});
|
|
165
|
-
let stdout = '', stderr = '', combined = '';
|
|
166
|
-
/** @param {Blob} data */
|
|
167
|
-
const onStdout = (data) => {
|
|
168
|
-
log({ kind: 'stdout', data, verbose: ENV.verbose && !this.#quiet });
|
|
169
|
-
stdout += data;
|
|
170
|
-
combined += data;
|
|
171
|
-
};
|
|
172
|
-
/** @param {Blob} data */
|
|
173
|
-
const onStderr = (data) => {
|
|
174
|
-
log({ kind: 'stderr', data, verbose: ENV.verbose && !this.#quiet });
|
|
175
|
-
stderr += data;
|
|
176
|
-
combined += data;
|
|
177
|
-
};
|
|
178
|
-
if (!this.#piped)
|
|
179
|
-
// @ts-ignore
|
|
180
|
-
this.child.stdout?.on('data', onStdout); // If process is piped, don't collect or print output.
|
|
181
|
-
// @ts-ignore
|
|
182
|
-
this.child.stderr?.on('data', onStderr); // Stderr should be printed regardless of piping.
|
|
183
|
-
this.#postrun(); // In case $1.pipe($2), after both subprocesses are running, we can pipe $1.stdout to $2.stdin.
|
|
184
|
-
if (this._timeout && this._timeoutSignal) {
|
|
185
|
-
const t = setTimeout(() => this.kill(this._timeoutSignal), this._timeout);
|
|
186
|
-
this.finally(() => clearTimeout(t)).catch(noop);
|
|
187
|
-
}
|
|
188
|
-
return this;
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* stdin child stream
|
|
192
|
-
* @retruns {Writeable}
|
|
193
|
-
*/
|
|
194
|
-
get stdin() {
|
|
195
|
-
this.stdio('pipe');
|
|
196
|
-
this.run();
|
|
197
|
-
assert(this.child);
|
|
198
|
-
if (this.child.stdin == null)
|
|
199
|
-
throw new Error('The stdin of subprocess is null.');
|
|
200
|
-
return this.child.stdin;
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* stdout child stream
|
|
204
|
-
* @retruns {Readable}
|
|
205
|
-
*/
|
|
206
|
-
get stdout() {
|
|
207
|
-
this.run();
|
|
208
|
-
assert(this.child);
|
|
209
|
-
if (this.child.stdout == null)
|
|
210
|
-
throw new Error('The stdout of subprocess is null.');
|
|
211
|
-
return this.child.stdout;
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* stderr child stream
|
|
215
|
-
* @retruns {Readable}
|
|
216
|
-
*/
|
|
217
|
-
get stderr() {
|
|
218
|
-
this.run();
|
|
219
|
-
assert(this.child);
|
|
220
|
-
if (this.child.stderr == null)
|
|
221
|
-
throw new Error('The stderr of subprocess is null.');
|
|
222
|
-
return this.child.stderr;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* process exit code
|
|
226
|
-
* @returns {Promise<number>}
|
|
227
|
-
*/
|
|
228
|
-
get exitCode() {
|
|
229
|
-
return this.#then((p) => p.exitCode, (p) => p.exitCode);
|
|
230
|
-
}
|
|
231
|
-
#then(onfulfilled, onrejected) {
|
|
232
|
-
if (this.isHalted && !this.child) {
|
|
233
|
-
throw new Error('The process is halted!');
|
|
234
|
-
}
|
|
235
|
-
return super.then(onfulfilled, onrejected);
|
|
236
|
-
}
|
|
237
|
-
catch(onrejected) {
|
|
238
|
-
return super.catch(onrejected);
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Pipe the output to the input to the next Promise
|
|
242
|
-
* @example
|
|
243
|
-
* const res = await SH`ls -FLa`.pipe(SH`grep package.json`);
|
|
244
|
-
*/
|
|
245
|
-
pipe(dest) {
|
|
246
|
-
if (typeof dest == 'string')
|
|
247
|
-
throw new Error('The pipe() method does not take strings. Forgot SH?');
|
|
248
|
-
if (this.#resolved) {
|
|
249
|
-
if (dest instanceof ProcessPromise)
|
|
250
|
-
// @ts-ignore
|
|
251
|
-
dest.stdin.end(); // In case of piped stdin, we may want to close stdin of dest as well.
|
|
252
|
-
throw new Error("The pipe() method shouldn't be called after promise is already resolved!");
|
|
253
|
-
}
|
|
254
|
-
this.#piped = true;
|
|
255
|
-
if (dest instanceof ProcessPromise) {
|
|
256
|
-
dest.stdio('pipe');
|
|
257
|
-
dest._prerun = this.run.bind(this);
|
|
258
|
-
dest._postrun = () => {
|
|
259
|
-
if (!dest.child)
|
|
260
|
-
throw new Error('Access to stdin of pipe destination without creation a subprocess.');
|
|
261
|
-
// @ts-ignore
|
|
262
|
-
this.stdout.pipe(dest.stdin);
|
|
263
|
-
};
|
|
264
|
-
return dest;
|
|
265
|
-
}
|
|
266
|
-
else {
|
|
267
|
-
// @ts-ignore
|
|
268
|
-
this._postrun = () => this.stdout.pipe(dest);
|
|
269
|
-
return this;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Send a KILL signal to the child process
|
|
274
|
-
* @returns {Promise<number[]>} the pid numbers that has been killed
|
|
275
|
-
*/
|
|
276
|
-
async kill(signal = 'SIGTERM') {
|
|
277
|
-
if (!this.child)
|
|
278
|
-
throw new Error('Trying to kill a process without creating one.');
|
|
279
|
-
if (!this.child.pid)
|
|
280
|
-
throw new Error('The process pid is undefined.');
|
|
281
|
-
|
|
282
|
-
return await killProcesses(this.child.pid, signal)
|
|
283
|
-
}
|
|
284
|
-
stdio(stdin, stdout = 'pipe', stderr = 'pipe') {
|
|
285
|
-
this.#stdio = [stdin, stdout, stderr];
|
|
286
|
-
return this;
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Do not throw
|
|
290
|
-
*/
|
|
291
|
-
nothrow() {
|
|
292
|
-
this.#nothrow = true;
|
|
293
|
-
return this;
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* supress log output
|
|
297
|
-
* SH.verbose = false; does the same
|
|
298
|
-
*/
|
|
299
|
-
quiet() {
|
|
300
|
-
this.#quiet = true;
|
|
301
|
-
return this;
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* Show log output in the console
|
|
305
|
-
*/
|
|
306
|
-
verbose() {
|
|
307
|
-
this._quiet = false;
|
|
308
|
-
return this;
|
|
309
|
-
}
|
|
310
|
-
/**
|
|
311
|
-
* Set a timeout to kill a process
|
|
312
|
-
*
|
|
313
|
-
* @param {string} d - 10s, 1000ms
|
|
314
|
-
* @param {string} [signal] - default "SIGTERM" Signal to send to kill the proces
|
|
315
|
-
*/
|
|
316
|
-
timeout(d, signal = 'SIGTERM') {
|
|
317
|
-
this._timeout = parseDuration(d);
|
|
318
|
-
this._timeoutSignal = signal;
|
|
319
|
-
return this;
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* stop execution for the next step
|
|
323
|
-
*/
|
|
324
|
-
halt() {
|
|
325
|
-
this.#halted = true;
|
|
326
|
-
return this;
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* @private
|
|
330
|
-
* Set a prerun action, internal use only
|
|
331
|
-
* @param {function} f
|
|
332
|
-
*/
|
|
333
|
-
set _prerun(f) {
|
|
334
|
-
// @ts-ignore
|
|
335
|
-
this.#prerun = f;
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* @private
|
|
339
|
-
* Set a postrun action, internal use only
|
|
340
|
-
* @param {function} f
|
|
341
|
-
*/
|
|
342
|
-
set _postrun(f) {
|
|
343
|
-
// @ts-ignore
|
|
344
|
-
this.#postrun = f;
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* Is this promise halted?
|
|
348
|
-
* @returns {boolean}
|
|
349
|
-
*/
|
|
350
|
-
get isHalted() {
|
|
351
|
-
return this.#halted;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
export default ProcessPromise;
|
package/lib/utils.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/**
|
|
3
|
-
* Kills a process and all child processes of a given process ID in Linux/Posix.
|
|
4
|
-
* @param {number} processPid - The process ID.
|
|
5
|
-
* @param {string} signal - Signal to send.
|
|
6
|
-
* @retruns {Promise<number[]>} array with killed pid numbers
|
|
7
|
-
*/
|
|
8
|
-
export function killProcesses(processPid: number, signal: string): Promise<any>;
|
|
9
|
-
export function noop(): void;
|
|
10
|
-
export function randomId(): string;
|
|
11
|
-
export function isString(obj: any): boolean;
|
|
12
|
-
export function quote(arg: any): any;
|
|
13
|
-
export function quotePowerShell(arg: any): any;
|
|
14
|
-
export function log(entry: any): void;
|
|
15
|
-
export function exitCodeInfo(exitCode: any): any;
|
|
16
|
-
export function errnoMessage(errno: any): any;
|
|
17
|
-
export function parseDuration(d: any): number;
|
|
18
|
-
export function formatCmd(cmd: any): string;
|
|
19
|
-
export { spawn };
|
|
20
|
-
import { spawn } from 'node:child_process';
|