import child_process from "child_process" ; import { logger } from "../logger" ; example param cmd param args param opts export const exec = async ( cmd , args = [ ] , opts = { } ) : Promise < string > => { return new Promise < string > ( ( resolve , reject ) => { if ( process . platform === "win32" ) { opts = { ... opts , shell : true } ; } const child = child_process . spawn ( cmd , args , opts ) ; const cmdString = `${ cmd } ${ args . join ( " " ) }` ; let stdout = "" ; let stderr = "" ; child . stdout ?. on ( "data" , ( data ) => { logger . debug ( `stdout -> '${ cmdString }' -> ${ data }` . trim ( ) ) ; stdout = stdout + data ; } ) ; child . stderr ?. on ( "data" , ( data ) => { logger . debug ( `stderr -> '${ cmdString }' -> ${ data }` . trim ( ) ) ; stderr = stderr + data ; } ) ; child . on ( "error" , ( err ) => { logger . verbose ( `'${ cmdString }' encountered an error during execution` ) ; logger . verbose ( err ) ; reject ( err ) ; } ) ; child . on ( "exit" , ( code ) => { logger . verbose ( `'${ cmdString }' exited with code: ${ code }` ) ; if ( stdout . length > 0 ) { logger . verbose ( `'${ cmdString }': ${ stdout }` . trim ( ) ) ; } if ( stderr . length > 0 ) { logger . verbose ( `'${ cmdString }': ${ stderr }` . trim ( ) ) ; } if ( code === 0 ) { resolve ( stdout . trim ( ) ) ; } else { reject ( Error ( stderr . trim ( ) ) ) ; } } ) ; } ) ; } ;