-
Notifications
You must be signed in to change notification settings - Fork 66
/
fancy.js
83 lines (66 loc) · 1.84 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* eslint-disable no-console */
import chalk from 'chalk';
import { renderBar, colorize, ellipsisLeft } from '../utils/cli';
import { formatRequest } from '../utils/webpack';
import { BULLET, TICK, CROSS, CIRCLE_OPEN } from '../utils/consts';
import LogUpdate from '../utils/log-update';
const logUpdate = new LogUpdate();
let lastRender = Date.now();
export default class FancyReporter {
allDone() {
logUpdate.done();
}
done(context) {
this._renderStates(context.statesArray);
if (context.hasErrors) {
logUpdate.done();
}
}
progress(context) {
if (Date.now() - lastRender > 50) {
this._renderStates(context.statesArray);
}
}
_renderStates(statesArray) {
lastRender = Date.now();
const renderedStates = statesArray
.map((c) => this._renderState(c))
.join('\n\n');
logUpdate.render('\n' + renderedStates + '\n');
}
_renderState(state) {
const color = colorize(state.color);
let line1;
let line2;
if (state.progress >= 0 && state.progress < 100) {
// Running
line1 = [
color(BULLET),
color(state.name),
renderBar(state.progress, state.color),
state.message,
`(${state.progress || 0}%)`,
chalk.grey(state.details[0] || ''),
chalk.grey(state.details[1] || ''),
].join(' ');
line2 = state.request
? ' ' +
chalk.grey(
ellipsisLeft(formatRequest(state.request), logUpdate.columns)
)
: '';
} else {
let icon = ' ';
if (state.hasErrors) {
icon = CROSS;
} else if (state.progress === 100) {
icon = TICK;
} else if (state.progress === -1) {
icon = CIRCLE_OPEN;
}
line1 = color(`${icon} ${state.name}`);
line2 = chalk.grey(' ' + state.message);
}
return line1 + '\n' + line2;
}
}