Skip to content

Commit

Permalink
feat($cli): '--silent' option
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Nov 25, 2018
1 parent 9c61390 commit df99cb6
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 38 deletions.
49 changes: 42 additions & 7 deletions packages/@vuepress/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ exports.bootstrap = function ({
plugins,
theme
} = {}) {
const { path } = require('@vuepress/shared-utils')
const { path, logger, env } = require('@vuepress/shared-utils')
const { dev, build, eject } = require('@vuepress/core')

program
Expand All @@ -46,21 +46,56 @@ exports.bootstrap = function ({
.option('-c, --cache <cache>', 'set the directory of cache')
.option('--no-cache', 'clean the cache before build')
.option('--debug', 'start development server in debug mode')
.action((dir = '.', { host, port, debug, temp, cache }) => {
wrapCommand(dev)(path.resolve(dir), { host, port, debug, temp, cache, plugins, theme })
.option('--silent', 'start development server in silent mode')
.action((sourceDir = '.', {
host,
port,
debug,
temp,
cache,
silent
}) => {
logger.setOptions({ logLevel: silent ? 1 : debug ? 4 : 3 })
env.setOptions({ isDebug: debug, isTest: process.env.NODE_ENV === 'test' })

wrapCommand(dev)(path.resolve(sourceDir), {
host,
port,
temp,
cache,
plugins,
theme
})
})

program
.command('build [targetDir]')
.description('build dir as static site')
.option('-d, --dest <outDir>', 'specify build output dir (default: .vuepress/dist)')
.option('-d, --dest <dest>', 'specify build output dir (default: .vuepress/dist)')
.option('-t, --temp <temp>', 'set the directory of the temporary file')
.option('-c, --cache <cache>', 'set the directory of cache')
.option('--no-cache', 'clean the cache before build')
.option('--debug', 'build in development mode for debugging')
.action((dir = '.', { debug, dest, temp, cache }) => {
const outDir = dest ? path.resolve(dest) : null
wrapCommand(build)(path.resolve(dir), { debug, outDir, plugins, theme, temp, cache })
.option('--silent', 'build static site in silent mode')
.action((sourceDir, {
debug,
dest,
temp,
cache,
silent
}) => {
logger.setOptions({ logLevel: silent ? 1 : debug ? 4 : 3 })
env.setOptions({ isDebug: debug, isTest: process.env.NODE_ENV === 'test' })

wrapCommand(build)(path.resolve(sourceDir), {
debug,
dest,
plugins,
theme,
temp,
cache,
silent
})
})

program
Expand Down
4 changes: 2 additions & 2 deletions packages/@vuepress/core/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
const readline = require('readline')
const escape = require('escape-html')

const { chalk, fs, logger } = require('@vuepress/shared-utils')
const { chalk, fs, logger, env } = require('@vuepress/shared-utils')
const prepare = require('./prepare/index')
const createClientConfig = require('./webpack/createClientConfig')
const createServerConfig = require('./webpack/createServerConfig')
Expand Down Expand Up @@ -99,7 +99,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
reject(new Error(`Failed to compile with errors.`))
return
}
if (cliOptions.debug && stats.hasWarnings()) {
if (env.isDebug && stats.hasWarnings()) {
stats.toJson().warnings.forEach(warning => {
console.warn(warning)
})
Expand Down
12 changes: 6 additions & 6 deletions packages/@vuepress/core/lib/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ module.exports = async function dev (sourceDir, cliOptions = {}) {
const { host, displayHost } = await resolveHost(cliOptions.host || ctx.siteConfig.host)

config
.plugin('vuepress-log')
.use(DevLogPlugin, [{
port,
displayHost,
publicPath: ctx.base
}])
.plugin('vuepress-log')
.use(DevLogPlugin, [{
port,
displayHost,
publicPath: ctx.base
}])

config = config.toConfig()
const userConfig = ctx.siteConfig.configureWebpack
Expand Down
7 changes: 3 additions & 4 deletions packages/@vuepress/core/lib/plugin-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { PLUGIN_OPTION_MAP } = require('./constants')
const {
moduleResolver: { getPluginResolver },
datatypes: { assertTypes, isPlainObject },
env: { isDebug },
env: { debug },
logger, chalk
} = require('@vuepress/shared-utils')

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

if (plugin.plugins) {
logger.debug(`Start to use plugins defined at ${chalk.gray(plugin.name)}`)
logger.debug(JSON.stringify(plugin.plugins, null, 2))
logger.debug(`Plugins defined at ${chalk.gray(plugin.name)}`, plugin.plugins)
this.useByPluginsConfig(plugin.plugins)
}

Expand Down Expand Up @@ -211,7 +210,7 @@ module.exports = class PluginAPI {
alias
}) {
const isInternalPlugin = pluginName.startsWith('@vuepress/internal-')
if (isDebug && !isInternalPlugin) {
if (!isInternalPlugin || debug) {
logger.tip(
shortcut
? `Apply plugin ${chalk.magenta(shortcut)} ${chalk.gray(`(i.e. "${pluginName}")`)} ...`
Expand Down
2 changes: 1 addition & 1 deletion packages/@vuepress/core/lib/prepare/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ function createTemp (tempPath) {
fs.emptyDirSync(tempPath)
}

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

async function writeTemp (file, content) {
Expand Down
8 changes: 3 additions & 5 deletions packages/@vuepress/core/lib/webpack/createBaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

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

/**
* Expose createBaseConfig method.
Expand All @@ -21,7 +21,6 @@ module.exports = function createBaseConfig ({
cacheDirectory,
cacheIdentifier,
cliOptions: {
debug,
cache
},
pluginAPI
Expand All @@ -36,20 +35,19 @@ module.exports = function createBaseConfig ({
const config = new Config()

config
.mode(isProd && !debug ? 'production' : 'development')
.mode(isProd && !env.isDebug ? 'production' : 'development')
.output
.path(outDir)
.filename(isProd ? 'assets/js/[name].[chunkhash:8].js' : 'assets/js/[name].js')
.publicPath(isProd ? publicPath : '/')

if (debug) {
if (env.isDebug) {
config.devtool('source-map')
} else if (!isProd) {
config.devtool('cheap-module-eval-source-map')
}

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

config.resolve
.set('symlinks', true)
Expand Down
6 changes: 3 additions & 3 deletions packages/@vuepress/core/lib/webpack/createClientConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/

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

const config = createBaseConfig(ctx)
Expand Down Expand Up @@ -57,7 +56,8 @@ module.exports = function createClientConfig (ctx) {
}])
}

if (!ctx.cliOptions.debug) {
if (!env.isDebug) {
const WebpackBar = require('webpackbar')
config
.plugin('bar')
.use(WebpackBar, [{
Expand Down
6 changes: 3 additions & 3 deletions packages/@vuepress/core/lib/webpack/createServerConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

module.exports = function createServerConfig (ctx) {
const fs = require('fs')
const { path } = require('@vuepress/shared-utils')
const WebpackBar = require('webpackbar')
const { path, env } = require('@vuepress/shared-utils')
const createBaseConfig = require('./createBaseConfig')
const VueSSRServerPlugin = require('vue-server-renderer/server-plugin')
const CopyPlugin = require('copy-webpack-plugin')
Expand Down Expand Up @@ -46,7 +45,8 @@ module.exports = function createServerConfig (ctx) {
]])
}

if (!ctx.cliOptions.debug) {
if (!env.isDebug) {
const WebpackBar = require('webpackbar')
config
.plugin('bar')
.use(WebpackBar, [{
Expand Down
19 changes: 13 additions & 6 deletions packages/@vuepress/shared-utils/lib/env.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const isDebug = process.argv.indexOf('--debug') !== -1
const isProduction = () => process.env.NODE_ENV === 'production'
const isTest = () => process.env.NODE_ENV === 'test'
class ENV {
constructor () {
this.isDebug = false
this.isTest = false
this.isProduction = false
}

setOptions (options) {
Object.assign(this, options)
}
}

module.exports = new ENV()

exports.isDebug = isDebug
exports.isTest = isTest
exports.isProduction = isProduction
12 changes: 12 additions & 0 deletions packages/@vuepress/shared-utils/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,32 @@ class Logger {

// level: 3
success (...args) {
if (this.options.logLevel < 3) {
return
}
this.status('green', 'success', ...args)
}

// level: 3
tip (...args) {
if (this.options.logLevel < 3) {
return
}
this.status('blue', 'tip', ...args)
}

// level: 3
info (...args) {
if (this.options.logLevel < 3) {
return
}
this.status('cyan', 'info', ...args)
}

wait (...args) {
if (this.options.logLevel < 3) {
return
}
this.status('cyan', 'wait', ...args)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/@vuepress/shared-utils/lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ exports.resolveModule = function (request, context) {
// TODO
// Temporary workaround for jest cannot resolve module path from '__mocks__'
// when using 'require.resolve'.
if (isTest() && request !== '@vuepress/theme-default') {
if (isTest && request !== '@vuepress/theme-default') {
resolvedPath = path.resolve(__dirname, '../../../../__mocks__', request)
if (!fs.existsSync(`${resolvedPath}.js`) && !fs.existsSync(`${resolvedPath}/index.js`)) {
throw new Error(`Cannot find module '${request}'`)
Expand Down

0 comments on commit df99cb6

Please sign in to comment.