refactor: make progress plugin more dynamic for adding log type

This commit is contained in:
Clark Du 2018-03-13 11:08:55 +08:00
parent 061718c66d
commit 354cf88a97
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9

View File

@ -30,15 +30,19 @@ module.exports = function ProgressPlugin({ color, pcolor, title }) {
let consoleSpied = false
const silentConsole = {}
const originalConsole = {}
const consoleQueue = {
log: [],
warn: [],
error: []
}
const silentConsole = {
log: (...args) => consoleQueue.log.push(args),
error: (...args) => consoleQueue.error.push(args)
}
// 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 = () => {
if (consoleSpied) return
@ -47,22 +51,15 @@ const muteConsole = () => {
Object.assign(console, silentConsole)
}
const originalConsole = {
log: console.log, // eslint-disable-line no-console
error: console.error // eslint-disable-line no-console
}
const restoreConsole = () => {
if (!consoleSpied) return
consoleSpied = false
Object.assign(console, originalConsole)
const l = consoleQueue.log
consoleQueue.log = []
l.forEach(args => console.log(...args)) // eslint-disable-line no-console
const e = consoleQueue.error
consoleQueue.error = []
e.forEach(args => console.error(...args)) // eslint-disable-line no-console
// level: log, warn, error
for (let level in consoleQueue) {
consoleQueue[level].forEach(args => console[level](...args)) // eslint-disable-line no-console
consoleQueue[level] = []
}
}