import { spawn , ChildProcess } from 'child_process' ; export async function exec ( cmd , args = [ ] ) { const child = spawn ( cmd , args , { shell : true } ) ; redirectOutputFor ( child ) ; await waitFor ( child ) ; } function redirectOutputFor ( child ) { const printStdout = ( data ) => { process . stdout . write ( data . toString ( ) ) ; } ; const printStderr = ( data ) => { process . stderr . write ( data . toString ( ) ) ; } ; child . stdout && child . stdout . on ( 'data' , printStdout ) ; child . stderr && child . stderr . on ( 'data' , printStderr ) ; child . once ( 'close' , ( ) => { child . stdout && child . stdout . off ( 'data' , printStdout ) ; child . stderr && child . stderr . off ( 'data' , printStderr ) ; } ) ; } async function waitFor ( child ) { return new Promise ( resolve => { child . once ( 'close' , ( ) => resolve ( ) ) ; } ) ; }