-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (71 loc) · 1.4 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const readline = require('readline')
const chalk = require('chalk')
const spinners = require('cli-spinners')
const upper = require('lodash.upperfirst')
const { dots } = spinners
const interval = 100
const stream = process.stdout
let index = 0
let id = null
const print = (...args) => {
const chunk = args.join(' ') + '\n'
stream.write(chunk)
}
const clear = () => {
readline.clearLine(stream)
readline.cursorTo(stream, 0)
}
const render = () => {
clear()
stream.write(frame())
}
const frame = () => {
const { frames } = dots
let frame = frames[index]
const dot = chalk[log.color](frame)
index = ++index % frames.length
const chunk = [
log.block,
dot,
log.text
].join(' ')
return chunk
}
const log = (...args) => {
print(log.block, ...args)
}
log.error = (...args) => {
print(
chalk.black.bgRed(' err '),
chalk.red(...args)
)
}
log.start = (...args) => {
log.text = args.join(' ')
render()
id = setInterval(render.bind(log), interval)
}
log.stop = (msg = 'done', ...args) => {
clearInterval(id)
id = null
index = 0
clear()
log(msg, ...args)
}
log.color = 'cyan'
log.text = ''
let name = 'c8r'
Object.defineProperty(log, 'name', {
get: () => {
return name
},
set: next => {
name = next
}
})
Object.defineProperty(log, 'block', {
get: () => {
return chalk.black['bg' + upper(log.color)](` ${log.name} `)
}
})
module.exports = log