mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-05-23 22:18:30 -05:00
feat(plugin-api): expose inquirer to prompts.js, allowing custom prompt types (#5498)
This commit is contained in:
@@ -7,7 +7,7 @@ exports.prompt = prompts => {
|
||||
|
||||
const answers = {}
|
||||
let skipped = 0
|
||||
prompts.forEach((prompt, i) => {
|
||||
prompts.forEach((prompt, index) => {
|
||||
if (prompt.when && !prompt.when(answers)) {
|
||||
skipped++
|
||||
return
|
||||
@@ -25,7 +25,7 @@ exports.prompt = prompts => {
|
||||
: val
|
||||
}
|
||||
|
||||
const a = pendingAssertions[i - skipped]
|
||||
const a = pendingAssertions[index - skipped]
|
||||
if (!a) {
|
||||
console.error(`no matching assertion for prompt:`, prompt)
|
||||
console.log(prompts)
|
||||
@@ -88,6 +88,10 @@ exports.prompt = prompts => {
|
||||
return Promise.resolve(answers)
|
||||
}
|
||||
|
||||
exports.createPromptModule = () => {
|
||||
return exports.prompt
|
||||
}
|
||||
|
||||
exports.expectPrompts = assertions => {
|
||||
pendingAssertions = assertions
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ module.exports = class Creator extends EventEmitter {
|
||||
// run generator
|
||||
log(`🚀 Invoking generators...`)
|
||||
this.emit('creation', { event: 'invoking-generators' })
|
||||
const plugins = await this.resolvePlugins(preset.plugins)
|
||||
const plugins = await this.resolvePlugins(preset.plugins, pkg)
|
||||
const generator = new Generator(context, {
|
||||
pkg,
|
||||
plugins,
|
||||
@@ -355,21 +355,33 @@ module.exports = class Creator extends EventEmitter {
|
||||
}
|
||||
|
||||
// { id: options } => [{ id, apply, options }]
|
||||
async resolvePlugins (rawPlugins) {
|
||||
async resolvePlugins (rawPlugins, pkg) {
|
||||
// ensure cli-service is invoked first
|
||||
rawPlugins = sortObject(rawPlugins, ['@vue/cli-service'], true)
|
||||
const plugins = []
|
||||
for (const id of Object.keys(rawPlugins)) {
|
||||
const apply = loadModule(`${id}/generator`, this.context) || (() => {})
|
||||
let options = rawPlugins[id] || {}
|
||||
|
||||
if (options.prompts) {
|
||||
const prompts = loadModule(`${id}/prompts`, this.context)
|
||||
if (prompts) {
|
||||
let pluginPrompts = loadModule(`${id}/prompts`, this.context)
|
||||
|
||||
if (pluginPrompts) {
|
||||
const prompt = inquirer.createPromptModule()
|
||||
|
||||
if (typeof pluginPrompts === 'function') {
|
||||
pluginPrompts = pluginPrompts(pkg, prompt)
|
||||
}
|
||||
if (typeof pluginPrompts.getPrompts === 'function') {
|
||||
pluginPrompts = pluginPrompts.getPrompts(pkg, prompt)
|
||||
}
|
||||
|
||||
log()
|
||||
log(`${chalk.cyan(options._isPreset ? `Preset options:` : id)}`)
|
||||
options = await inquirer.prompt(prompts)
|
||||
options = await prompt(pluginPrompts)
|
||||
}
|
||||
}
|
||||
|
||||
plugins.push({ id, apply, options })
|
||||
}
|
||||
return plugins
|
||||
|
||||
@@ -68,13 +68,15 @@ async function invoke (pluginName, options = {}, context = process.cwd()) {
|
||||
} else if (!Object.keys(pluginOptions).length) {
|
||||
let pluginPrompts = loadModule(`${id}/prompts`, context)
|
||||
if (pluginPrompts) {
|
||||
const prompt = inquirer.createPromptModule()
|
||||
|
||||
if (typeof pluginPrompts === 'function') {
|
||||
pluginPrompts = pluginPrompts(pkg)
|
||||
pluginPrompts = pluginPrompts(pkg, prompt)
|
||||
}
|
||||
if (typeof pluginPrompts.getPrompts === 'function') {
|
||||
pluginPrompts = pluginPrompts.getPrompts(pkg)
|
||||
pluginPrompts = pluginPrompts.getPrompts(pkg, prompt)
|
||||
}
|
||||
pluginOptions = await inquirer.prompt(pluginPrompts)
|
||||
pluginOptions = await prompt(pluginPrompts)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user