Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow 'vue add' to work with plugins without a generator #1032

Merged
merged 1 commit into from
Mar 24, 2018
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: 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)
}
}