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

90 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-03-16 16:12:06 +00:00
import webpack from 'webpack'
import chalk from 'chalk'
import _ from 'lodash'
2018-03-22 19:29:05 +00:00
import logUpdate from 'log-update'
2018-03-13 10:33:02 +00:00
const sharedState = {}
2018-03-22 19:29:05 +00:00
const BAR_LENGTH = 25
2018-03-22 19:56:35 +00:00
const IS_WINDOWS = /^win/.test(process.platform)
const BLOCK_CHAR = IS_WINDOWS ? ' ' : '█'
2018-03-22 20:05:22 +00:00
const BLOCK_CHAR2 = IS_WINDOWS ? '=' : '█'
const ICON_CHAR = IS_WINDOWS ? ':' : '⠸'
const BAR_BEFORE = IS_WINDOWS ? chalk.grey('[') : ''
const BAR_AFTER = IS_WINDOWS ? chalk.grey(']') : ''
2018-03-11 23:15:14 +00:00
2018-03-16 16:12:06 +00:00
export default class ProgressPlugin extends webpack.ProgressPlugin {
2018-03-13 08:29:34 +00:00
constructor(options) {
super(options)
2018-03-16 06:45:31 +00:00
this.handler = (percent, msg, ...details) => this.updateProgress(percent, msg, details)
2018-03-13 08:29:34 +00:00
2018-03-22 19:29:05 +00:00
this.handler = _.throttle(this.handler, 25, { leading: true, trailing: true })
2018-03-13 10:33:02 +00:00
this.options = options
if (!sharedState[options.name]) {
sharedState[options.name] = {
color: options.color
}
}
2018-03-22 19:29:05 +00:00
}
apply(compiler) {
super.apply(compiler)
2018-03-13 08:29:34 +00:00
2018-03-22 19:29:05 +00:00
compiler.hooks.done.tap('progress', () => logUpdate.clear())
2018-03-13 10:33:02 +00:00
}
2018-03-13 08:29:34 +00:00
2018-03-13 10:33:02 +00:00
get state() {
return sharedState[this.options.name]
}
2018-03-12 15:16:08 +00:00
2018-03-16 06:45:31 +00:00
updateProgress(percent, msg, details) {
2018-03-13 10:33:02 +00:00
const progress = Math.floor(percent * 100)
2018-03-12 15:16:08 +00:00
2018-03-13 10:33:02 +00:00
this.state.progress = progress
2018-03-22 19:29:05 +00:00
this.state.msg = (msg && msg.length) ? msg : 'wait'
2018-03-12 15:16:08 +00:00
2018-03-13 11:03:01 +00:00
// Process all states
let inProgress = false
2018-03-22 19:29:05 +00:00
const lines = []
2018-03-12 15:16:08 +00:00
2018-03-22 19:29:05 +00:00
_.sortBy(Object.keys(sharedState), s => s.name).forEach(name => {
2018-03-13 10:33:02 +00:00
const state = sharedState[name]
2018-03-12 15:16:08 +00:00
2018-03-13 11:03:01 +00:00
if (state.progress < 100) {
inProgress = true
2018-03-13 10:33:02 +00:00
}
2018-03-12 15:16:08 +00:00
2018-03-22 19:29:05 +00:00
const _color = chalk.keyword(state.color)
2018-03-13 08:29:34 +00:00
const _icon = _color(ICON_CHAR)
2018-03-22 19:29:05 +00:00
const _name = _color(_.startCase(name))
const _bar = this._renderBar(state.progress, state.color)
const _msg = chalk.grey(_.startCase(state.msg))
const _progress = chalk.grey('(' + state.progress + '%)')
2018-03-12 15:16:08 +00:00
2018-03-22 19:29:05 +00:00
lines.push([_icon, _name, _bar, _msg, _progress].join(' '))
2018-03-13 10:33:02 +00:00
})
2018-03-13 11:03:01 +00:00
if (!inProgress) {
2018-03-22 19:29:05 +00:00
logUpdate.clear()
} else {
logUpdate('\n' + lines.join('\n') + '\n')
2018-03-13 10:33:02 +00:00
}
2018-03-22 19:29:05 +00:00
}
2018-03-13 11:03:01 +00:00
2018-03-22 19:29:05 +00:00
_renderBar(progress, color) {
const w = progress * (BAR_LENGTH / 100)
const bg = chalk.white(BLOCK_CHAR)
// const fg = chalk.keyword(color)(BLOCK_CHAR)
2018-03-13 11:03:01 +00:00
const base = color === 'green' ? 120 : 50
const fg = i => chalk.hsl(i * 3 + base, 100, 50)(BLOCK_CHAR2)
2018-03-13 11:03:01 +00:00
return BAR_BEFORE + _.range(BAR_LENGTH).map(i => i < w ? fg(i) : bg).join('') + BAR_AFTER
}
2018-03-12 15:16:08 +00:00
}