Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions __mocks__/inquirer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -88,6 +88,10 @@ exports.prompt = prompts => {
return Promise.resolve(answers)
}

exports.createPromptModule = () => {
return exports.prompt
}

exports.expectPrompts = assertions => {
pendingAssertions = assertions
}
22 changes: 17 additions & 5 deletions packages/@vue/cli/lib/Creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions packages/@vue/cli/lib/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down