@@ -28,11 +28,24 @@ const bigIntStringify = <T>(value: T): string => {
2828 ) ;
2929} ;
3030
31+ // Flag so that next log can add comma if previous log is JSON to create valid JSON array output
3132let IS_PREV_JSON_LOG = false ;
3233
34+ export const openJsonLogging = ( ) => {
35+ console . info ( '[' ) ;
36+ } ;
37+
38+ export const closeJsonLogging = ( ) => {
39+ console . info ( ']' ) ;
40+ } ;
41+
42+ export const TEST_RESET_JSON_LOG_FLAG = ( ) => {
43+ IS_PREV_JSON_LOG = false ;
44+ } ;
45+
3346export const createConsole = (
3447 headMessage : HeadMessage ,
35- type : 'info' | 'error' | 'table' | 'bold' = 'info' ,
48+ type : 'info' | 'error' | 'table' | 'bold' | 'json' = 'info' ,
3649) => {
3750 return < T , U > ( ...args : T [ ] | U [ ] ) => {
3851 // print comma if previous log is JSON to separate logs
@@ -57,6 +70,8 @@ export const createConsole = (
5770 return console . info ( bigIntStringify ( { result : args } ) ) ;
5871 }
5972 return console . info ( getColoredLog ( headMessage , args ) ) ;
73+ case 'json' :
74+ return console . info ( bigIntStringify ( args ) ) ;
6075 default :
6176 if ( program . opts ( ) . json ) {
6277 return console . info ( bigIntStringify ( { result : args } ) ) ;
@@ -70,13 +85,9 @@ export const createConsole = (
7085 } ;
7186} ;
7287
73- export const TEST_RESET_JSON_LOG_FLAG = ( ) => {
74- IS_PREV_JSON_LOG = false ;
75- } ;
76-
7788const createTable = ( headMessage ?: HeadMessage ) => ( args : CreateTableArgs ) => {
7889 const { data, params, csvPath } = args ;
79- if ( headMessage )
90+ if ( headMessage && ! program . opts ( ) . json )
8091 console . info ( `\n${ getColoredLog ( headMessage , headMessage + ':' ) } ` ) ;
8192
8293 if ( ! data ) return ;
@@ -112,3 +123,4 @@ export const logError = createConsole('Error', 'error');
112123export const logBold = createConsole ( 'Bold' , 'bold' ) ;
113124export const logCancel = createConsole ( 'Cancel' ) ;
114125export const logResultSimple = createConsole ( 'Result' , 'table' ) ;
126+ export const logJson = createConsole ( 'Result' , 'json' ) ;
0 commit comments