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