Skip to content

Commit 4ee41cc

Browse files
committed
refactor(cli): separate json payload output from logging
1 parent 274af04 commit 4ee41cc

89 files changed

Lines changed: 710 additions & 693 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/cli/browser-cli-actions-input/register.files-downloads.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async function runBrowserPostAction<T>(params: {
3838
{ timeoutMs: params.timeoutMs },
3939
);
4040
if (params.parent?.json) {
41-
defaultRuntime.log(JSON.stringify(result, null, 2));
41+
defaultRuntime.writeJson(result);
4242
return;
4343
}
4444
defaultRuntime.log(params.describeSuccess(result));

src/cli/browser-cli-actions-input/register.form-wait-eval.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ export function registerBrowserFormWaitEvalCommands(
116116
},
117117
});
118118
if (parent?.json) {
119-
defaultRuntime.log(JSON.stringify(result, null, 2));
119+
defaultRuntime.writeJson(result);
120120
return;
121121
}
122-
defaultRuntime.log(JSON.stringify(result.result ?? null, null, 2));
122+
defaultRuntime.writeJson(result.result ?? null);
123123
} catch (err) {
124124
defaultRuntime.error(danger(String(err)));
125125
defaultRuntime.exit(1);

src/cli/browser-cli-actions-input/register.navigation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function registerBrowserNavigationCommands(
3131
{ timeoutMs: 20000 },
3232
);
3333
if (parent?.json) {
34-
defaultRuntime.log(JSON.stringify(result, null, 2));
34+
defaultRuntime.writeJson(result);
3535
return;
3636
}
3737
defaultRuntime.log(`navigated to ${result.url ?? url}`);

src/cli/browser-cli-actions-input/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function logBrowserActionResult(
4646
successMessage: string,
4747
) {
4848
if (parent?.json) {
49-
defaultRuntime.log(JSON.stringify(result, null, 2));
49+
defaultRuntime.writeJson(result);
5050
return;
5151
}
5252
defaultRuntime.log(successMessage);

src/cli/browser-cli-actions-observe.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ export function registerBrowserActionObserveCommands(
3939
{ timeoutMs: 20000 },
4040
);
4141
if (parent?.json) {
42-
defaultRuntime.log(JSON.stringify(result, null, 2));
42+
defaultRuntime.writeJson(result);
4343
return;
4444
}
45-
defaultRuntime.log(JSON.stringify(result.messages, null, 2));
45+
defaultRuntime.writeJson(result.messages);
4646
});
4747
});
4848

@@ -65,7 +65,7 @@ export function registerBrowserActionObserveCommands(
6565
{ timeoutMs: 20000 },
6666
);
6767
if (parent?.json) {
68-
defaultRuntime.log(JSON.stringify(result, null, 2));
68+
defaultRuntime.writeJson(result);
6969
return;
7070
}
7171
defaultRuntime.log(`PDF: ${shortenHomePath(result.path)}`);
@@ -107,7 +107,7 @@ export function registerBrowserActionObserveCommands(
107107
{ timeoutMs: timeoutMs ?? 20000 },
108108
);
109109
if (parent?.json) {
110-
defaultRuntime.log(JSON.stringify(result, null, 2));
110+
defaultRuntime.writeJson(result);
111111
return;
112112
}
113113
defaultRuntime.log(result.response.body);

src/cli/browser-cli-debug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function printJsonResult(parent: BrowserParentOpts, result: unknown): boolean {
3939
if (!parent.json) {
4040
return false;
4141
}
42-
defaultRuntime.log(JSON.stringify(result, null, 2));
42+
defaultRuntime.writeJson(result);
4343
return true;
4444
}
4545

src/cli/browser-cli-inspect.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function registerBrowserInspectCommands(
3939
{ timeoutMs: 20000 },
4040
);
4141
if (parent?.json) {
42-
defaultRuntime.log(JSON.stringify(result, null, 2));
42+
defaultRuntime.writeJson(result);
4343
return;
4444
}
4545
defaultRuntime.log(`MEDIA:${shortenHomePath(result.path)}`);
@@ -106,19 +106,13 @@ export function registerBrowserInspectCommands(
106106
await fs.writeFile(opts.out, payload, "utf8");
107107
}
108108
if (parent?.json) {
109-
defaultRuntime.log(
110-
JSON.stringify(
111-
{
112-
ok: true,
113-
out: opts.out,
114-
...(result.format === "ai" && result.imagePath
115-
? { imagePath: result.imagePath }
116-
: {}),
117-
},
118-
null,
119-
2,
120-
),
121-
);
109+
defaultRuntime.writeJson({
110+
ok: true,
111+
out: opts.out,
112+
...(result.format === "ai" && result.imagePath
113+
? { imagePath: result.imagePath }
114+
: {}),
115+
});
122116
} else {
123117
defaultRuntime.log(shortenHomePath(opts.out));
124118
if (result.format === "ai" && result.imagePath) {
@@ -129,7 +123,7 @@ export function registerBrowserInspectCommands(
129123
}
130124

131125
if (parent?.json) {
132-
defaultRuntime.log(JSON.stringify(result, null, 2));
126+
defaultRuntime.writeJson(result);
133127
return;
134128
}
135129

src/cli/browser-cli-manage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function printJsonResult(parent: BrowserParentOpts, payload: unknown): boolean {
2525
if (!parent?.json) {
2626
return false;
2727
}
28-
defaultRuntime.log(JSON.stringify(payload, null, 2));
28+
defaultRuntime.writeJson(payload);
2929
return true;
3030
}
3131

@@ -89,7 +89,7 @@ function runBrowserCommand(action: () => Promise<void>) {
8989

9090
function logBrowserTabs(tabs: BrowserTab[], json?: boolean) {
9191
if (json) {
92-
defaultRuntime.log(JSON.stringify({ tabs }, null, 2));
92+
defaultRuntime.writeJson({ tabs });
9393
return;
9494
}
9595
if (tabs.length === 0) {

src/cli/browser-cli-resize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function runBrowserResizeWithOutput(params: {
3030
);
3131

3232
if (params.parent?.json) {
33-
defaultRuntime.log(JSON.stringify(result, null, 2));
33+
defaultRuntime.writeJson(result);
3434
return;
3535
}
3636
defaultRuntime.log(params.successMessage);

src/cli/browser-cli-state.cookies-storage.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async function runMutationRequest(params: {
3636
try {
3737
const result = await callBrowserRequest(params.parent, params.request, { timeoutMs: 20000 });
3838
if (params.parent?.json) {
39-
defaultRuntime.log(JSON.stringify(result, null, 2));
39+
defaultRuntime.writeJson(result);
4040
return;
4141
}
4242
defaultRuntime.log(params.successMessage);
@@ -72,10 +72,10 @@ export function registerBrowserCookiesAndStorageCommands(
7272
{ timeoutMs: 20000 },
7373
);
7474
if (parent?.json) {
75-
defaultRuntime.log(JSON.stringify(result, null, 2));
75+
defaultRuntime.writeJson(result);
7676
return;
7777
}
78-
defaultRuntime.log(JSON.stringify(result.cookies ?? [], null, 2));
78+
defaultRuntime.writeJson(result.cookies ?? []);
7979
} catch (err) {
8080
defaultRuntime.error(danger(String(err)));
8181
defaultRuntime.exit(1);
@@ -165,10 +165,10 @@ export function registerBrowserCookiesAndStorageCommands(
165165
{ timeoutMs: 20000 },
166166
);
167167
if (parent?.json) {
168-
defaultRuntime.log(JSON.stringify(result, null, 2));
168+
defaultRuntime.writeJson(result);
169169
return;
170170
}
171-
defaultRuntime.log(JSON.stringify(result.values ?? {}, null, 2));
171+
defaultRuntime.writeJson(result.values ?? {});
172172
} catch (err) {
173173
defaultRuntime.error(danger(String(err)));
174174
defaultRuntime.exit(1);

0 commit comments

Comments
 (0)