Skip to content

Commit 9c61390

Browse files
committed
feat($core): more concise logger
1 parent 975f568 commit 9c61390

File tree

10 files changed

+92
-64
lines changed

10 files changed

+92
-64
lines changed

packages/@vuepress/core/lib/build.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
1515
const { createBundleRenderer } = require('vue-server-renderer')
1616
const { normalizeHeadTag, applyUserWebpackConfig } = require('./util/index')
1717

18-
logger.wait('\nExtracting site metadata...')
18+
logger.wait('Extracting site metadata...')
1919
const ctx = await prepare(sourceDir, cliOptions, true /* isProd */)
2020

2121
const { outDir, cwd } = ctx
@@ -82,7 +82,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
8282

8383
// DONE.
8484
const relativeDir = path.relative(cwd, outDir)
85-
logger.success(`\n${chalk.green('Success!')} Generated static files in ${chalk.cyan(relativeDir)}.\n`)
85+
logger.success(`${chalk.green('Success!')} Generated static files in ${chalk.cyan(relativeDir)}.\n`)
8686

8787
// --- helpers ---
8888

packages/@vuepress/core/lib/dev.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = async function dev (sourceDir, cliOptions = {}) {
1919
const { applyUserWebpackConfig } = require('./util/index')
2020
const { frontmatterEmitter } = require('@vuepress/markdown-loader')
2121

22-
logger.wait('\nExtracting site metadata...')
22+
logger.wait('Extracting site metadata...')
2323
const ctx = await prepare(sourceDir, cliOptions, false /* isProd */)
2424

2525
// setup watchers to update options and dynamically generated files

packages/@vuepress/core/lib/plugin-api/index.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module.exports = class PluginAPI {
6262
if (plugin.enabled) {
6363
this.applyPlugin(plugin)
6464
} else {
65-
logger.debug(`\n${chalk.gray(`[${plugin.name}]`)} disabled.`)
65+
logger.debug(`${chalk.gray(`[${plugin.name}]`)} disabled.`)
6666
}
6767
})
6868
}
@@ -98,7 +98,7 @@ module.exports = class PluginAPI {
9898
this._pluginQueue.push(plugin)
9999

100100
if (plugin.plugins) {
101-
logger.debug(`\nStart to use plugins defined at ${chalk.gray(plugin.name)}`)
101+
logger.debug(`Start to use plugins defined at ${chalk.gray(plugin.name)}`)
102102
logger.debug(JSON.stringify(plugin.plugins, null, 2))
103103
this.useByPluginsConfig(plugin.plugins)
104104
}
@@ -211,10 +211,12 @@ module.exports = class PluginAPI {
211211
alias
212212
}) {
213213
const isInternalPlugin = pluginName.startsWith('@vuepress/internal-')
214-
if (shortcut) {
215-
logger.tip(`\nApply plugin ${chalk.magenta(shortcut)} ${chalk.gray(`(i.e. "${pluginName}")`)} ...`)
216-
} else if (!isInternalPlugin || isDebug) {
217-
logger.tip(`\nApply plugin ${chalk.magenta(pluginName)} ...`)
214+
if (isDebug && !isInternalPlugin) {
215+
logger.tip(
216+
shortcut
217+
? `Apply plugin ${chalk.magenta(shortcut)} ${chalk.gray(`(i.e. "${pluginName}")`)} ...`
218+
: `Apply plugin ${chalk.magenta(pluginName)} ...`
219+
)
218220
}
219221

220222
this

packages/@vuepress/core/lib/prepare/AppContext.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ module.exports = class AppContext {
229229
defaultDevTemplate
230230
])
231231

232-
logger.debug('\nSSR Template File: ' + chalk.gray(ssrTemplate))
233-
logger.debug('\nDEV Template File: ' + chalk.gray(devTemplate))
232+
logger.debug('SSR Template File: ' + chalk.gray(ssrTemplate))
233+
logger.debug('DEV Template File: ' + chalk.gray(devTemplate))
234234
this.devTemplate = devTemplate
235235
this.ssrTemplate = ssrTemplate
236236
}
@@ -346,7 +346,7 @@ function createTemp (tempPath) {
346346
fs.emptyDirSync(tempPath)
347347
}
348348

349-
logger.tip(`\nTemp directory: ${chalk.gray(tempPath)}`)
349+
logger.tip(`Temp directory: ${chalk.gray(tempPath)}`)
350350
const tempCache = new Map()
351351

352352
async function writeTemp (file, content) {

packages/@vuepress/core/lib/prepare/CacheLoader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ exports.getCacheLoaderOptions = function (siteConfig, cliOptions, cwd, isProd) {
5454
)
5555
})
5656

57-
logger.debug('\nCache directory: ' + chalk.gray(cacheDirectory))
58-
logger.debug('\nCache identifier : ' + chalk.gray(cacheIdentifier))
57+
logger.debug('Cache directory: ' + chalk.gray(cacheDirectory))
58+
logger.debug('Cache identifier : ' + chalk.gray(cacheIdentifier))
5959

6060
return { cacheDirectory, cacheIdentifier }
6161
}

packages/@vuepress/core/lib/prepare/loadTheme.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module.exports = async function loadTheme (ctx) {
4848

4949
if (useLocalTheme) {
5050
themePath = localThemePath
51-
logger.tip(`\nApply theme located at ${chalk.gray(themePath)}...`)
51+
logger.tip(`Apply theme located at ${chalk.gray(themePath)}...`)
5252
} else if (isString(theme)) {
5353
const resolved = themeResolver.resolve(theme, sourceDir)
5454
const { entry, name, shortcut } = resolved
@@ -60,7 +60,7 @@ module.exports = async function loadTheme (ctx) {
6060
themePath = normalizeThemePath(resolved)
6161
themeName = name
6262
themeShortcut = shortcut
63-
logger.tip(`\nApply theme ${chalk.gray(themeName)}`)
63+
logger.tip(`Apply theme ${chalk.gray(themeName)}`)
6464
} else {
6565
throw new Error(`[vuepress] You must specify a theme, or create a local custom theme. \n For more details, refer to https://vuepress.vuejs.org/guide/custom-themes.html#custom-themes. \n`)
6666
}

packages/@vuepress/core/lib/webpack/DevLogPlugin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = class DevLogPlugin {
2525
const displayUrl = `http://${displayHost}:${port}${publicPath}`
2626

2727
logger.success(
28-
`\n${chalk.gray(`[${time}]`)} Build ${chalk.italic(stats.hash.slice(0, 6))} ` +
28+
`${chalk.gray(`[${time}]`)} Build ${chalk.italic(stats.hash.slice(0, 6))} ` +
2929
`finished in ${stats.endTime - stats.startTime} ms! ` +
3030
(
3131
isFirst

packages/@vuepress/core/lib/webpack/createBaseConfig.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ module.exports = function createBaseConfig ({
7676
.noParse(/^(vue|vue-router|vuex|vuex-router-sync)$/)
7777

7878
if (cache === false) {
79-
logger.tip('\nClean cache...\n')
79+
logger.tip('Clean cache...\n')
8080
fs.emptyDirSync(cacheDirectory)
8181
}
8282

packages/@vuepress/plugin-pwa/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = (options, context) => ({
3535
const { outDir } = context
3636
const swFilePath = path.resolve(outDir, 'service-worker.js')
3737
if (serviceWorker) {
38-
logger.wait('\nGenerating service worker...')
38+
logger.wait('Generating service worker...')
3939
const wbb = require('workbox-build')
4040
await wbb.generateSW({
4141
swDest: swFilePath,
+71-45
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,82 @@
1+
'use strict'
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
17
const chalk = require('chalk')
2-
const env = require('./env')
3-
4-
const logger = {}
5-
6-
const logTypes = {
7-
success: {
8-
color: 'green',
9-
label: 'DONE'
10-
},
11-
error: {
12-
color: 'red',
13-
label: 'FAIL'
14-
},
15-
warn: {
16-
color: 'yellow',
17-
label: 'WARN'
18-
},
19-
tip: {
20-
color: 'cyan',
21-
label: 'TIP'
22-
},
23-
wait: {
24-
color: 'blue',
25-
label: 'WAIT'
8+
9+
class Logger {
10+
constructor (options) {
11+
this.options = Object.assign(
12+
{
13+
logLevel: 3
14+
},
15+
options
16+
)
2617
}
27-
}
2818

29-
const getLoggerFn = (color, label) => (msg, log = true) => {
30-
let newLine = false
31-
if (msg.startsWith('\n')) {
32-
if (log) msg = msg.slice(1)
33-
newLine = true
19+
setOptions (options) {
20+
Object.assign(this.options, options)
21+
}
22+
23+
// level: 4
24+
debug (...args) {
25+
if (this.options.logLevel < 4) {
26+
return
27+
}
28+
29+
this.status('magenta', 'debug', ...args)
3430
}
35-
msg = chalk.reset.inverse.bold[color](` ${label} `) + ' ' + msg
36-
if (log) {
37-
console.log(newLine ? '\n' + msg : msg)
38-
} else {
39-
return msg
31+
32+
// level: 2
33+
warn (...args) {
34+
if (this.options.logLevel < 2) {
35+
return
36+
}
37+
console.warn(chalk.yellow('warning'), ...args)
4038
}
41-
}
4239

43-
for (const type in logTypes) {
44-
const { color, label } = logTypes[type]
45-
logger[type] = getLoggerFn(color, label)
46-
}
40+
// level: 1
41+
error (...args) {
42+
if (this.options.logLevel < 1) {
43+
return
44+
}
45+
process.exitCode = process.exitCode || 1
46+
console.error(chalk.red('error'), ...args)
47+
}
48+
49+
// level: 3
50+
success (...args) {
51+
this.status('green', 'success', ...args)
52+
}
53+
54+
// level: 3
55+
tip (...args) {
56+
this.status('blue', 'tip', ...args)
57+
}
4758

48-
const debugFn = getLoggerFn('magenta', 'DEBUG')
59+
// level: 3
60+
info (...args) {
61+
this.status('cyan', 'info', ...args)
62+
}
63+
64+
wait (...args) {
65+
this.status('cyan', 'wait', ...args)
66+
}
4967

50-
logger.debug = function (msg) {
51-
if (env.isDebug) {
52-
debugFn(msg)
68+
// level: 3
69+
status (color, label, ...args) {
70+
if (this.options.logLevel < 3) {
71+
return
72+
}
73+
console.log(chalk[color](label), ...args)
5374
}
5475
}
5576

56-
module.exports = logger
77+
/**
78+
* Expose a logger instance.
79+
*/
80+
81+
module.exports = new Logger()
82+

0 commit comments

Comments
 (0)