@@ -96,39 +96,42 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
9696 _processes . push ( childProcess ) ;
9797
9898 // 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+ }
99109
100- return new Promise ( ( resolve , reject ) => {
110+ return new Promise < ProcessOutput > ( ( resolve , reject ) => {
101111 let matched = false ;
102112
103- childProcess . on ( 'exit' , ( error : any ) => {
113+ childProcess . on ( 'exit' , ( code : number ) => {
104114 _processes = _processes . filter ( ( p ) => p !== childProcess ) ;
105115
106116 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 ;
108123 }
109124
110- if ( ! error ) {
125+ if ( ! code ) {
111126 resolve ( { stdout, stderr } ) ;
112127 return ;
113128 }
114129
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` ) ;
124131 } ) ;
132+
125133 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` ) ;
132135 } ) ;
133136
134137 if ( options . waitForMatch ) {
@@ -154,6 +157,9 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
154157 childProcess . stdin ! . write ( options . stdin ) ;
155158 childProcess . stdin ! . end ( ) ;
156159 }
160+ } ) . catch ( ( err ) => {
161+ error . message = err . toString ( ) ;
162+ return Promise . reject ( error ) ;
157163 } ) ;
158164}
159165
0 commit comments