-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
dotRenderer.ts
66 lines (59 loc) · 1.36 KB
/
dotRenderer.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
import { createLogUpdate } from 'log-update'
import c from 'picocolors'
import type { Task } from '../../../types'
import { clearInterval, getTests, setInterval } from '../../../utils'
export interface DotRendererOptions {
outputStream: NodeJS.WritableStream
}
const check = c.green('·')
const cross = c.red('x')
const pending = c.yellow('*')
const skip = c.dim(c.gray('-'))
function render(tasks: Task[]) {
const all = getTests(tasks)
return all.map((i) => {
if (i.mode === 'skip' || i.mode === 'todo')
return skip
switch (i.result?.state) {
case 'pass':
return check
case 'fail':
return cross
default:
return pending
}
}).join('')
}
export const createDotRenderer = (_tasks: Task[], options: DotRendererOptions) => {
let tasks = _tasks
let timer: any
const log = createLogUpdate(options.outputStream)
function update() {
log(render(tasks))
}
return {
start() {
if (timer)
return this
timer = setInterval(update, 200)
return this
},
update(_tasks: Task[]) {
tasks = _tasks
update()
return this
},
async stop() {
if (timer) {
clearInterval(timer)
timer = undefined
}
log.clear()
options.outputStream.write(`${render(tasks)}\n`)
return this
},
clear() {
log.clear()
},
}
}