/**
 * Copyright(c) Microsoft Corporation.All rights reserved.
 * Licensed under the MIT License.
 */ import * as childProcess from 'child_process' ; import { join } from 'path' ; import { isAzPreviewMessage } from './' ; export class ChildProcessUtils { private async execDispatch ( args ) { const dispatchPath = join ( __dirname , '..' , '..' , 'node_modules' , 'botdispatch' , 'bin' , 'netcoreapp2.1' , 'Dispatch.dll' ) ; return new Promise ( ( pResolve , pReject ) : void => { childProcess . spawn ( 'dotnet' , [ dispatchPath , ... args ] , { stdio : 'inherit' } ) . on ( 'close' , ( code ) : void => { pResolve ( '' ) ; } ) . on ( 'error' , ( err ) : void => { pReject ( err ) ; } ) ; } ) ; } public async execute ( command , args ) { if ( command === 'dispatch' ) { return this . execDispatch ( args ) ; } return new Promise ( ( pResolve , pReject ) : void => { childProcess . exec ( `${ command } ${ args . join ( ' ' ) }` , ( err , stdout , stderr ) : void => { if ( stderr && ! stderr . includes ( 'Update available' ) && ! stderr . toLowerCase ( ) . includes ( 'warning' ) ) { pReject ( stderr ) ; } pResolve ( stdout ) ; } ) ; } ) ; } public async tryExecute ( command ) { return new Promise ( ( pResolve , pReject ) : void => { try { childProcess . exec ( `${ command . join ( ' ' ) }` , ( err , stdout , stderr ) : void => { if ( stderr && ! isAzPreviewMessage ( stderr ) ) { pReject ( stderr ) ; } pResolve ( stdout ) ; } ) ; } catch ( err ) { pReject ( err ) ; } } ) ; } }