import ansiEscapes from 'ansi-escapes' ; import wrapAnsi from 'wrap-ansi' ; const originalWrite = Symbol ( 'webpackbarWrite' ) ; export default class LogUpdate { private prevLineCount : any ; private listening : any ; private extraLines : any ; private _streams : any ; constructor ( ) { this . prevLineCount = 0 ; this . listening = false ; this . extraLines = '' ; this . _onData = this . _onData . bind ( this ) ; this . _streams = [ process . stdout , process . stderr ] ; } get columns ( ) { return ( process . stderr . columns || 80 ) - 2 ; } write ( data ) { const stream = process . stderr ; if ( stream . write [ originalWrite ] ) { stream . write [ originalWrite ] . call ( stream , data , 'utf-8' ) ; } else { stream . write ( data , 'utf-8' ) ; } } start ( ) { this . listen ( ) ; } clear ( ) { this . write ( ansiEscapes . eraseLines ( this . prevLineCount ) ) ; } done ( ) { this . stopListen ( ) ; this . prevLineCount = 0 ; } listen ( ) { if ( this . listening ) { return ; } for ( const stream of this . _streams ) { if ( stream . write [ originalWrite ] ) { continue ; } const write = ( data , ... args ) => { if ( ! stream . write [ originalWrite ] ) { return stream . write ( data , ... args ) ; } this . _onData ( data ) ; } ; write [ originalWrite ] = stream . write ; stream . write = write ; } this . listening = true ; } stopListen ( ) { for ( const stream of this . _streams ) { if ( stream . write [ originalWrite ] ) { stream . write = stream . write [ originalWrite ] ; } } this . listening = false ; } _onData ( data ) { this . extraLines += data ; } render ( lines ) { const wrappedLines = wrapAnsi ( lines , this . columns , { trim : false , hard : true , wordWrap : false , } ) ; const data = `${ ansiEscapes . eraseLines ( this . prevLineCount ) }${ this . extraLines }${ wrappedLines }` ; this . write ( data ) ; this . extraLines = '' ; this . prevLineCount = wrappedLines . split ( '\n' ) . length ; } }