fix(cli): 补齐显式机器输出边界

This commit is contained in:
Codex
2026-07-12 02:17:05 +02:00
parent 80edc6c08b
commit bcc7807c3d
5 changed files with 181 additions and 38 deletions
+10 -7
View File
@@ -97,7 +97,7 @@ export function emitText(text: string, command = "text"): void {
export function emitError(command: string, error: unknown): void {
const safeCommand = redactSensitiveCommandArgs(command);
if (error instanceof CliInputError && !cliDebugEnabled()) {
if (error instanceof CliInputError && !cliDebugEnabled() && !cliJsonOutputRequested()) {
safeStdoutWrite(renderTextOutput(safeCommand, renderCliInputErrorText(safeCommand, error)));
return;
}
@@ -171,15 +171,18 @@ function cliInputErrorPayload(error: CliInputError): Record<string, unknown> {
...(error.usage !== undefined ? { usage: error.usage } : {}),
...(error.supported !== undefined ? { supported: error.supported } : {}),
...(error.hint !== undefined ? { hint: error.hint } : {}),
...(debug
? { debug: true, stack: error.stack ?? null }
: {
stackOmitted: true,
diagnosticHint: "Set UNIDESK_CLI_DEBUG=1 only when an input error needs a stack trace.",
}),
...(debug ? { debug: true, stack: error.stack ?? null } : { debug: false }),
};
}
function cliJsonOutputRequested(argv = process.argv.slice(2)): boolean {
for (let index = 0; index < argv.length; index += 1) {
if (argv[index] === "--json") return true;
if (argv[index] === "-o" && argv[index + 1] === "json") return true;
}
return false;
}
function cliDebugEnabled(): boolean {
return process.env.UNIDESK_CLI_DEBUG === "1"
|| process.env.UNIDESK_CLI_FULL_ERROR === "1"