feat(cli): route non-interactive output to stderr (#5624)

This commit is contained in:
Allen Hutchison
2025-08-05 16:11:21 -07:00
committed by GitHub
parent 268627469b
commit 2141b39c3d
3 changed files with 41 additions and 23 deletions

View File

@@ -8,8 +8,9 @@ import util from 'util';
import { ConsoleMessageItem } from '../types.js';
interface ConsolePatcherParams {
onNewMessage: (message: Omit<ConsoleMessageItem, 'id'>) => void;
onNewMessage?: (message: Omit<ConsoleMessageItem, 'id'>) => void;
debugMode: boolean;
stderr?: boolean;
}
export class ConsolePatcher {
@@ -46,16 +47,22 @@ export class ConsolePatcher {
originalMethod: (...args: unknown[]) => void,
) =>
(...args: unknown[]) => {
if (this.params.debugMode) {
originalMethod.apply(console, args);
}
if (this.params.stderr) {
if (type !== 'debug' || this.params.debugMode) {
this.originalConsoleError(this.formatArgs(args));
}
} else {
if (this.params.debugMode) {
originalMethod.apply(console, args);
}
if (type !== 'debug' || this.params.debugMode) {
this.params.onNewMessage({
type,
content: this.formatArgs(args),
count: 1,
});
if (type !== 'debug' || this.params.debugMode) {
this.params.onNewMessage?.({
type,
content: this.formatArgs(args),
count: 1,
});
}
}
};
}