Skip to content

Commit df99cb6

Browse files
committed
feat($cli): '--silent' option
1 parent 9c61390 commit df99cb6

File tree

11 files changed

+89
-38
lines changed

11 files changed

+89
-38
lines changed

packages/@vuepress/cli/index.js

+42-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports.bootstrap = function ({
3030
plugins,
3131
theme
3232
} = {}) {
33-
const { path } = require('@vuepress/shared-utils')
33+
const { path, logger, env } = require('@vuepress/shared-utils')
3434
const { dev, build, eject } = require('@vuepress/core')
3535

3636
program
@@ -46,21 +46,56 @@ exports.bootstrap = function ({
4646
.option('-c, --cache <cache>', 'set the directory of cache')
4747
.option('--no-cache', 'clean the cache before build')
4848
.option('--debug', 'start development server in debug mode')
49-
.action((dir = '.', { host, port, debug, temp, cache }) => {
50-
wrapCommand(dev)(path.resolve(dir), { host, port, debug, temp, cache, plugins, theme })
49+
.option('--silent', 'start development server in silent mode')
50+
.action((sourceDir = '.', {
51+
host,
52+
port,
53+
debug,
54+
temp,
55+
cache,
56+
silent
57+
}) => {
58+
logger.setOptions({ logLevel: silent ? 1 : debug ? 4 : 3 })
59+
env.setOptions({ isDebug: debug, isTest: process.env.NODE_ENV === 'test' })
60+
61+
wrapCommand(dev)(path.resolve(sourceDir), {
62+
host,
63+
port,
64+
temp,
65+
cache,
66+
plugins,
67+
theme
68+
})
5169
})
5270

5371
program
5472
.command('build [targetDir]')
5573
.description('build dir as static site')
56-
.option('-d, --dest <outDir>', 'specify build output dir (default: .vuepress/dist)')
74+
.option('-d, --dest <dest>', 'specify build output dir (default: .vuepress/dist)')
5775
.option('-t, --temp <temp>', 'set the directory of the temporary file')
5876
.option('-c, --cache <cache>', 'set the directory of cache')
5977
.option('--no-cache', 'clean the cache before build')
6078
.option('--debug', 'build in development mode for debugging')
61-
.action((dir = '.', { debug, dest, temp, cache }) => {
62-
const outDir = dest ? path.resolve(dest) : null
63-
wrapCommand(build)(path.resolve(dir), { debug, outDir, plugins, theme, temp, cache })
79+
.option('--silent', 'build static site in silent mode')
80+
.action((sourceDir, {
81+
debug,
82+
dest,
83+
temp,
84+
cache,
85+
silent
86+
}) => {
87+
logger.setOptions({ logLevel: silent ? 1 : debug ? 4 : 3 })
88+
env.setOptions({ isDebug: debug, isTest: process.env.NODE_ENV === 'test' })
89+
90+
wrapCommand(build)(path.resolve(sourceDir), {
91+
debug,
92+
dest,
93+
plugins,
94+
theme,
95+
temp,
96+
cache,
97+
silent
98+
})
6499
})
65100

66101
program

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
88
const readline = require('readline')
99
const escape = require('escape-html')
1010

11-
const { chalk, fs, logger } = require('@vuepress/shared-utils')
11+
const { chalk, fs, logger, env } = require('@vuepress/shared-utils')
1212
const prepare = require('./prepare/index')
1313
const createClientConfig = require('./webpack/createClientConfig')
1414
const createServerConfig = require('./webpack/createServerConfig')
@@ -99,7 +99,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
9999
reject(new Error(`Failed to compile with errors.`))
100100
return
101101
}
102-
if (cliOptions.debug && stats.hasWarnings()) {
102+
if (env.isDebug && stats.hasWarnings()) {
103103
stats.toJson().warnings.forEach(warning => {
104104
console.warn(warning)
105105
})

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ module.exports = async function dev (sourceDir, cliOptions = {}) {
8080
const { host, displayHost } = await resolveHost(cliOptions.host || ctx.siteConfig.host)
8181

8282
config
83-
.plugin('vuepress-log')
84-
.use(DevLogPlugin, [{
85-
port,
86-
displayHost,
87-
publicPath: ctx.base
88-
}])
83+
.plugin('vuepress-log')
84+
.use(DevLogPlugin, [{
85+
port,
86+
displayHost,
87+
publicPath: ctx.base
88+
}])
8989

9090
config = config.toConfig()
9191
const userConfig = ctx.siteConfig.configureWebpack

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const { PLUGIN_OPTION_MAP } = require('./constants')
1010
const {
1111
moduleResolver: { getPluginResolver },
1212
datatypes: { assertTypes, isPlainObject },
13-
env: { isDebug },
13+
env: { debug },
1414
logger, chalk
1515
} = require('@vuepress/shared-utils')
1616

@@ -98,8 +98,7 @@ module.exports = class PluginAPI {
9898
this._pluginQueue.push(plugin)
9999

100100
if (plugin.plugins) {
101-
logger.debug(`Start to use plugins defined at ${chalk.gray(plugin.name)}`)
102-
logger.debug(JSON.stringify(plugin.plugins, null, 2))
101+
logger.debug(`Plugins defined at ${chalk.gray(plugin.name)}`, plugin.plugins)
103102
this.useByPluginsConfig(plugin.plugins)
104103
}
105104

@@ -211,7 +210,7 @@ module.exports = class PluginAPI {
211210
alias
212211
}) {
213212
const isInternalPlugin = pluginName.startsWith('@vuepress/internal-')
214-
if (isDebug && !isInternalPlugin) {
213+
if (!isInternalPlugin || debug) {
215214
logger.tip(
216215
shortcut
217216
? `Apply plugin ${chalk.magenta(shortcut)} ${chalk.gray(`(i.e. "${pluginName}")`)} ...`

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ function createTemp (tempPath) {
346346
fs.emptyDirSync(tempPath)
347347
}
348348

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

352352
async function writeTemp (file, content) {

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Module dependencies.
55
*/
66

7-
const { fs, path, logger } = require('@vuepress/shared-utils')
7+
const { fs, path, logger, env } = require('@vuepress/shared-utils')
88

99
/**
1010
* Expose createBaseConfig method.
@@ -21,7 +21,6 @@ module.exports = function createBaseConfig ({
2121
cacheDirectory,
2222
cacheIdentifier,
2323
cliOptions: {
24-
debug,
2524
cache
2625
},
2726
pluginAPI
@@ -36,20 +35,19 @@ module.exports = function createBaseConfig ({
3635
const config = new Config()
3736

3837
config
39-
.mode(isProd && !debug ? 'production' : 'development')
38+
.mode(isProd && !env.isDebug ? 'production' : 'development')
4039
.output
4140
.path(outDir)
4241
.filename(isProd ? 'assets/js/[name].[chunkhash:8].js' : 'assets/js/[name].js')
4342
.publicPath(isProd ? publicPath : '/')
4443

45-
if (debug) {
44+
if (env.isDebug) {
4645
config.devtool('source-map')
4746
} else if (!isProd) {
4847
config.devtool('cheap-module-eval-source-map')
4948
}
5049

5150
const modulePaths = getModulePaths()
52-
logger.debug('modulePaths = ' + JSON.stringify(modulePaths, null, 2))
5351

5452
config.resolve
5553
.set('symlinks', true)

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
*/
66

77
module.exports = function createClientConfig (ctx) {
8-
const { path } = require('@vuepress/shared-utils')
9-
const WebpackBar = require('webpackbar')
8+
const { path, env } = require('@vuepress/shared-utils')
109
const createBaseConfig = require('./createBaseConfig')
1110

1211
const config = createBaseConfig(ctx)
@@ -57,7 +56,8 @@ module.exports = function createClientConfig (ctx) {
5756
}])
5857
}
5958

60-
if (!ctx.cliOptions.debug) {
59+
if (!env.isDebug) {
60+
const WebpackBar = require('webpackbar')
6161
config
6262
.plugin('bar')
6363
.use(WebpackBar, [{

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
module.exports = function createServerConfig (ctx) {
88
const fs = require('fs')
9-
const { path } = require('@vuepress/shared-utils')
10-
const WebpackBar = require('webpackbar')
9+
const { path, env } = require('@vuepress/shared-utils')
1110
const createBaseConfig = require('./createBaseConfig')
1211
const VueSSRServerPlugin = require('vue-server-renderer/server-plugin')
1312
const CopyPlugin = require('copy-webpack-plugin')
@@ -46,7 +45,8 @@ module.exports = function createServerConfig (ctx) {
4645
]])
4746
}
4847

49-
if (!ctx.cliOptions.debug) {
48+
if (!env.isDebug) {
49+
const WebpackBar = require('webpackbar')
5050
config
5151
.plugin('bar')
5252
.use(WebpackBar, [{

packages/@vuepress/shared-utils/lib/env.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
const isDebug = process.argv.indexOf('--debug') !== -1
2-
const isProduction = () => process.env.NODE_ENV === 'production'
3-
const isTest = () => process.env.NODE_ENV === 'test'
1+
class ENV {
2+
constructor () {
3+
this.isDebug = false
4+
this.isTest = false
5+
this.isProduction = false
6+
}
7+
8+
setOptions (options) {
9+
Object.assign(this, options)
10+
}
11+
}
12+
13+
module.exports = new ENV()
414

5-
exports.isDebug = isDebug
6-
exports.isTest = isTest
7-
exports.isProduction = isProduction

packages/@vuepress/shared-utils/lib/logger.js

+12
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,32 @@ class Logger {
4848

4949
// level: 3
5050
success (...args) {
51+
if (this.options.logLevel < 3) {
52+
return
53+
}
5154
this.status('green', 'success', ...args)
5255
}
5356

5457
// level: 3
5558
tip (...args) {
59+
if (this.options.logLevel < 3) {
60+
return
61+
}
5662
this.status('blue', 'tip', ...args)
5763
}
5864

5965
// level: 3
6066
info (...args) {
67+
if (this.options.logLevel < 3) {
68+
return
69+
}
6170
this.status('cyan', 'info', ...args)
6271
}
6372

6473
wait (...args) {
74+
if (this.options.logLevel < 3) {
75+
return
76+
}
6577
this.status('cyan', 'wait', ...args)
6678
}
6779

packages/@vuepress/shared-utils/lib/module.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ exports.resolveModule = function (request, context) {
4242
// TODO
4343
// Temporary workaround for jest cannot resolve module path from '__mocks__'
4444
// when using 'require.resolve'.
45-
if (isTest() && request !== '@vuepress/theme-default') {
45+
if (isTest && request !== '@vuepress/theme-default') {
4646
resolvedPath = path.resolve(__dirname, '../../../../__mocks__', request)
4747
if (!fs.existsSync(`${resolvedPath}.js`) && !fs.existsSync(`${resolvedPath}/index.js`)) {
4848
throw new Error(`Cannot find module '${request}'`)

0 commit comments

Comments
 (0)