Nuxt/lib/builder/webpack/plugins/progress.js

125 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-03-11 23:15:14 +00:00
const ProgressBar = require('node-progress-bars')
const webpack = require('webpack')
2018-03-13 08:29:34 +00:00
const throttle = require('lodash/debounce')
2018-03-11 23:15:14 +00:00
2018-03-13 08:29:34 +00:00
module.exports = class ProgressPlugin extends webpack.ProgressPlugin {
constructor(options) {
super(options)
if (typeof options === 'function') {
options = {
handler: options
}
}
this.handler = (percent, msg) => this.updateProgress(percent, msg)
this.options = options || {}
this.lastUpdate = 0
// BUG: plugin.appy is being called twice!
// So initialize progress here
this.startProgress()
this.lastPrgoress = 0
this.lastMsg = ''
}
2018-03-12 15:16:08 +00:00
2018-03-13 08:29:34 +00:00
updateProgress(percent, msg) {
if (!this.bar) {
this.startProgress()
2018-03-12 15:16:08 +00:00
}
2018-03-13 08:29:34 +00:00
const progress = Math.floor(percent * 100)
2018-03-12 15:16:08 +00:00
2018-03-13 08:29:34 +00:00
if (progress === this.lastPrgoress || msg === this.lastMsg) {
return
}
2018-03-12 15:16:08 +00:00
2018-03-13 08:29:34 +00:00
this.lastPrgoress = progress
this.lastMsg = msg
2018-03-12 15:16:08 +00:00
2018-03-13 08:29:34 +00:00
if (percent === 1) {
this.stopProgress()
return
}
2018-03-12 15:16:08 +00:00
2018-03-13 08:29:34 +00:00
this.bar.update(percent, { msg })
}
2018-03-12 15:16:08 +00:00
2018-03-13 08:29:34 +00:00
startProgress() {
if (this.bar) {
this.stopProgress()
}
2018-03-12 15:16:08 +00:00
2018-03-13 08:29:34 +00:00
// https://github.com/bubkoo/ascii-progress
this.bar = new ProgressBar({
schema: `${this.options.title}.${this.options.color} >.grey :filled.${this.options.pcolor}:blank.white :msg.grey`,
filled: '█',
blank: '█',
total: 100,
width: 25,
clear: true
})
2018-03-12 15:16:08 +00:00
2018-03-13 08:29:34 +00:00
this.bar.update = throttle(this.bar.update, 50)
}
stopProgress() {
if (!this.bar) {
return
}
2018-03-12 15:16:08 +00:00
2018-03-13 08:29:34 +00:00
this.bar.terminate()
this.bar = undefined
}
2018-03-12 15:16:08 +00:00
}
2018-03-13 08:29:34 +00:00
// -----------------------------------------------------------
// Shared console utils
// -----------------------------------------------------------
// let consoleSpied = 0
// const silentConsole = {}
// const originalConsole = {}
// const consoleQueue = {
// log: [],
// warn: [],
// error: []
// }
// // level: log, warn, error
// Object.keys(consoleQueue).forEach((level) => {
// silentConsole[level] = (...args) => consoleQueue[level].push(args)
// originalConsole[level] = console[level] // eslint-disable-line no-console
// })
// const muteConsole = () => {
// // eslint-disable-next-line no-console
// if (console.spiedInTest) {
// return
// }
// consoleSpied++
// Object.assign(console, silentConsole)
// }
// const restoreConsole = () => {
// if (consoleSpied === 0) {
// return
// }
// consoleSpied--
// Object.assign(console, originalConsole)
// // level: log, warn, error
// for (let level in consoleQueue) {
// const q = consoleQueue[level]
// consoleQueue[level] = []
// q.forEach(args => console[level](...args)) // eslint-disable-line no-console
// }
// }