-
Notifications
You must be signed in to change notification settings - Fork 27k
/
log.ts
77 lines (62 loc) · 1.78 KB
/
log.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { bold, green, magenta, red, yellow, white } from '../../lib/picocolors'
export const prefixes = {
wait: white(bold('○')),
error: red(bold('⨯')),
warn: yellow(bold('⚠')),
ready: '▲', // no color
info: white(bold(' ')),
event: green(bold('✓')),
trace: magenta(bold('»')),
} as const
const LOGGING_METHOD = {
log: 'log',
warn: 'warn',
error: 'error',
} as const
function prefixedLog(prefixType: keyof typeof prefixes, ...message: any[]) {
if ((message[0] === '' || message[0] === undefined) && message.length === 1) {
message.shift()
}
const consoleMethod: keyof typeof LOGGING_METHOD =
prefixType in LOGGING_METHOD
? LOGGING_METHOD[prefixType as keyof typeof LOGGING_METHOD]
: 'log'
const prefix = prefixes[prefixType]
// If there's no message, don't print the prefix but a new line
if (message.length === 0) {
console[consoleMethod]('')
} else {
console[consoleMethod](' ' + prefix, ...message)
}
}
export function bootstrap(...message: any[]) {
console.log(' ', ...message)
}
export function wait(...message: any[]) {
prefixedLog('wait', ...message)
}
export function error(...message: any[]) {
prefixedLog('error', ...message)
}
export function warn(...message: any[]) {
prefixedLog('warn', ...message)
}
export function ready(...message: any[]) {
prefixedLog('ready', ...message)
}
export function info(...message: any[]) {
prefixedLog('info', ...message)
}
export function event(...message: any[]) {
prefixedLog('event', ...message)
}
export function trace(...message: any[]) {
prefixedLog('trace', ...message)
}
const warnOnceMessages = new Set()
export function warnOnce(...message: any[]) {
if (!warnOnceMessages.has(message[0])) {
warnOnceMessages.add(message.join(' '))
warn(...message)
}
}