improve progress and cli

This commit is contained in:
Pooya Parsa 2018-03-13 11:59:34 +03:30
parent 354cf88a97
commit 1a0d263180
3 changed files with 110 additions and 52 deletions

View File

@ -136,8 +136,7 @@ module.exports = class Builder {
this.spinner.succeed() this.spinner.succeed()
this.spinner.start('Building project...') this.spinner.info('Compiling...')
this.spinner.stopAndPersist()
// Start webpack build // Start webpack build
await this.webpackBuild() await this.webpackBuild()

View File

@ -135,7 +135,7 @@ module.exports = function webpackBaseConfig({ name, isServer }) {
config.plugins.push(new ProgressPlugin({ config.plugins.push(new ProgressPlugin({
color: isServer ? 'yellow' : 'green', color: isServer ? 'yellow' : 'green',
pcolor: isServer ? 'gradient(red,yellow)' : 'gradient(green,cyan)', pcolor: isServer ? 'gradient(red,yellow)' : 'gradient(green,cyan)',
title: isServer ? 'SSR ' : 'CLIENT' title: isServer ? 'SERVER' : 'CLIENT'
})) }))
} }

View File

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