@@ -96,39 +96,42 @@ 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
+
101
+ // Return log info about the current process status
102
+ function envDump ( ) {
103
+ return [
104
+ `ENV:${ JSON . stringify ( spawnOptions . env , null , 2 ) } ` ,
105
+ `STDOUT:\n${ stdout } ` ,
106
+ `STDERR:\n${ stderr } ` ,
107
+ ] . join ( '\n\n' ) ;
108
+ }
99
109
100
- return new Promise ( ( resolve , reject ) => {
110
+ return new Promise < ProcessOutput > ( ( resolve , reject ) => {
101
111
let matched = false ;
102
112
103
- childProcess . on ( 'exit' , ( error : any ) => {
113
+ childProcess . on ( 'exit' , ( code : number ) => {
104
114
_processes = _processes . filter ( ( p ) => p !== childProcess ) ;
105
115
106
116
if ( options . waitForMatch && ! matched ) {
107
- error = `Output didn't match '${ options . waitForMatch } '.` ;
117
+ reject (
118
+ `Process output didn't match - "${ cmd } ${ args . join ( ' ' ) } ": '${
119
+ options . waitForMatch
120
+ } ': ${ code } ...\n\n${ envDump ( ) } \n`,
121
+ ) ;
122
+ return ;
108
123
}
109
124
110
- if ( ! error ) {
125
+ if ( ! code ) {
111
126
resolve ( { stdout, stderr } ) ;
112
127
return ;
113
128
}
114
129
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
- ) ;
130
+ reject ( `Process exit error - "${ cmd } ${ args . join ( ' ' ) } ": ${ code } ...\n\n${ envDump ( ) } \n` ) ;
124
131
} ) ;
132
+
125
133
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 ) ;
134
+ reject ( `Process error - "${ cmd } ${ args . join ( ' ' ) } ": ${ err } ...\n\n${ envDump ( ) } \n` ) ;
132
135
} ) ;
133
136
134
137
if ( options . waitForMatch ) {
@@ -154,6 +157,9 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
154
157
childProcess . stdin ! . write ( options . stdin ) ;
155
158
childProcess . stdin ! . end ( ) ;
156
159
}
160
+ } ) . catch ( ( err ) => {
161
+ error . message = err . toString ( ) ;
162
+ return Promise . reject ( error ) ;
157
163
} ) ;
158
164
}
159
165
0 commit comments