From 0875f69432586d3b712de44844f218099d9e6a0c Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Sat, 3 Mar 2018 14:00:02 +0100 Subject: [PATCH 1/3] feat(bin): new 'add' command --- packages/@vue/cli/bin/vue.js | 8 ++++ packages/@vue/cli/lib/Creator.js | 2 +- packages/@vue/cli/lib/add.js | 37 +++++++++++++++ packages/@vue/cli/lib/invoke.js | 2 +- packages/@vue/cli/lib/util/installDeps.js | 58 +++++++++++++++++------ 5 files changed, 91 insertions(+), 16 deletions(-) create mode 100644 packages/@vue/cli/lib/add.js diff --git a/packages/@vue/cli/bin/vue.js b/packages/@vue/cli/bin/vue.js index 83e0053193..20c5f95860 100755 --- a/packages/@vue/cli/bin/vue.js +++ b/packages/@vue/cli/bin/vue.js @@ -54,6 +54,14 @@ program require('../lib/invoke')(plugin, minimist(process.argv.slice(3))) }) +program + .command('add [pluginOptions]') + .allowUnknownOption() + .description('install a plugin and invoke its generator in an already created project') + .action((plugin) => { + require('../lib/add')(plugin, minimist(process.argv.slice(3))) + }) + program .command('inspect [paths...]') .option('--mode ') diff --git a/packages/@vue/cli/lib/Creator.js b/packages/@vue/cli/lib/Creator.js index 9b636fd556..7e9027f7a6 100644 --- a/packages/@vue/cli/lib/Creator.js +++ b/packages/@vue/cli/lib/Creator.js @@ -7,7 +7,7 @@ const Generator = require('./Generator') const cloneDeep = require('lodash.clonedeep') const sortObject = require('./util/sortObject') const getVersions = require('./util/getVersions') -const installDeps = require('./util/installDeps') +const { installDeps } = require('./util/installDeps') const clearConsole = require('./util/clearConsole') const PromptModuleAPI = require('./PromptModuleAPI') const writeFileTree = require('./util/writeFileTree') diff --git a/packages/@vue/cli/lib/add.js b/packages/@vue/cli/lib/add.js new file mode 100644 index 0000000000..b57263640f --- /dev/null +++ b/packages/@vue/cli/lib/add.js @@ -0,0 +1,37 @@ +const chalk = require('chalk') +const { loadOptions } = require('./options') +const { installPackage } = require('./util/installDeps') +const { + log, + error, + hasYarn, + stopSpinner +} = require('@vue/cli-shared-utils') +const invoke = require('./invoke') + +async function add (pluginName, options = {}, context = process.cwd()) { + const packageName = pluginName.includes('vue-cli-plugin-') ? pluginName : `vue-cli-plugin-${pluginName}` + + log() + log(`📦 Installing ${chalk.cyan(packageName)}...`) + + const packageManager = loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm') + await installPackage(context, packageManager, null, packageName) + + stopSpinner() + + log() + log(`${chalk.green('✔')} Successfully installed plugin: ${chalk.cyan(packageName)}`) + log() + + invoke(pluginName, options, context) +} + +module.exports = (...args) => { + return add(...args).catch(err => { + error(err) + if (!process.env.VUE_CLI_TEST) { + process.exit(1) + } + }) +} diff --git a/packages/@vue/cli/lib/invoke.js b/packages/@vue/cli/lib/invoke.js index 5a8f8a922f..4e03c062da 100644 --- a/packages/@vue/cli/lib/invoke.js +++ b/packages/@vue/cli/lib/invoke.js @@ -6,7 +6,7 @@ const resolve = require('resolve') const inquirer = require('inquirer') const Generator = require('./Generator') const { loadOptions } = require('./options') -const installDeps = require('./util/installDeps') +const { installDeps } = require('./util/installDeps') const { log, error, diff --git a/packages/@vue/cli/lib/util/installDeps.js b/packages/@vue/cli/lib/util/installDeps.js index 3f454f0376..f8239ea7a7 100644 --- a/packages/@vue/cli/lib/util/installDeps.js +++ b/packages/@vue/cli/lib/util/installDeps.js @@ -93,16 +93,7 @@ function renderProgressBar (curr, total) { process.stderr.write(`[${complete}${incomplete}]${bar}`) } -module.exports = async function installDeps (targetDir, command, cliRegistry) { - const args = [] - if (command === 'npm') { - args.push('install', '--loglevel', 'error') - } else if (command === 'yarn') { - // do nothing - } else { - throw new Error(`Unknown package manager: ${command}`) - } - +async function addRegistryToArgs (command, args, cliRegistry) { if (command === 'yarn' && cliRegistry) { throw new Error( `Inline registry is not supported when using yarn. ` + @@ -124,11 +115,10 @@ module.exports = async function installDeps (targetDir, command, cliRegistry) { args.push(`--disturl=${taobaoDistURL}`) } } +} - debug(`command: `, command) - debug(`args: `, args) - - await new Promise((resolve, reject) => { +function executeCommand (command, args, targetDir) { + return new Promise((resolve, reject) => { const child = execa(command, args, { cwd: targetDir, stdio: ['inherit', 'inherit', command === 'yarn' ? 'pipe' : 'inherit'] @@ -162,3 +152,43 @@ module.exports = async function installDeps (targetDir, command, cliRegistry) { }) }) } + +module.exports.installDeps = async function installDeps (targetDir, command, cliRegistry) { + const args = [] + if (command === 'npm') { + args.push('install', '--loglevel', 'error') + } else if (command === 'yarn') { + // do nothing + } else { + throw new Error(`Unknown package manager: ${command}`) + } + + await addRegistryToArgs(command, args, cliRegistry) + + debug(`command: `, command) + debug(`args: `, args) + + await executeCommand(command, args, targetDir) +} + +module.exports.installPackage = async function (targetDir, command, cliRegistry, packageName, dev = true) { + const args = [] + if (command === 'npm') { + args.push('install', '--loglevel', 'error') + } else if (command === 'yarn') { + args.push('add') + } else { + throw new Error(`Unknown package manager: ${command}`) + } + + if (dev) args.push('-D') + + await addRegistryToArgs(command, args, cliRegistry) + + args.push(packageName) + + debug(`command: `, command) + debug(`args: `, args) + + await executeCommand(command, args, targetDir) +} From 2804adc1202f5b5264fad146f9917867adfb9900 Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Sat, 3 Mar 2018 14:06:55 +0100 Subject: [PATCH 2/3] fix(add): Add a blank line --- packages/@vue/cli/lib/add.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/@vue/cli/lib/add.js b/packages/@vue/cli/lib/add.js index b57263640f..6c159e981b 100644 --- a/packages/@vue/cli/lib/add.js +++ b/packages/@vue/cli/lib/add.js @@ -14,6 +14,7 @@ async function add (pluginName, options = {}, context = process.cwd()) { log() log(`📦 Installing ${chalk.cyan(packageName)}...`) + log() const packageManager = loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm') await installPackage(context, packageManager, null, packageName) From eb3c04795cb8df232ce1459bf7d714edba8b0cd9 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sun, 4 Mar 2018 13:23:33 -0500 Subject: [PATCH 3/3] Update installDeps.js --- packages/@vue/cli/lib/util/installDeps.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@vue/cli/lib/util/installDeps.js b/packages/@vue/cli/lib/util/installDeps.js index f8239ea7a7..8c6b88a896 100644 --- a/packages/@vue/cli/lib/util/installDeps.js +++ b/packages/@vue/cli/lib/util/installDeps.js @@ -153,7 +153,7 @@ function executeCommand (command, args, targetDir) { }) } -module.exports.installDeps = async function installDeps (targetDir, command, cliRegistry) { +exports.installDeps = async function installDeps (targetDir, command, cliRegistry) { const args = [] if (command === 'npm') { args.push('install', '--loglevel', 'error') @@ -171,7 +171,7 @@ module.exports.installDeps = async function installDeps (targetDir, command, cli await executeCommand(command, args, targetDir) } -module.exports.installPackage = async function (targetDir, command, cliRegistry, packageName, dev = true) { +exports.installPackage = async function (targetDir, command, cliRegistry, packageName, dev = true) { const args = [] if (command === 'npm') { args.push('install', '--loglevel', 'error')