@@ -96,39 +96,47 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
96
96
_processes . push ( childProcess ) ;
97
97
98
98
// Create the error here so the stack shows who called this function.
99
+ const error = new Error ( ) ;
100
+ try {
101
+ throw error ;
102
+ } catch ( _ ) {
103
+ // ignore, just need to generate the stack trace
104
+ }
105
+
106
+ // Return log info about the current process status
107
+ function envDump ( ) {
108
+ return [
109
+ `ENV:${ JSON . stringify ( spawnOptions . env , null , 2 ) } ` ,
110
+ `STDOUT:\n${ stdout } ` ,
111
+ `STDERR:\n${ stderr } ` ,
112
+ ] . join ( '\n\n' ) ;
113
+ }
99
114
100
- return new Promise ( ( resolve , reject ) => {
115
+ return new Promise < ProcessOutput > ( ( resolve , reject ) => {
101
116
let matched = false ;
102
117
103
- childProcess . on ( 'exit' , ( error : any ) => {
118
+ childProcess . on ( 'exit' , ( err : any ) => {
104
119
_processes = _processes . filter ( ( p ) => p !== childProcess ) ;
105
120
106
121
if ( options . waitForMatch && ! matched ) {
107
- error = `Output didn't match '${ options . waitForMatch } '.` ;
122
+ reject (
123
+ `Process output didn't match - "${ cmd } ${ args . join ( ' ' ) } ": '${
124
+ options . waitForMatch
125
+ } '.\n\n${ envDump ( ) } \n`,
126
+ ) ;
127
+ return ;
108
128
}
109
129
110
- if ( ! error ) {
130
+ if ( ! err ) {
111
131
resolve ( { stdout, stderr } ) ;
112
132
return ;
113
133
}
114
134
115
- reject (
116
- new Error (
117
- `Running "${ cmd } ${ args . join ( ' ' ) } " returned error. ${ error } ...\n\nENV:${ JSON . stringify (
118
- process . env ,
119
- null ,
120
- 2 ,
121
- ) } \n\nSTDOUT:\n${ stdout } \n\nSTDERR:\n${ stderr } \n`,
122
- ) ,
123
- ) ;
135
+ reject ( `Process exit error - "${ cmd } ${ args . join ( ' ' ) } ": ${ err } ...\n\n${ envDump ( ) } \n` ) ;
124
136
} ) ;
137
+
125
138
childProcess . on ( 'error' , ( err ) => {
126
- err . message += `${ err } ...\n\nENV:${ JSON . stringify (
127
- process . env ,
128
- null ,
129
- 2 ,
130
- ) } \n\nSTDOUT:\n${ stdout } \n\nSTDERR:\n${ stderr } \n`;
131
- reject ( err ) ;
139
+ reject ( `Process error - "${ cmd } ${ args . join ( ' ' ) } ": ${ err } ...\n\n${ envDump ( ) } \n` ) ;
132
140
} ) ;
133
141
134
142
if ( options . waitForMatch ) {
@@ -154,6 +162,9 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
154
162
childProcess . stdin ! . write ( options . stdin ) ;
155
163
childProcess . stdin ! . end ( ) ;
156
164
}
165
+ } ) . catch ( ( err ) => {
166
+ error . message = err . toString ( ) ;
167
+ return Promise . reject ( error ) ;
157
168
} ) ;
158
169
}
159
170
0 commit comments