import Table from 'cli-table' ; import prettyjson from 'prettyjson' ; import PrettyError from 'pretty-error' ; import chalk from 'chalk' ; export class Output { private exitCode : number = 0 ; private data : any [ ] = [ ] ; private prettyError : PrettyError = new PrettyError ( ) ; public static create ( ) { return new this ( ) ; } public static errorOutput ( error ? , exitCode ? ) { const output = new this ( ) ; output . error ( error , exitCode ) ; return output ; } see http param { Number } exitCode return { this } public setExitCode ( exitCode ) { this . exitCode = exitCode ; return this ; } public addData ( ... data ) { data . forEach ( item => { if ( item === undefined ) { return ; } if ( item . constructor === Object ) { return this . addData ( prettyjson . render ( item ) ) ; } this . data . push ( item ) ; } ) ; return this ; } public success ( message , data ? ) { this . addData ( chalk `\n  {green {bold Success!}} ${ message }\n` , data ) ; } public resetData ( ) { this . data = [ ] ; return this ; } public addHorizontalTable ( head , data , options = { } ) { const table = new Table ( { head , ... options } ) ; table . push ( ... data ) ; this . addData ( table . toString ( ) ) ; return this ; } public addVerticalTable ( data , options ? ) { const table = new Table ( options ) ; table . push ( ... data ) ; this . addData ( table . toString ( ) ) ; return this ; } public addCrossTable ( head , data , options = { } ) { const table = new Table ( options ) ; table . push ( ... data ) ; this . addData ( table . toString ( ) ) ; return this ; } param { Error | string } error param { Number | null } exitCode param { boolean } clear return { this } public error ( error = 'Unknown error' , exitCode = 1 , clear = false ) { if ( ! this . prettyError ) { this . prettyError = new PrettyError ( ) ; } if ( exitCode ) { this . setExitCode ( exitCode ) ; } if ( clear ) { this . resetData ( ) ; } this . addData ( this . prettyError . render ( error ) ) ; return this ; } /**
     * Write the output to the console.
     */ public flush ( ) { this . data . forEach ( ( piece ) => console . log ( piece ) ) ; } public send ( ) { this . flush ( ) ; process . exit ( this . exitCode ) ; } }