/
log.ts
56 lines (46 loc) · 1.26 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
import * as colors from 'colorette'
export const makeLabel = (
name: string | undefined,
input: string,
type: 'info' | 'success' | 'error'
) => {
return [
name && `${colors.dim('[')}${name.toUpperCase()}${colors.dim(']')}`,
colors[type === 'info' ? 'blue' : type === 'error' ? 'red' : 'green'](
input.toUpperCase()
),
]
.filter(Boolean)
.join(colors.dim(' '))
}
let silent = false
export function setSilent(isSilent?: boolean) {
silent = !!isSilent
}
export type Logger = ReturnType<typeof createLogger>
export const createLogger = (name?: string) => {
return {
setName(_name: string) {
name = _name
},
success(label: string, ...args: any[]) {
return this.log(label, 'success', ...args)
},
info(label: string, ...args: any[]) {
return this.log(label, 'info', ...args)
},
error(label: string, ...args: any[]) {
return this.log(label, 'error', ...args)
},
log(label: string, type: 'info' | 'success' | 'error', ...data: unknown[]) {
switch (type) {
case 'error': {
return console.error(makeLabel(name, label, type), ...data)
}
default:
if (silent) return
console.log(makeLabel(name, label, type), ...data)
}
},
}
}