|
1 | | -export const Console = console.constructor; |
| 1 | +import { Writable } from "node:stream"; |
| 2 | +import { formatWithOptions } from "node:util"; |
| 3 | +import type { InspectOptions } from "node:util"; |
| 4 | + |
| 5 | +const originalConsole = console; |
| 6 | + |
| 7 | +interface ConsoleOptions { |
| 8 | + stdout?: Writable; |
| 9 | + stderr?: Writable; |
| 10 | + ignoreErrors?: boolean; // ignored |
| 11 | + colorMode?: boolean | string; |
| 12 | + inspectOptions?: InspectOptions; |
| 13 | + groupIndentation?: number; // ignored |
| 14 | +} |
| 15 | + |
| 16 | +export class Console { |
| 17 | + readonly #stdout?: Writable; |
| 18 | + readonly #stderr?: Writable; |
| 19 | + readonly #inspectOptions: InspectOptions; |
| 20 | + |
| 21 | + constructor(stdout: Writable, stderr?: Writable, ignoreErrors?: boolean); |
| 22 | + constructor(opts: ConsoleOptions); |
| 23 | + constructor( |
| 24 | + opts: Writable | ConsoleOptions, |
| 25 | + stderr?: Writable, |
| 26 | + ignoreErrors?: boolean |
| 27 | + ) { |
| 28 | + if (opts instanceof Writable) { |
| 29 | + opts = { stdout: opts, stderr, ignoreErrors }; |
| 30 | + } |
| 31 | + this.#stdout = opts.stdout; |
| 32 | + this.#stderr = opts.stderr ?? this.#stdout; |
| 33 | + const colors = |
| 34 | + typeof opts.colorMode === "string" ? false : opts.colorMode ?? false; |
| 35 | + this.#inspectOptions = opts.inspectOptions ?? { colors }; |
| 36 | + |
| 37 | + // Ensure methods are bound to the instance |
| 38 | + return new Proxy(this, { |
| 39 | + get(target, prop) { |
| 40 | + const value = target[prop as keyof Console]; |
| 41 | + if (typeof value === "function") { |
| 42 | + return value.bind(target); |
| 43 | + } |
| 44 | + return value; |
| 45 | + }, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + // Vitest expects this function to be called `value`: |
| 50 | + // https://github.com/vitest-dev/vitest/blob/v1.0.0-beta.5/packages/vitest/src/runtime/console.ts#L16 |
| 51 | + value(stream: Writable, data: unknown[]): void { |
| 52 | + stream.write(formatWithOptions(this.#inspectOptions, ...data) + "\n"); |
| 53 | + } |
| 54 | + |
| 55 | + assert(condition?: boolean, ...data: unknown[]): void { |
| 56 | + originalConsole.assert(condition, ...data); |
| 57 | + } |
| 58 | + clear(): void { |
| 59 | + originalConsole.clear(); |
| 60 | + } |
| 61 | + count(label?: string): void { |
| 62 | + originalConsole.count(label); |
| 63 | + } |
| 64 | + countReset(label?: string): void { |
| 65 | + originalConsole.countReset(label); |
| 66 | + } |
| 67 | + debug(...data: unknown[]): void { |
| 68 | + if (this.#stdout === undefined) { |
| 69 | + originalConsole.debug(...data); |
| 70 | + } else { |
| 71 | + this.value(this.#stdout, data); |
| 72 | + } |
| 73 | + } |
| 74 | + dir(item?: unknown, options?: unknown): void { |
| 75 | + originalConsole.dir(item, options); |
| 76 | + } |
| 77 | + dirxml(...data: unknown[]): void { |
| 78 | + originalConsole.dirxml(...data); |
| 79 | + } |
| 80 | + error(...data: unknown[]): void { |
| 81 | + if (this.#stderr === undefined) { |
| 82 | + originalConsole.error(...data); |
| 83 | + } else { |
| 84 | + this.value(this.#stderr, data); |
| 85 | + } |
| 86 | + } |
| 87 | + group(...data: unknown[]): void { |
| 88 | + originalConsole.group(...data); |
| 89 | + } |
| 90 | + groupCollapsed(...data: unknown[]): void { |
| 91 | + originalConsole.groupCollapsed(...data); |
| 92 | + } |
| 93 | + groupEnd(): void { |
| 94 | + originalConsole.groupEnd(); |
| 95 | + } |
| 96 | + info(...data: unknown[]): void { |
| 97 | + if (this.#stdout === undefined) { |
| 98 | + originalConsole.info(...data); |
| 99 | + } else { |
| 100 | + this.value(this.#stdout, data); |
| 101 | + } |
| 102 | + } |
| 103 | + log(...data: unknown[]): void { |
| 104 | + if (this.#stdout === undefined) { |
| 105 | + originalConsole.log(...data); |
| 106 | + } else { |
| 107 | + this.value(this.#stdout, data); |
| 108 | + } |
| 109 | + } |
| 110 | + table(tabularData?: unknown, properties?: string[]): void { |
| 111 | + originalConsole.table(tabularData, properties); |
| 112 | + } |
| 113 | + time(label?: string): void { |
| 114 | + originalConsole.time(label); |
| 115 | + } |
| 116 | + timeEnd(label?: string): void { |
| 117 | + originalConsole.timeEnd(label); |
| 118 | + } |
| 119 | + timeLog(label?: string, ...data: unknown[]): void { |
| 120 | + originalConsole.timeLog(label, ...data); |
| 121 | + } |
| 122 | + timeStamp(label?: string): void { |
| 123 | + originalConsole.timeStamp(label); |
| 124 | + } |
| 125 | + trace(...data: unknown[]): void { |
| 126 | + if (this.#stdout === undefined) { |
| 127 | + originalConsole.trace(...data); |
| 128 | + } else { |
| 129 | + this.value(this.#stdout, data); |
| 130 | + } |
| 131 | + } |
| 132 | + warn(...data: unknown[]): void { |
| 133 | + if (this.#stderr === undefined) { |
| 134 | + originalConsole.warn(...data); |
| 135 | + } else { |
| 136 | + this.value(this.#stderr, data); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments