rewrite progress

This commit is contained in:
Pooya Parsa 2018-03-13 14:03:02 +03:30
parent 388db1c2d1
commit 436b8b73b9
3 changed files with 55 additions and 44 deletions

View File

@ -94,7 +94,7 @@ module.exports = class Builder {
} }
this._buildStatus = STATUS.BUILDING this._buildStatus = STATUS.BUILDING
this.spinner.start('Starting build') this.spinner.start('Initializing builder')
// Wait for nuxt ready // Wait for nuxt ready
await this.nuxt.ready() await this.nuxt.ready()
@ -119,7 +119,8 @@ module.exports = class Builder {
} }
} }
this.spinner.succeed() this.spinner.succeed('Builder ready')
this.spinner.start(`Generating nuxt files...`) this.spinner.start(`Generating nuxt files...`)
debug(`App root: ${this.options.srcDir}`) debug(`App root: ${this.options.srcDir}`)
@ -134,9 +135,9 @@ module.exports = class Builder {
// Generate routes and interpret the template files // Generate routes and interpret the template files
await this.generateRoutesAndFiles() await this.generateRoutesAndFiles()
this.spinner.succeed() this.spinner.succeed('Files genrated')
this.spinner.info('Compiling...') this.spinner.start('Compiling...')
// Start webpack build // Start webpack build
await this.webpackBuild() await this.webpackBuild()

View File

@ -133,9 +133,9 @@ module.exports = function webpackBaseConfig({ name, isServer }) {
config.plugins.push(new webpack.ProgressPlugin({ profile: true })) config.plugins.push(new webpack.ProgressPlugin({ profile: true }))
} else { } else {
config.plugins.push(new ProgressPlugin({ config.plugins.push(new ProgressPlugin({
color: isServer ? 'yellow' : 'green', spinner: this.spinner,
pcolor: isServer ? 'gradient(red,yellow)' : 'gradient(green,cyan)', name: isServer ? 'server' : 'client',
title: isServer ? 'SERVER' : 'CLIENT' color: isServer ? 'green' : 'orange'
})) }))
} }

View File

@ -1,6 +1,10 @@
const ProgressBar = require('node-progress-bars')
const webpack = require('webpack') const webpack = require('webpack')
const throttle = require('lodash/throttle') const chalk = require('chalk')
const _ = require('lodash')
const sharedState = {}
const BLOCK_CHAR = '█'
module.exports = class ProgressPlugin extends webpack.ProgressPlugin { module.exports = class ProgressPlugin extends webpack.ProgressPlugin {
constructor(options) { constructor(options) {
@ -8,52 +12,58 @@ module.exports = class ProgressPlugin extends webpack.ProgressPlugin {
this.handler = (percent, msg) => this.updateProgress(percent, msg) this.handler = (percent, msg) => this.updateProgress(percent, msg)
this.options = options || {} this.options = options
this.lastUpdate = 0 if (!sharedState[options.name]) {
sharedState[options.name] = {
color: options.color
}
}
// BUG: plugin.appy is being called twice! this.spinner = options.spinner
// So initialize progress here }
this.startProgress()
get state() {
return sharedState[this.options.name]
} }
updateProgress(percent, msg) { updateProgress(percent, msg) {
if (!this.bar) { const progress = Math.floor(percent * 100)
this.startProgress()
}
if (percent === 1) { this.state.progress = progress
this.stopProgress() this.state.succeed =
return this.state.msg = msg
}
this.bar.update(percent, { msg }) // Update spinner using shared state
} let isInProgress = false
const width = 25
let line = _.range(width).fill(chalk.white(BLOCK_CHAR))
let additional = []
startProgress() { Object.keys(sharedState).forEach(name => {
if (this.bar) { const state = sharedState[name]
this.stopProgress()
}
// https://github.com/bubkoo/ascii-progress if (state.progress >= 100) {
this.bar = new ProgressBar({ return
schema: `[${this.options.title}].${this.options.color} :filled.${this.options.pcolor}:blank.white :msg.grey`, }
filled: '█',
blank: '█', isInProgress = true
total: 100,
width: 25, const w = state.progress * (width / 100)
clear: true const b = chalk.keyword(state.color)(BLOCK_CHAR)
for (let i = 0; i < w; i++) {
line[i] = b
}
additional.push(`${b} ${name} (${state.progress}%) `)
}) })
this.bar.update = throttle(this.bar.update, 50) if (isInProgress) {
} this.spinner.start()
this.spinner.text = 'Compiling ' + line.join('') + ' ' + additional.join(' ')
stopProgress() { } else {
if (!this.bar) { this.spinner.succeed('Compiled ' + this.options.name)
return
} }
this.bar.terminate()
this.bar = undefined
} }
} }