import child_process = require ( "child_process" ) ; import SFPLogger , { COLOR_TRACE , Logger , LoggerLevel } from "../../logger/SFPLogger" ; export default class ExecuteCommand { public constructor ( protected logger ? , protected logLevel ? , protected showProgress ? ) { } public execCommand ( command , workingdirectory , timeout = 0 , isEnableErrorFromOutputStream = false ) { return new Promise ( ( resolve , reject ) => { try { let childProcess ; childProcess = child_process . exec ( command , { encoding : "utf8" , cwd : workingdirectory , maxBuffer : 1024 * 1024 * 5 , timeout : timeout } ) ; let stdoutContents = '' ; let stderrContents = '' ; childProcess . stdout . on ( 'data' , ( data ) => { stdoutContents += data . toString ( ) ; if ( this . showProgress ) SFPLogger . log ( COLOR_TRACE ( data ) , LoggerLevel . INFO , this . logger ) ; } ) ; childProcess . stderr . on ( 'data' , ( data ) => { stderrContents += data . toString ( ) ; } ) ; childProcess . once ( 'close' , ( code , signal ) => { if ( code === 0 || ( code === null && signal === "SIGTERM" ) ) { resolve ( stdoutContents ) ; } else { if ( stderrContents && ! isEnableErrorFromOutputStream ) reject ( new Error ( stderrContents ) ) ; else reject ( new Error ( stdoutContents ) ) ; } } ) ; childProcess . once ( 'error' , ( err ) => { if ( stderrContents && ! isEnableErrorFromOutputStream ) reject ( new Error ( stderrContents ) ) ; else reject ( new Error ( stdoutContents ) ) ; } ) ; } catch ( error ) { reject ( error ) ; } } ) ; } }