import { exec , ExecException , ChildProcess } from "child_process" ; type Handler = ( error : ExecException | null , stdout : Buffer | string , stderr : Buffer | string ) => void ; export class Command { static async apply ( path , command ) { return new Promise < string > ( ( resolve , reject ) => { Command . run ( path , command , ( error , stdOut , stdErr ) => { if ( error === null ) { const out = stdOut as Buffer ; resolve ( out . toString ( "utf8" ) ) ; } else { reject ( Command . attemptErrorParse ( error , stdOut as Buffer ) ) ; } } ) ; } ) ; } private static attemptErrorParse ( error , stdOut ) { const out = stdOut . toString ( "utf-8" ) ; if ( out !== null && out . length > 0 ) { try { const tryGenericError = JSON . parse ( out ) as Error ; return new Error ( tryGenericError ?. message ) ; } catch { } } return new Error ( error ?. message ) ; } private static run ( path , command , handler ) { return exec ( command . join ( " " ) , { cwd : path , maxBuffer : 21 * 1024 * 1024 } , handler ) ; } }