forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.progress.js
More file actions
123 lines (101 loc) · 2.75 KB
/
plugin.progress.js
File metadata and controls
123 lines (101 loc) · 2.75 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const
{ ProgressPlugin } = require('webpack'),
throttle = require('lodash.throttle'),
{ green, grey } = require('chalk'),
log = require('../helpers/logger')('app:progress'),
logUpdate = require('log-update'),
ms = require('ms')
const
isMinimalTerminal = require('../helpers/is-minimal-terminal'),
logLine = isMinimalTerminal
? () => {}
: logUpdate.create(process.stdout, { showCursor: true })
const
compilations = {},
barLength = 25,
barItems = Array.apply(null, { length: barLength })
let maxLengthName = 0
function isRunningGlobally () {
return Object.values(compilations).find(c => c.running) !== void 0
}
function renderBar (progress, color) {
const width = progress * (barLength / 100)
return barItems
.map((_, index) => index < width ? '█' : ' ')
.join('')
}
function printState () {
const lines = Object.values(compilations).map(state => {
return [
' ' + green( state.name.padEnd(maxLengthName) + ' ' + renderBar(state.progress)),
state.msg,
`[${state.progress}%]`.padStart(4),
state.running
? grey(state.details
? [ state.details[0], state.details[1] ].filter(s => s).join(' ')
: ''
)
: state.doneStamp
].filter(m => m).join(' ') + '\n'
})
logLine('\n' + lines.join(''))
}
const render = throttle(printState, 200)
module.exports = class WebpackProgress extends ProgressPlugin {
constructor (opts = {}) {
super({
handler: (percent, msg, ...details) => {
this.updateProgress(percent, msg, details)
}
})
this.opts = opts
if (this.state) { return }
const len = opts.name.length
if (len > maxLengthName) {
maxLengthName = len
}
compilations[opts.name] = {
name: opts.name,
progress: 0,
running: false
}
}
get state () {
return compilations[this.opts.name]
}
updateProgress (percent, msg, details) {
const
progress = Math.floor(percent * 100),
wasRunning = this.state.running,
running = progress < 100
Object.assign(this.state, {
progress,
msg: running && msg ? msg : '',
details,
running
})
if (!wasRunning && running) {
this.state.startTime = +new Date()
if (isMinimalTerminal) {
log(`Compiling ${this.state.name}...`)
}
}
else if (wasRunning && !running) {
const diff = +new Date() - this.state.startTime
this.state.doneStamp = `in ~${ms(diff)}`
if (isMinimalTerminal) {
log(`Compiled ${this.state.name} ${this.state.doneStamp}`)
}
}
if (!isMinimalTerminal) {
if (running && isRunningGlobally()) {
render()
}
else {
render.cancel()
printState()
logLine.done()
}
}
}
}