Skip to content

Commit 896aec5

Browse files
Akryumyyx990803
authored andcommitted
feat: add vue add command (#936)
* feat(bin): new 'add' command * fix(add): Add a blank line * Update installDeps.js
1 parent edff5b4 commit 896aec5

File tree

5 files changed

+92
-16
lines changed

5 files changed

+92
-16
lines changed

packages/@vue/cli/bin/vue.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ program
5454
require('../lib/invoke')(plugin, minimist(process.argv.slice(3)))
5555
})
5656

57+
program
58+
.command('add <plugin> [pluginOptions]')
59+
.allowUnknownOption()
60+
.description('install a plugin and invoke its generator in an already created project')
61+
.action((plugin) => {
62+
require('../lib/add')(plugin, minimist(process.argv.slice(3)))
63+
})
64+
5765
program
5866
.command('inspect [paths...]')
5967
.option('--mode <mode>')

packages/@vue/cli/lib/Creator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const Generator = require('./Generator')
77
const cloneDeep = require('lodash.clonedeep')
88
const sortObject = require('./util/sortObject')
99
const getVersions = require('./util/getVersions')
10-
const installDeps = require('./util/installDeps')
10+
const { installDeps } = require('./util/installDeps')
1111
const clearConsole = require('./util/clearConsole')
1212
const PromptModuleAPI = require('./PromptModuleAPI')
1313
const writeFileTree = require('./util/writeFileTree')

packages/@vue/cli/lib/add.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const chalk = require('chalk')
2+
const { loadOptions } = require('./options')
3+
const { installPackage } = require('./util/installDeps')
4+
const {
5+
log,
6+
error,
7+
hasYarn,
8+
stopSpinner
9+
} = require('@vue/cli-shared-utils')
10+
const invoke = require('./invoke')
11+
12+
async function add (pluginName, options = {}, context = process.cwd()) {
13+
const packageName = pluginName.includes('vue-cli-plugin-') ? pluginName : `vue-cli-plugin-${pluginName}`
14+
15+
log()
16+
log(`📦 Installing ${chalk.cyan(packageName)}...`)
17+
log()
18+
19+
const packageManager = loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm')
20+
await installPackage(context, packageManager, null, packageName)
21+
22+
stopSpinner()
23+
24+
log()
25+
log(`${chalk.green('✔')} Successfully installed plugin: ${chalk.cyan(packageName)}`)
26+
log()
27+
28+
invoke(pluginName, options, context)
29+
}
30+
31+
module.exports = (...args) => {
32+
return add(...args).catch(err => {
33+
error(err)
34+
if (!process.env.VUE_CLI_TEST) {
35+
process.exit(1)
36+
}
37+
})
38+
}

packages/@vue/cli/lib/invoke.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const resolve = require('resolve')
66
const inquirer = require('inquirer')
77
const Generator = require('./Generator')
88
const { loadOptions } = require('./options')
9-
const installDeps = require('./util/installDeps')
9+
const { installDeps } = require('./util/installDeps')
1010
const {
1111
log,
1212
error,

packages/@vue/cli/lib/util/installDeps.js

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,7 @@ function renderProgressBar (curr, total) {
9393
process.stderr.write(`[${complete}${incomplete}]${bar}`)
9494
}
9595

96-
module.exports = async function installDeps (targetDir, command, cliRegistry) {
97-
const args = []
98-
if (command === 'npm') {
99-
args.push('install', '--loglevel', 'error')
100-
} else if (command === 'yarn') {
101-
// do nothing
102-
} else {
103-
throw new Error(`Unknown package manager: ${command}`)
104-
}
105-
96+
async function addRegistryToArgs (command, args, cliRegistry) {
10697
if (command === 'yarn' && cliRegistry) {
10798
throw new Error(
10899
`Inline registry is not supported when using yarn. ` +
@@ -124,11 +115,10 @@ module.exports = async function installDeps (targetDir, command, cliRegistry) {
124115
args.push(`--disturl=${taobaoDistURL}`)
125116
}
126117
}
118+
}
127119

128-
debug(`command: `, command)
129-
debug(`args: `, args)
130-
131-
await new Promise((resolve, reject) => {
120+
function executeCommand (command, args, targetDir) {
121+
return new Promise((resolve, reject) => {
132122
const child = execa(command, args, {
133123
cwd: targetDir,
134124
stdio: ['inherit', 'inherit', command === 'yarn' ? 'pipe' : 'inherit']
@@ -162,3 +152,43 @@ module.exports = async function installDeps (targetDir, command, cliRegistry) {
162152
})
163153
})
164154
}
155+
156+
exports.installDeps = async function installDeps (targetDir, command, cliRegistry) {
157+
const args = []
158+
if (command === 'npm') {
159+
args.push('install', '--loglevel', 'error')
160+
} else if (command === 'yarn') {
161+
// do nothing
162+
} else {
163+
throw new Error(`Unknown package manager: ${command}`)
164+
}
165+
166+
await addRegistryToArgs(command, args, cliRegistry)
167+
168+
debug(`command: `, command)
169+
debug(`args: `, args)
170+
171+
await executeCommand(command, args, targetDir)
172+
}
173+
174+
exports.installPackage = async function (targetDir, command, cliRegistry, packageName, dev = true) {
175+
const args = []
176+
if (command === 'npm') {
177+
args.push('install', '--loglevel', 'error')
178+
} else if (command === 'yarn') {
179+
args.push('add')
180+
} else {
181+
throw new Error(`Unknown package manager: ${command}`)
182+
}
183+
184+
if (dev) args.push('-D')
185+
186+
await addRegistryToArgs(command, args, cliRegistry)
187+
188+
args.push(packageName)
189+
190+
debug(`command: `, command)
191+
debug(`args: `, args)
192+
193+
await executeCommand(command, args, targetDir)
194+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy