import { Chalk , Instance } from "chalk" ; import { Logger , Colors , Spinner , ProgressBar } from "reg-suit-interface" ; import { Bar as ProgressBarConstructor , Presets } from "cli-progress" ; import { Spinner as SpinnerConstructor } from "cli-spinner" ; export type LogLevel = "verbose" | "info" | "silent" ; const noopSpinner = { start : ( ) => { } , stop : ( ) => { } , } ; const noopProgressBar = { start : ( _x , _y ? ) => { } , update : ( _x ) => { } , increment : ( _x ) => { } , stop : ( ) => { } , } ; export class RegLogger implements Logger { _level : LogLevel ; _chalk : Chalk ; constructor ( private _category = "reg-suit" ) { this . _chalk = new Instance ( { level : 1 } ) ; this . _level = "info" ; } fork ( newCategory ) { const l = new RegLogger ( newCategory ) ; l . setLevel ( this . _level ) ; return l ; } setLevel ( v ) { this . _level = v ; } get colors ( ) : Colors { return this . _chalk as Colors ; } set colors ( _v ) { return ; } getSpinner ( msg ? ) { if ( this . _level === "silent" ) return noopSpinner ; const spinner = new SpinnerConstructor ( msg ) ; spinner . setSpinnerString ( 3 ) ; spinner . stop = spinner . stop . bind ( spinner , true ) ; return spinner ; } getProgressBar ( ) { if ( this . _level === "silent" ) return noopProgressBar ; const bar = new ProgressBarConstructor ( { } , Presets . rect ) ; return bar ; } info ( msg ) { if ( this . _level !== "silent" ) { console . log ( this . _prefix + this . colors . green ( "info " ) + msg ) ; } } warn ( msg ) { if ( this . _level !== "silent" ) { console . warn ( this . _prefix + this . colors . yellow ( "warn " ) + msg ) ; } } error ( obj ) { if ( this . _level !== "silent" ) { if ( typeof obj === "string" ) { console . error ( this . _prefix + this . colors . red ( "error " ) + obj ) ; } else { console . error ( this . _prefix + this . colors . red ( "error " ) , obj ) ; } } } verbose ( msg , ... objects ) { if ( this . _level === "verbose" ) { console . log ( this . _prefix + this . colors . green ( "debug " ) + msg ) ; if ( objects && objects . length ) { objects . forEach ( obj => { console . log ( this . colors . gray ( JSON . stringify ( obj , null , 2 ) ) ) ; } ) ; } } } get _prefix ( ) { return `[${ this . _category }] ` ; } } export function createLogger ( category ? ) { return new RegLogger ( category ) ; }