|
| 1 | +/* eslint no-console: 0 */ |
| 2 | +// copied and adapted from Vite (MIT Licensed) |
| 3 | + |
| 4 | +import readline from 'node:readline' |
| 5 | +import colors from 'picocolors' |
| 6 | + |
| 7 | +export type LogType = 'error' | 'warn' | 'info' |
| 8 | +export type LogLevel = LogType | 'silent' |
| 9 | +export interface Logger { |
| 10 | + info(msg: string, options?: LogOptions): void |
| 11 | + warn(msg: string, options?: LogOptions): void |
| 12 | + warnOnce(msg: string, options?: LogOptions): void |
| 13 | + error(msg: string, options?: LogErrorOptions): void |
| 14 | + clearScreen(type: LogType): void |
| 15 | + hasErrorLogged(error: Error): boolean |
| 16 | + hasWarned: boolean |
| 17 | +} |
| 18 | + |
| 19 | +export interface LogOptions { |
| 20 | + clear?: boolean |
| 21 | + timestamp?: boolean |
| 22 | + environment?: string |
| 23 | +} |
| 24 | + |
| 25 | +export interface LogErrorOptions extends LogOptions { |
| 26 | + error?: Error | null |
| 27 | +} |
| 28 | + |
| 29 | +export const LogLevels: Record<LogLevel, number> = { |
| 30 | + silent: 0, |
| 31 | + error: 1, |
| 32 | + warn: 2, |
| 33 | + info: 3, |
| 34 | +} |
| 35 | + |
| 36 | +let lastType: LogType | undefined |
| 37 | +let lastMsg: string | undefined |
| 38 | +let sameCount = 0 |
| 39 | + |
| 40 | +function clearScreen() { |
| 41 | + const repeatCount = process.stdout.rows - 2 |
| 42 | + const blank = repeatCount > 0 ? '\n'.repeat(repeatCount) : '' |
| 43 | + console.log(blank) |
| 44 | + readline.cursorTo(process.stdout, 0, 0) |
| 45 | + readline.clearScreenDown(process.stdout) |
| 46 | +} |
| 47 | + |
| 48 | +export interface LoggerOptions { |
| 49 | + prefix?: string |
| 50 | + allowClearScreen?: boolean |
| 51 | + customLogger?: Logger |
| 52 | + console?: Console |
| 53 | +} |
| 54 | + |
| 55 | +// Only initialize the timeFormatter when the timestamp option is used, and |
| 56 | +// reuse it across all loggers |
| 57 | +let timeFormatter: Intl.DateTimeFormat |
| 58 | +function getTimeFormatter() { |
| 59 | + timeFormatter ??= new Intl.DateTimeFormat(undefined, { |
| 60 | + hour: 'numeric', |
| 61 | + minute: 'numeric', |
| 62 | + second: 'numeric', |
| 63 | + }) |
| 64 | + return timeFormatter |
| 65 | +} |
| 66 | + |
| 67 | +export function createLogger( |
| 68 | + level: LogLevel = 'info', |
| 69 | + options: LoggerOptions = {}, |
| 70 | +): Logger { |
| 71 | + if (options.customLogger) { |
| 72 | + return options.customLogger |
| 73 | + } |
| 74 | + |
| 75 | + const loggedErrors = new WeakSet<Error>() |
| 76 | + const { |
| 77 | + prefix = '[vite]', |
| 78 | + allowClearScreen = true, |
| 79 | + console = globalThis.console, |
| 80 | + } = options |
| 81 | + const thresh = LogLevels[level] |
| 82 | + const canClearScreen = |
| 83 | + allowClearScreen && process.stdout.isTTY && !process.env.CI |
| 84 | + const clear = canClearScreen ? clearScreen : () => { } |
| 85 | + |
| 86 | + function format(type: LogType, msg: string, options: LogErrorOptions = {}) { |
| 87 | + if (options.timestamp) { |
| 88 | + let tag = '' |
| 89 | + if (type === 'info') { |
| 90 | + tag = colors.cyan(colors.bold(prefix)) |
| 91 | + } else if (type === 'warn') { |
| 92 | + tag = colors.yellow(colors.bold(prefix)) |
| 93 | + } else { |
| 94 | + tag = colors.red(colors.bold(prefix)) |
| 95 | + } |
| 96 | + const environment = options.environment ? options.environment + ' ' : '' |
| 97 | + return `${colors.dim(getTimeFormatter().format(new Date()))} ${tag} ${environment}${msg}` |
| 98 | + } else { |
| 99 | + return msg |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + function output(type: LogType, msg: string, options: LogErrorOptions = {}) { |
| 104 | + if (thresh >= LogLevels[type]) { |
| 105 | + const method = type === 'info' ? 'log' : type |
| 106 | + |
| 107 | + if (options.error) { |
| 108 | + loggedErrors.add(options.error) |
| 109 | + } |
| 110 | + if (canClearScreen) { |
| 111 | + if (type === lastType && msg === lastMsg) { |
| 112 | + sameCount++ |
| 113 | + clear() |
| 114 | + console[method]( |
| 115 | + format(type, msg, options), |
| 116 | + colors.yellow(`(x${sameCount + 1})`), |
| 117 | + ) |
| 118 | + } else { |
| 119 | + sameCount = 0 |
| 120 | + lastMsg = msg |
| 121 | + lastType = type |
| 122 | + if (options.clear) { |
| 123 | + clear() |
| 124 | + } |
| 125 | + console[method](format(type, msg, options)) |
| 126 | + } |
| 127 | + } else { |
| 128 | + console[method](format(type, msg, options)) |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + const warnedMessages = new Set<string>() |
| 134 | + |
| 135 | + const logger: Logger = { |
| 136 | + hasWarned: false, |
| 137 | + info(msg, opts) { |
| 138 | + output('info', msg, opts) |
| 139 | + }, |
| 140 | + warn(msg, opts) { |
| 141 | + logger.hasWarned = true |
| 142 | + output('warn', msg, opts) |
| 143 | + }, |
| 144 | + warnOnce(msg, opts) { |
| 145 | + if (warnedMessages.has(msg)) return |
| 146 | + logger.hasWarned = true |
| 147 | + output('warn', msg, opts) |
| 148 | + warnedMessages.add(msg) |
| 149 | + }, |
| 150 | + error(msg, opts) { |
| 151 | + logger.hasWarned = true |
| 152 | + output('error', msg, opts) |
| 153 | + }, |
| 154 | + clearScreen(type) { |
| 155 | + if (thresh >= LogLevels[type]) { |
| 156 | + clear() |
| 157 | + } |
| 158 | + }, |
| 159 | + hasErrorLogged(error) { |
| 160 | + return loggedErrors.has(error) |
| 161 | + }, |
| 162 | + } |
| 163 | + |
| 164 | + return logger |
| 165 | +} |
0 commit comments