@@ -3,45 +3,53 @@ import * as process from 'process';
33
44export class ConsoleOutput {
55 private static lastWasLive = false ;
6+ public static showColors = true ;
67
78 public static log ( message : string ) {
8- if ( this . lastWasLive ) {
9- console . log ( "" ) ;
9+ if ( this . lastWasLive ) {
10+ console . log ( '' ) ;
1011 this . lastWasLive = false ;
1112 }
1213 console . log ( `${ message } ` ) ;
1314 }
1415
1516 public static error ( message : string ) {
16- if ( this . lastWasLive ) {
17- console . log ( "" ) ;
17+ if ( this . lastWasLive ) {
18+ console . log ( '' ) ;
1819 this . lastWasLive = false ;
1920 }
20- console . error ( `${ message } ` ) ;
21+ const formattedStr = ( ConsoleOutput . showColors ) ? ConsoleOutput . format ( message , 'error' ) : message ;
22+ console . error ( formattedStr ) ;
2123 }
2224
2325 public static info ( message : string ) {
24- if ( this . lastWasLive ) {
25- console . log ( "" ) ;
26+ if ( this . lastWasLive ) {
27+ console . log ( '' ) ;
2628 this . lastWasLive = false ;
2729 }
28- console . log ( `${ message } ` ) ;
30+
31+ const formattedStr = ( ConsoleOutput . showColors ) ? ConsoleOutput . format ( message , 'info' ) : message ;
32+ console . error ( formattedStr ) ;
2933 }
3034
3135 public static success ( message : string ) {
32- if ( this . lastWasLive ) {
33- console . log ( "" ) ;
36+ if ( this . lastWasLive ) {
37+ console . log ( '' ) ;
3438 this . lastWasLive = false ;
3539 }
36- console . log ( `${ message } ` ) ;
40+
41+ const formattedStr = ( ConsoleOutput . showColors ) ? ConsoleOutput . format ( message , 'success' ) : message ;
42+ console . error ( formattedStr ) ;
3743 }
3844
3945 public static warning ( message : string ) {
40- if ( this . lastWasLive ) {
41- console . log ( "" ) ;
46+ if ( this . lastWasLive ) {
47+ console . log ( '' ) ;
4248 this . lastWasLive = false ;
4349 }
44- console . log ( `${ message } ` ) ;
50+
51+ const formattedStr = ( ConsoleOutput . showColors ) ? ConsoleOutput . format ( message , 'warning' ) : message ;
52+ console . error ( formattedStr ) ;
4553 }
4654
4755 public static logLive = ( message : string ) => {
@@ -50,6 +58,19 @@ export class ConsoleOutput {
5058 readline . cursorTo ( process . stdout , 0 , null ) ;
5159 process . stdout . write ( message ) ;
5260 ConsoleOutput . lastWasLive = true ;
61+ } ;
62+
63+ public static format ( message : string , type : 'error' | 'info' | 'success' | 'warning' ) {
64+ switch ( type ) {
65+ case 'info' :
66+ return `\x1b[34m${ message } \x1b[0m` ;
67+ case 'warning' :
68+ return `\x1b[33m${ message } \x1b[0m` ;
69+ case 'error' :
70+ return `\x1b[31m${ message } \x1b[0m` ;
71+ case 'success' :
72+ return `\x1b[32m${ message } \x1b[0m` ;
73+ }
5374 }
5475
5576
0 commit comments