Skip to content

Commit

Permalink
refactor: improve options validation
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 4, 2018
1 parent d3d040a commit 85aacc3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 30 deletions.
17 changes: 14 additions & 3 deletions packages/@vue/cli-service/lib/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ module.exports = class Service {

loadProjectOptions (inlineOptions) {
// vue.config.js
let fileConfig, pkgConfig, resolved
let fileConfig, pkgConfig, resolved, resovledFrom
const configPath = (
process.env.VUE_CLI_SERVICE_CONFIG_PATH ||
path.resolve(this.context, 'vue.config.js')
Expand Down Expand Up @@ -182,23 +182,34 @@ module.exports = class Service {
if (fileConfig) {
if (pkgConfig) {
warn(
`"vue" field in ${chalk.bold(`package.json`)} ignored ` +
`"vue" field in package.json ignored ` +
`due to presence of ${chalk.bold('vue.config.js')}.`
)
warn(
`You should migrate it into ${chalk.bold('vue.config.js')} ` +
`and remove it from package.json.`
)
}
resolved = fileConfig
resovledFrom = 'vue.config.js'
} else if (pkgConfig) {
resolved = pkgConfig
resovledFrom = '"vue" field in package.json'
} else {
resolved = inlineOptions || {}
resovledFrom = 'inline options'
}

// normlaize some options
ensureSlash(resolved, 'base')
removeSlash(resolved, 'outputDir')

// validate options
validate(resolved)
validate(resolved, msg => {
error(
`Invalid options in ${chalk.bold(resovledFrom)}: ${msg}`
)
})

return resolved
}
Expand Down
25 changes: 14 additions & 11 deletions packages/@vue/cli-service/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ const schema = createSchema(joi => joi.object({
productionSourceMap: joi.boolean(),
vueLoader: joi.object(),
parallel: joi.boolean(),
devServer: joi.object(),
dll: joi.alternatives().try(
joi.boolean(),
joi.array().items(joi.string())
),

// css
css: joi.object({
modules: joi.boolean(),
extract: joi.boolean(),
Expand All @@ -21,25 +24,25 @@ const schema = createSchema(joi => joi.object({
stylus: joi.object()
})
}),
devServer: joi.object(),

// known options from offical plugins
lintOnSave: joi.boolean(),

// webpack
chainWebpack: joi.func(),
configureWebpack: joi.alternatives().try(
joi.object(),
joi.func()
)
),

// known runtime options for built-in plugins
lintOnSave: joi.boolean(),
pwa: joi.object(),

// 3rd party plugin options
pluginOptions: joi.object()
}))

exports.validate = options => validate(
options,
schema,
// so that plugins can make use of custom options
{ allowUnknown: true }
)
exports.validate = (options, cb) => {
validate(options, schema, cb)
}

exports.defaults = () => ({
// project deployment base
Expand Down
13 changes: 6 additions & 7 deletions packages/@vue/cli-shared-utils/lib/validate.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const joi = require('joi')
const { error } = require('./logger')

// proxy to joi for option validation
exports.createSchema = fn => fn(joi)

exports.validate = (obj, schema, options = {}, noExit) => {
joi.validate(obj, schema, options, err => {
exports.validate = (obj, schema, cb) => {
joi.validate(obj, schema, {}, err => {
if (err) {
error(`vue-cli options validation failed:\n` + err.message)
if (!noExit) {
process.exit(1)
} else {
cb(err.message)
if (process.env.VUE_CLI_TEST) {
throw err
} else {
process.exit(1)
}
}
})
Expand Down
17 changes: 8 additions & 9 deletions packages/@vue/cli/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs')
const os = require('os')
const path = require('path')
const cloneDeep = require('lodash.clonedeep')
const { error, log } = require('@vue/cli-shared-utils/lib/logger')
const { error } = require('@vue/cli-shared-utils/lib/logger')
const { createSchema, validate } = require('@vue/cli-shared-utils/lib/validate')

const rcPath = exports.rcPath = (
Expand All @@ -24,7 +24,9 @@ const schema = createSchema(joi => joi.object().keys({
presets: joi.object().pattern(/^/, presetSchema)
}))

exports.validatePreset = preset => validate(preset, presetSchema)
exports.validatePreset = preset => validate(preset, presetSchema, msg => {
error(`invalid preset options: ${msg}`)
})

exports.defaultPreset = {
router: false,
Expand Down Expand Up @@ -64,17 +66,14 @@ exports.loadOptions = () => {
`Please fix/delete it and re-run vue-cli in manual mode.\n` +
`(${e.message})`,
)
// process.exit(1)
process.exit(1)
}
try {
validate(cachedOptions, schema, undefined, true /* noExit */)
} catch (e) {
log(
validate(cachedOptions, schema, () => {
error(
`~/.vuerc may be outdated. ` +
`Please delete it and re-run vue-cli in manual mode.`
)
// process.exit(1)
}
})
return cachedOptions
} else {
return {}
Expand Down

0 comments on commit 85aacc3

Please sign in to comment.