import { ChildProcess , spawn , SpawnOptions } from 'child_process' ; import { MESSAGES } from '../ui/messages' ; export class AbstractRunner { constructor ( protected binary ) { } public async run ( command , collect = false , cwd = process . cwd ( ) ) { const args = [ command ] ; const options = { cwd , stdio : collect ? 'pipe' : 'inherit' , shell : true , } ; return new Promise < null | string > ( ( resolve , reject ) => { const child = spawn ( this . binary , args , options ) ; if ( collect ) { child . stdout ?. on ( 'data' , ( data ) => resolve ( data . toString ( ) . replace ( /\r\n|\n/ , '' ) ) ) ; } child . on ( 'close' , ( code ) => { if ( code === 0 ) { resolve ( null ) ; } else { reject ( new Error ( MESSAGES . RUNNER_EXECUTION_ERROR ( `${ this . binary } ${ command }` ) ) ) ; } } ) ; } ) ; } }