Skip to content

Commit

Permalink
feat: allow vue add to work with plugins without a generator (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
javoski authored and yyx990803 committed Mar 24, 2018
1 parent 2106f47 commit 11956ac
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
8 changes: 7 additions & 1 deletion packages/@vue/cli/lib/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const chalk = require('chalk')
const invoke = require('./invoke')
const { loadOptions } = require('./options')
const { installPackage } = require('./util/installDeps')
const { resolveModule } = require('./util/module')
const {
log,
error,
Expand All @@ -26,7 +27,12 @@ async function add (pluginName, options = {}, context = process.cwd()) {
log(`${chalk.green('✔')} Successfully installed plugin: ${chalk.cyan(packageName)}`)
log()

invoke(pluginName, options, context)
const generatorPath = resolveModule(`${packageName}/generator`, context)
if (generatorPath) {
invoke(pluginName, options, context)
} else {
log(`Plugin ${packageName} does not have a generator to invoke`)
}
}

module.exports = (...args) => {
Expand Down
16 changes: 3 additions & 13 deletions packages/@vue/cli/lib/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const path = require('path')
const execa = require('execa')
const chalk = require('chalk')
const globby = require('globby')
const resolve = require('resolve')
const inquirer = require('inquirer')
const Generator = require('./Generator')
const { loadOptions } = require('./options')
const { installDeps } = require('./util/installDeps')
const { loadModule } = require('./util/module')
const {
log,
error,
Expand All @@ -18,16 +18,6 @@ const {
resolvePluginId
} = require('@vue/cli-shared-utils')

function load (request, context) {
let resolvedPath
try {
resolvedPath = resolve.sync(request, { basedir: context })
} catch (e) {}
if (resolvedPath) {
return require(resolvedPath)
}
}

async function readFiles (context) {
const files = await globby(['**'], {
cwd: context,
Expand Down Expand Up @@ -75,15 +65,15 @@ async function invoke (pluginName, options = {}, context = process.cwd()) {
)
}

const pluginGenerator = load(`${id}/generator`, context)
const pluginGenerator = loadModule(`${id}/generator`, context)
if (!pluginGenerator) {
throw new Error(`Plugin ${id} does not have a generator.`)
}

// resolve options if no command line options are passed, and the plugin
// contains a prompt module.
if (!Object.keys(options).length) {
const pluginPrompts = load(`${id}/prompts`, context)
const pluginPrompts = loadModule(`${id}/prompts`, context)
if (pluginPrompts) {
options = await inquirer.prompt(pluginPrompts)
}
Expand Down
16 changes: 16 additions & 0 deletions packages/@vue/cli/lib/util/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const resolve = require('resolve')

exports.resolveModule = function resolveModule (request, context) {
let resolvedPath
try {
resolvedPath = resolve.sync(request, { basedir: context })
} catch (e) {}
return resolvedPath
}

exports.loadModule = function loadModule (request, context) {
const resolvedPath = exports.resolveModule(request, context)
if (resolvedPath) {
return require(resolvedPath)
}
}

0 comments on commit 11956ac

Please sign in to comment.