summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/utils/ConsolePatcher.ts
diff options
context:
space:
mode:
authorN. Taylor Mullen <[email protected]>2025-07-12 15:42:47 -0700
committerGitHub <[email protected]>2025-07-12 15:42:47 -0700
commit4442e893c367a901a4c801816e0ade5b78c291c9 (patch)
treea5962bb7e5995e33306bd33635f25f889f5c4b52 /packages/cli/src/ui/utils/ConsolePatcher.ts
parent890982a811e22de9525148e6c28f39bfbf10a49a (diff)
fix(auth): Remove sharp edges from headless auth (#3985)
Diffstat (limited to 'packages/cli/src/ui/utils/ConsolePatcher.ts')
-rw-r--r--packages/cli/src/ui/utils/ConsolePatcher.ts61
1 files changed, 61 insertions, 0 deletions
diff --git a/packages/cli/src/ui/utils/ConsolePatcher.ts b/packages/cli/src/ui/utils/ConsolePatcher.ts
new file mode 100644
index 00000000..10be3bc7
--- /dev/null
+++ b/packages/cli/src/ui/utils/ConsolePatcher.ts
@@ -0,0 +1,61 @@
+/**
+ * @license
+ * Copyright 2025 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+import util from 'util';
+import { ConsoleMessageItem } from '../types.js';
+
+interface ConsolePatcherParams {
+ onNewMessage: (message: Omit<ConsoleMessageItem, 'id'>) => void;
+ debugMode: boolean;
+}
+
+export class ConsolePatcher {
+ private originalConsoleLog = console.log;
+ private originalConsoleWarn = console.warn;
+ private originalConsoleError = console.error;
+ private originalConsoleDebug = console.debug;
+
+ private params: ConsolePatcherParams;
+
+ constructor(params: ConsolePatcherParams) {
+ this.params = params;
+ }
+
+ patch() {
+ console.log = this.patchConsoleMethod('log', this.originalConsoleLog);
+ console.warn = this.patchConsoleMethod('warn', this.originalConsoleWarn);
+ console.error = this.patchConsoleMethod('error', this.originalConsoleError);
+ console.debug = this.patchConsoleMethod('debug', this.originalConsoleDebug);
+ }
+
+ cleanup = () => {
+ console.log = this.originalConsoleLog;
+ console.warn = this.originalConsoleWarn;
+ console.error = this.originalConsoleError;
+ console.debug = this.originalConsoleDebug;
+ };
+
+ private formatArgs = (args: unknown[]): string => util.format(...args);
+
+ private patchConsoleMethod =
+ (
+ type: 'log' | 'warn' | 'error' | 'debug',
+ originalMethod: (...args: unknown[]) => void,
+ ) =>
+ (...args: unknown[]) => {
+ if (this.params.debugMode) {
+ originalMethod.apply(console, args);
+ }
+
+ if (type !== 'debug' || this.params.debugMode) {
+ this.params.onNewMessage({
+ type,
+ content: this.formatArgs(args),
+ count: 1,
+ });
+ }
+ };
+}