forked from unjs/consola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfancy.js
62 lines (51 loc) · 1.48 KB
/
fancy.js
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
import chalk from 'chalk'
import figures from 'figures'
import startCase from 'lodash/startCase'
const NS_SEPARATOR = chalk.blue(figures(' › '))
const ICONS = {
start: figures('●'),
info: figures('ℹ'),
success: figures('✔'),
error: figures('✖'),
fatal: figures('✖'),
warn: figures('⚠'),
debug: figures('…'),
trace: figures('…'),
default: figures('❯'),
ready: figures('♥')
}
export default class FancyReporter {
constructor (stream, options = {}) {
this.stream = stream || process.stderr
}
formatBadge (type, color = 'blue') {
return chalk['bg' + startCase(color)].black(` ${type.toUpperCase()} `) + ' '
}
formatTag (type, color = 'blue') {
const icon = ICONS[type] || ICONS.default
return chalk[color](`${icon} ${type.toLowerCase()}`) + ' '
}
clear () {
this.stream.write('\x1b[2J\x1b[0f')
}
log (logObj) {
let message = logObj.message
if (logObj.scope) {
message =
(logObj.scope.replace(/:/g, '>') + '>').split('>').join(NS_SEPARATOR) +
message
}
if (logObj.clear) {
this.clear()
}
if (logObj.badge) {
this.stream.write('\n\n' + this.formatBadge(logObj.type, logObj.color) + message + '\n\n')
} else {
this.stream.write(this.formatTag(logObj.type, logObj.color) + message + '\n')
}
if (logObj.additional) {
const lines = logObj.additional.split('\n').map(s => ' ' + s).join('\n')
this.stream.write(chalk.grey(lines) + '\n')
}
}
}