feat(plugin-api): prompts.js can now export a function which receives package info

This commit is contained in:
Evan You
2018-07-18 16:51:13 -04:00
parent ba4d0f285b
commit e33b04c306
2 changed files with 16 additions and 2 deletions
@@ -3,7 +3,7 @@
const { chalk, hasGit } = require('@vue/cli-shared-utils')
module.exports = [
const prompts = module.exports = [
{
name: `classComponent`,
type: `confirm`,
@@ -38,3 +38,11 @@ module.exports = [
]
}
]
// in RC6+ the export can be function, but that would break invoke for RC5 and
// below, so this is a temporary compatibility hack until we release stable.
// TODO just export the function in 3.0.0
module.exports.getPrompts = pkg => {
prompts[2].when = () => !('@vue/cli-plugin-eslint' in (pkg.devDependencies || {}))
return prompts
}
+7 -1
View File
@@ -84,8 +84,14 @@ async function invoke (pluginName, options = {}, context = process.cwd()) {
// resolve options if no command line options are passed, and the plugin
// contains a prompt module.
if (!Object.keys(options).length) {
const pluginPrompts = loadModule(`${id}/prompts`, context)
let pluginPrompts = loadModule(`${id}/prompts`, context)
if (pluginPrompts) {
if (typeof pluginPrompts === 'function') {
pluginPrompts = pluginPrompts(pkg)
}
if (typeof pluginPrompts.getPrompts === 'function') {
pluginPrompts = pluginPrompts.getPrompts(pkg)
}
options = await inquirer.prompt(pluginPrompts)
}
}