Skip to content

Commit

Permalink
feat(plugin-api): expose inquirer to prompts.js, allowing custom pr…
Browse files Browse the repository at this point in the history
…ompt types (#5498)
  • Loading branch information
sodatea committed May 19, 2020
1 parent 0295ff6 commit 02a365d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
8 changes: 6 additions & 2 deletions __mocks__/inquirer.js
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
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
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

0 comments on commit 02a365d

Please sign in to comment.