|
1 | 1 | import pico from 'picocolors'; |
2 | 2 |
|
3 | | -export function logSuccess(message: string, items?: string[]): void { |
4 | | - console.log(pico.green('✔'), message); |
| 3 | +type LogLevel = 'debug' | 'error' | 'info' | 'success' | 'warning'; |
| 4 | + |
| 5 | +type LogOptions = { |
| 6 | + level: LogLevel; |
| 7 | + message: string; |
| 8 | + items?: string[]; |
| 9 | +}; |
| 10 | + |
| 11 | +function getLogLevelInfo(logLevel: LogLevel) { |
| 12 | + const identity = (text: string) => text; |
| 13 | + const infos: Record<LogLevel, [string, (text: string) => string, (text: string) => string]> = { |
| 14 | + success: ['✔', pico.green, pico.green], |
| 15 | + info: ['→', pico.blueBright, identity], |
| 16 | + warning: ['▲', pico.yellow, pico.yellow], |
| 17 | + error: ['✖', pico.red, pico.red], |
| 18 | + debug: ['✱', pico.magenta, pico.magenta], |
| 19 | + }; |
| 20 | + |
| 21 | + return { |
| 22 | + icon: infos[logLevel][0], |
| 23 | + color: infos[logLevel][1], |
| 24 | + messageColor: infos[logLevel][2], |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +const logWrapper = (level: LogLevel) => (message: string, items?: string[]) => log({ level, message, items }); |
| 29 | +export const logSuccess = logWrapper('success'); |
| 30 | +export const logError = logWrapper('error'); |
| 31 | +export const logInfo = logWrapper('info'); |
| 32 | +export const logWarning = logWrapper('warning'); |
| 33 | +export const logDebug = logWrapper('debug'); |
| 34 | + |
| 35 | +function log({ level, message, items }: LogOptions): void { |
| 36 | + const { icon, color, messageColor } = getLogLevelInfo(level); |
| 37 | + console.log(color(icon), messageColor(message)); |
5 | 38 | if (items) { |
6 | | - logItems(items, pico.green); |
| 39 | + logItems(items, color); |
7 | 40 | } |
8 | 41 | } |
9 | 42 |
|
10 | | -export function logInfo(message: string, items?: string[]): void { |
11 | | - console.log(pico.blueBright('→'), message); |
12 | | - if (items) { |
13 | | - logItems(items, pico.blueBright); |
14 | | - } |
15 | | -} |
16 | | - |
17 | | -export function logWarning(message: string, items?: string[]): void { |
18 | | - console.log(pico.yellow('▲'), message); |
19 | | - if (items) { |
20 | | - logItems(items, pico.yellow); |
21 | | - } |
22 | | -} |
23 | | - |
24 | | -export function logError(message: string, items?: string[]): void { |
25 | | - console.log(pico.red('✖'), message); |
26 | | - if (items) { |
27 | | - logItems(items, pico.red); |
28 | | - } |
29 | | -} |
30 | | - |
31 | | -export function logDebug(message: string, items?: string[]): void { |
32 | | - console.log(pico.magenta('✱'), message); |
33 | | - if (items) { |
34 | | - logItems(items, pico.magenta); |
35 | | - } |
36 | | -} |
37 | | - |
38 | | -export function logItems(items: string[], color?: (text: string) => string): void { |
| 43 | +function logItems(items: string[], color?: (text: string) => string): void { |
39 | 44 | const colorFn = color ?? (text => text); |
40 | 45 | items.forEach((item, index) => { |
41 | 46 | const prefix = index === items.length - 1 ? '└─' : '├─'; |
|
0 commit comments