Skip to content

Commit 1b7279e

Browse files
committed
fix: improve json output
1 parent 3105764 commit 1b7279e

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ program.addHelpText('afterAll', () => {
5656

5757
// Add interrupt handling to the CLI
5858
const runCLI = withInterruptHandling(async () => {
59+
// programm.opts is not init yet
60+
const isJson = process.argv.includes('--json');
5961
const chain = await getChain();
60-
if (program.opts().json) {
62+
if (isJson) {
63+
console.info('[');
64+
} else {
6165
logInfo(`${'-'.repeat(100)}`);
6266
logInfo(`Using chain: Name: ${chain.name}, Chain ID: ${chain.id}`);
6367
logInfo(`${'-'.repeat(100)}`);
@@ -72,7 +76,9 @@ runCLI()
7276
process.exit(1);
7377
})
7478
.finally(async () => {
75-
if (!program.opts().json) {
79+
if (program.opts().json) {
80+
console.info(']');
81+
} else {
7682
const chain = await getChain();
7783
if (chain.id === mainnet.id) {
7884
showMainnetWarning();

utils/logging/console.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,41 @@ const bigIntStringify = <T>(value: T): string => {
2828
);
2929
};
3030

31+
let IS_PREV_JSON_LOG = false;
32+
3133
export const createConsole = (
3234
headMessage: HeadMessage,
3335
type: 'info' | 'error' | 'table' | 'bold' = 'info',
3436
) => {
3537
return <T, U>(...args: T[] | U[]) => {
38+
// print comma if previous log is JSON to separate logs
39+
if (IS_PREV_JSON_LOG) {
40+
console.info(',');
41+
}
42+
// set flag so that next log can check if previous log is JSON and print comma
43+
if (program.opts().json) {
44+
IS_PREV_JSON_LOG = true;
45+
}
46+
3647
switch (type) {
3748
case 'table':
3849
if (program.opts().json) {
39-
console.info(`\n${getColoredLog(headMessage, headMessage + ':')}`);
40-
console.info('<JSON>');
41-
console.info(bigIntStringify(args));
42-
console.info('</JSON>');
43-
return;
50+
return console.info(bigIntStringify(args));
4451
}
4552
console.info(`\n${getColoredLog(headMessage, headMessage + ':')}`);
46-
console.table(...args);
47-
break;
53+
return console.table(...args);
54+
4855
case 'bold':
4956
if (program.opts().json) {
50-
console.info(`\n${getColoredLog(headMessage, headMessage + ':')}`);
51-
console.info('<JSON>');
52-
console.info(bigIntStringify({ Result: args }));
53-
console.info('</JSON>');
54-
return;
57+
return console.info(bigIntStringify({ result: args }));
5558
}
56-
console.info(getColoredLog(headMessage, args));
57-
break;
59+
return console.info(getColoredLog(headMessage, args));
5860
default:
5961
if (program.opts().json) {
60-
console.info(`\n${getColoredLog(headMessage, headMessage + ':')}`);
61-
console.info('<JSON>');
62-
console.info(bigIntStringify({ Result: args }));
63-
console.info('</JSON>');
64-
return;
62+
return console.info(bigIntStringify({ result: args }));
6563
}
6664
// eslint-disable-next-line no-console
67-
console[type](
65+
return console[type](
6866
`\n${getColoredLog(headMessage, headMessage + ':')}`,
6967
...args,
7068
);
@@ -80,10 +78,14 @@ const createTable = (headMessage?: HeadMessage) => (args: CreateTableArgs) => {
8078
if (!data) return;
8179

8280
if (program.opts().json) {
83-
console.info('<JSON>');
84-
console.info(bigIntStringify({ Result: data }));
85-
console.info('</JSON>');
86-
return;
81+
// print comma if previous log is JSON to separate logs
82+
if (IS_PREV_JSON_LOG) {
83+
console.info(',');
84+
}
85+
86+
IS_PREV_JSON_LOG = true;
87+
88+
return console.info(bigIntStringify({ result: data }));
8789
} else {
8890
const table = new Table({ ...TABLE_PARAMS, ...params });
8991
table.push(...data);

0 commit comments

Comments
 (0)