import { spawn as spawnOg , SpawnOptionsWithoutStdio } from 'child_process' ; const d = require ( 'debug' ) ( 'electron-windows-installer:spawn' ) ; export default function spawn ( exe , params , opts ? ) { return new Promise ( ( resolve , reject ) : void => { let proc = null ; d ( `Spawning ${ exe } ${ params . join ( ' ' ) }` ) ; if ( ! opts ) { proc = spawnOg ( exe , params ) ; } else { proc = spawnOg ( exe , params , opts ) ; } let rejected = false ; let refCount = 3 ; let stdout = '' ; let release = ( ) : void => { if ( -- refCount <= 0 && ! rejected ) resolve ( stdout ) ; } ; let bufHandler = ( b ) : void => { let chunk = b . toString ( ) ; stdout += chunk ; } ; proc . stdout . on ( 'data' , bufHandler ) ; proc . stdout . once ( 'close' , release ) ; proc . stderr . on ( 'data' , bufHandler ) ; proc . stderr . once ( 'close' , release ) ; proc . on ( 'error' , ( e ) : void => reject ( e ) ) ; proc . on ( 'close' , ( code ) : void => { if ( code === 0 ) { release ( ) ; } else { rejected = true ; reject ( new Error ( `Failed with exit code: ${ code }\nOutput:\n${ stdout }` ) ) ; } } ) ; } ) ; }