fix(cli): keep json preflight stdout machine-readable

This commit is contained in:
Altay
2026-02-23 12:34:10 +03:00
committed by Peter Steinberger
parent 493ebb915b
commit 9e4a366ee6
7 changed files with 260 additions and 11 deletions

View File

@@ -3,7 +3,7 @@ import { setVerbose } from "../../globals.js";
import { isTruthyEnvValue } from "../../infra/env.js";
import type { LogLevel } from "../../logging/levels.js";
import { defaultRuntime } from "../../runtime.js";
import { getCommandPath, getVerboseFlag, hasHelpOrVersion } from "../argv.js";
import { getCommandPath, getVerboseFlag, hasFlag, hasHelpOrVersion } from "../argv.js";
import { emitCliBanner } from "../banner.js";
import { resolveCliName } from "../cli-name.js";
@@ -30,6 +30,7 @@ const PLUGIN_REQUIRED_COMMANDS = new Set([
"onboard",
]);
const CONFIG_GUARD_BYPASS_COMMANDS = new Set(["doctor", "completion", "secrets"]);
const JSON_PARSE_ONLY_COMMANDS = new Set(["config set"]);
function getRootCommand(command: Command): Command {
let current = command;
@@ -51,6 +52,17 @@ function getCliLogLevel(actionCommand: Command): LogLevel | undefined {
return typeof logLevel === "string" ? (logLevel as LogLevel) : undefined;
}
function isJsonOutputMode(commandPath: string[], argv: string[]): boolean {
if (!hasFlag(argv, "--json")) {
return false;
}
const key = `${commandPath[0] ?? ""} ${commandPath[1] ?? ""}`.trim();
if (JSON_PARSE_ONLY_COMMANDS.has(key)) {
return false;
}
return true;
}
export function registerPreActionHooks(program: Command, programVersion: string) {
program.hook("preAction", async (_thisCommand, actionCommand) => {
setProcessTitleForCommand(actionCommand);
@@ -79,8 +91,13 @@ export function registerPreActionHooks(program: Command, programVersion: string)
if (CONFIG_GUARD_BYPASS_COMMANDS.has(commandPath[0])) {
return;
}
const suppressDoctorStdout = isJsonOutputMode(commandPath, argv);
const { ensureConfigReady } = await import("./config-guard.js");
await ensureConfigReady({ runtime: defaultRuntime, commandPath });
await ensureConfigReady({
runtime: defaultRuntime,
commandPath,
...(suppressDoctorStdout ? { suppressDoctorStdout: true } : {}),
});
// Load plugins for commands that need channel access
if (PLUGIN_REQUIRED_COMMANDS.has(commandPath[0])) {
const { ensurePluginRegistryLoaded } = await import("../plugin-registry.js");