import { Console } from 'console' ; import stream from 'stream' ; import { Terminal } from 'terminal-kit' ; class ConsoleBuffer extends stream . Writable { constructor ( private buffer ) { super ( ) ; } _write ( chunk , enc , next ) { this . buffer . push ( chunk . toString ( ) ) ; next ( ) ; } } export function createBufferedConsole ( ) { let buffer = [ ] ; const stdout = new ConsoleBuffer ( buffer ) ; const stderr = new ConsoleBuffer ( buffer ) ; const bufferedConsole = new Console ( stdout , stderr ) ; return { bufferedConsole , flushConsole : ( terminal ) => { buffer . forEach ( message => { terminal ( message ) ; } ) ; buffer = [ ] ; } , } ; }