@@ -3,6 +3,10 @@ import * as path from "node:path";
3
3
import { bsc_exe , rescript_exe } from "#cli/bins" ;
4
4
5
5
/**
6
+ * @typedef {{
7
+ * throwOnFail?: boolean,
8
+ * } & child_process.SpawnOptions } ExecOptions
9
+ *
6
10
* @typedef {{
7
11
* status: number,
8
12
* stdout: string,
@@ -20,24 +24,18 @@ const signals = {
20
24
export const { exec, node, npx, mocha, bsc, rescript, execBuild, execClean } =
21
25
setup ( ) ;
22
26
23
- /**
24
- * @typedef {{
25
- * throwOnExit?: boolean,
26
- * }} ExecOptions
27
- */
28
-
29
27
/**
30
28
* @param {string } [cwd]
31
29
*/
32
30
export function setup ( cwd = process . cwd ( ) ) {
33
31
/**
34
32
* @param {string } command
35
33
* @param {string[] } [args]
36
- * @param {child_process.SpawnOptions & ExecOptions } [options]
34
+ * @param {ExecOptions } [options]
37
35
* @return {Promise<ExecResult> }
38
36
*/
39
37
async function exec ( command , args = [ ] , options = { } ) {
40
- const { throwOnExit = true } = options ;
38
+ const { throwOnFail = options . stdio === "inherit" } = options ;
41
39
42
40
const stdoutChunks = [ ] ;
43
41
const stderrChunks = [ ] ;
@@ -63,20 +61,16 @@ export function setup(cwd = process.cwd()) {
63
61
} ) ;
64
62
65
63
subprocess . once ( "close" , ( exitCode , signal ) => {
66
- const stdout = stdoutChunks . length
67
- ? Buffer . concat ( stdoutChunks ) . toString ( "utf8" )
68
- : null ;
69
- const stderr = stdoutChunks . length
70
- ? Buffer . concat ( stderrChunks ) . toString ( "utf8" )
71
- : null ;
64
+ const stdout = Buffer . concat ( stdoutChunks ) . toString ( "utf8" ) ;
65
+ const stderr = Buffer . concat ( stderrChunks ) . toString ( "utf8" ) ;
72
66
73
67
let code = exitCode ?? 1 ;
74
68
if ( signals [ signal ] ) {
75
69
// + 128 is standard POSIX practice, see also https://nodejs.org/api/process.html#exit-codes
76
70
code = signals [ signal ] + 128 ;
77
71
}
78
72
79
- if ( throwOnExit && code !== 0 ) {
73
+ if ( throwOnFail && code !== 0 ) {
80
74
reject ( { status : code , stdout, stderr } ) ;
81
75
} else {
82
76
resolve ( { status : code , stdout, stderr } ) ;
@@ -92,7 +86,7 @@ export function setup(cwd = process.cwd()) {
92
86
* `node` CLI
93
87
*
94
88
* @param {string[] } [args]
95
- * @param {child_process.SpawnOptions } [options]
89
+ * @param {ExecOptions } [options]
96
90
* @return {Promise<ExecResult> }
97
91
*/
98
92
node ( args = [ ] , options = { } ) {
@@ -103,7 +97,7 @@ export function setup(cwd = process.cwd()) {
103
97
* `npx` CLI
104
98
*
105
99
* @param {string[] } [args]
106
- * @param {child_process.SpawnOptions } [options]
100
+ * @param {ExecOptions } [options]
107
101
* @return {Promise<ExecResult> }
108
102
*/
109
103
npx ( args = [ ] , options = { } ) {
@@ -114,7 +108,7 @@ export function setup(cwd = process.cwd()) {
114
108
* Mocha CLI
115
109
*
116
110
* @param {string[] } [args]
117
- * @param {child_process.SpawnOptions } [options]
111
+ * @param {ExecOptions } [options]
118
112
* @return {Promise<ExecResult> }
119
113
*/
120
114
mocha ( args = [ ] , options = { } ) {
@@ -132,7 +126,7 @@ export function setup(cwd = process.cwd()) {
132
126
* | (string & {})
133
127
* )} command
134
128
* @param {string[] } [args]
135
- * @param {child_process.SpawnOptions } [options]
129
+ * @param {ExecOptions } [options]
136
130
* @return {Promise<ExecResult> }
137
131
*/
138
132
rescript ( command , args = [ ] , options = { } ) {
@@ -144,7 +138,7 @@ export function setup(cwd = process.cwd()) {
144
138
* `bsc` CLI
145
139
*
146
140
* @param {string[] } [args]
147
- * @param {child_process.SpawnOptions } [options]
141
+ * @param {ExecOptions } [options]
148
142
* @return {Promise<ExecResult> }
149
143
*/
150
144
bsc ( args = [ ] , options = { } ) {
@@ -155,7 +149,7 @@ export function setup(cwd = process.cwd()) {
155
149
* Execute ReScript `build` command directly
156
150
*
157
151
* @param {string[] } [args]
158
- * @param {child_process.SpawnOptions } [options]
152
+ * @param {ExecOptions } [options]
159
153
* @return {Promise<ExecResult> }
160
154
*/
161
155
execBuild ( args = [ ] , options = { } ) {
@@ -166,7 +160,7 @@ export function setup(cwd = process.cwd()) {
166
160
* Execute ReScript `clean` command directly
167
161
*
168
162
* @param {string[] } [args]
169
- * @param {child_process.SpawnOptions } [options]
163
+ * @param {ExecOptions } [options]
170
164
* @return {Promise<ExecResult> }
171
165
*/
172
166
execClean ( args = [ ] , options = { } ) {
0 commit comments