import { Transform } from 'stream' ; import { ExecaChildProcess } from 'execa' ; import { createPromiseFromStreams , createSplitStream , createMapStream } from '@kbn/utils' ; function skipLastEmptyLineStream ( ) { let skippedEmptyLine = false ; return new Transform ( { objectMode : true , transform ( line , _ , cb ) { if ( skippedEmptyLine ) { this . push ( '' ) ; skippedEmptyLine = false ; } if ( line === '' ) { skippedEmptyLine = true ; return cb ( ) ; } else { return cb ( undefined , line ) ; } } , } ) ; } export async function watchStdioForLine ( proc , logFn , exitAfter ? ) { function onLogLine ( line ) { logFn ( line ) ; if ( exitAfter && exitAfter . test ( line ) ) { proc . kill ( 'SIGINT' ) ; } } await Promise . all ( [ proc . catch ( ( error ) => { if ( error . signal !== 'SIGINT' ) { throw error ; } } ) , createPromiseFromStreams ( [ proc . stdout ! , createSplitStream ( '\n' ) , skipLastEmptyLineStream ( ) , createMapStream ( onLogLine ) , ] ) , createPromiseFromStreams ( [ proc . stderr ! , createSplitStream ( '\n' ) , skipLastEmptyLineStream ( ) , createMapStream ( onLogLine ) , ] ) , ] ) ; }