| 1 | /** |
| 2 | * Early initialization that must happen before ANY other imports |
| 3 | * This file intercepts console and stdout/stderr to prevent dependency logging |
| 4 | */ |
| 5 | |
| 6 | import { isHeadlessMode as checkIsHeadlessMode } from "./util/cli.js"; |
| 7 | |
| 8 | // Check if we're in headless mode by looking at process arguments |
| 9 | // We need to do this before any imports to catch early logging |
| 10 | const isHeadlessMode = checkIsHeadlessMode(); |
| 11 | |
| 12 | // Store original methods before ANY dependencies can use them |
| 13 | const originalStdoutWrite = process.stdout.write.bind(process.stdout); |
| 14 | const originalStderrWrite = process.stderr.write.bind(process.stderr); |
| 15 | const originalConsole = { |
| 16 | log: console.log, |
| 17 | info: console.info, |
| 18 | warn: console.warn, |
| 19 | error: console.error, |
| 20 | debug: console.debug, |
| 21 | trace: console.trace, |
| 22 | table: console.table, |
| 23 | dir: console.dir, |
| 24 | dirxml: console.dirxml, |
| 25 | group: console.group, |
| 26 | groupEnd: console.groupEnd, |
| 27 | groupCollapsed: console.groupCollapsed, |
| 28 | time: console.time, |
| 29 | timeEnd: console.timeEnd, |
| 30 | timeLog: console.timeLog, |
| 31 | clear: console.clear, |
| 32 | count: console.count, |
| 33 | countReset: console.countReset, |
| 34 | assert: console.assert, |
| 35 | }; |
| 36 | |
| 37 | // Track if we're inside our own code vs dependency code |
| 38 | let isHeadlessModeConfigured = false; |
| 39 | |
| 40 | // Override console methods if in headless mode |
| 41 | if (isHeadlessMode) { |
| 42 | // Override ALL console methods to no-ops |
| 43 | Object.keys(originalConsole).forEach((method) => { |
| 44 | (console as any)[method] = () => {}; |
| 45 | }); |
| 46 | |
| 47 | // Override stdout/stderr to block everything by default |
| 48 | process.stdout.write = function ( |
| 49 | chunk: any, |
| 50 | encoding?: any, |
| 51 | callback?: any, |
| 52 | ): boolean { |
| 53 | // Always return true to indicate success, but don't actually write |
| 54 | if (typeof encoding === "function") { |
| 55 | encoding(); |
| 56 | return true; |
| 57 | } |
| 58 | if (typeof callback === "function") { |
| 59 | callback(); |
| 60 | } |
| 61 | return true; |
| 62 | }; |
| 63 | |
| 64 | process.stderr.write = function ( |
| 65 | chunk: any, |
| 66 | encoding?: any, |
| 67 | callback?: any, |
| 68 | ): boolean { |
| 69 | // Always return true to indicate success, but don't actually write |
| 70 | if (typeof encoding === "function") { |
| 71 | encoding(); |
| 72 | return true; |
| 73 | } |
| 74 | if (typeof callback === "function") { |
| 75 | callback(); |
| 76 | } |
| 77 | return true; |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Configure console for headless mode |
| 83 | * Since init.ts already blocks everything, this just tracks state |
| 84 | * and provides the safe output functions |
| 85 | */ |
| 86 | export function configureConsoleForHeadless(headless: boolean): void { |
| 87 | isHeadlessModeConfigured = headless; |
| 88 | |
| 89 | // init.ts should have already handled the blocking if in headless mode |
| 90 | // This function now mainly exists for compatibility and state tracking |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Safe stdout output that bypasses all overrides |
| 95 | * Use this for intentional output in headless mode |
| 96 | */ |
| 97 | export function safeStdout(message: string): void { |
| 98 | if (isHeadlessMode || isHeadlessModeConfigured) { |
| 99 | // Use the original stdout that was saved before any overrides |
| 100 | originalStdoutWrite(message); |
| 101 | } else { |
| 102 | // In non-headless mode, just use normal stdout |
| 103 | process.stdout.write(message); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Safe stderr output that bypasses all overrides |
| 109 | * Use this for error messages that should always be visible |
| 110 | */ |
| 111 | export function safeStderr(message: string): void { |
| 112 | if (isHeadlessMode || isHeadlessModeConfigured) { |
| 113 | // Use the original stderr that was saved before any overrides |
| 114 | originalStderrWrite(message); |
| 115 | } else { |
| 116 | // In non-headless mode, just use normal stderr |
| 117 | process.stderr.write(message); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Reset console overrides to original state |
| 123 | * Note: This won't affect init.ts overrides which happen at module load |
| 124 | */ |
| 125 | export function resetConsoleOverrides(): void { |
| 126 | isHeadlessModeConfigured = false; |
| 127 | // The actual console restoration would need to happen in init.ts |
| 128 | // This is mainly for testing purposes |
| 129 | } |