forked from vuejs/vue-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.js
59 lines (51 loc) · 1.7 KB
/
add.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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,
warn,
hasYarn,
stopSpinner,
resolvePluginId
} = require('@vue/cli-shared-utils')
// vuex and vue-router are internally added via cli-service so they need to be handled differently
const internalPluginRE = /^(vuex|router)$/
async function add (pluginName, options = {}, context = process.cwd()) {
let packageName
const internalPlugin = internalPluginRE.test(pluginName)
if (internalPlugin) {
packageName = pluginName.replace(/router/, 'vue-router')
} else {
packageName = resolvePluginId(pluginName)
}
log()
log(`📦 Installing ${chalk.cyan(packageName)}...`)
log()
const packageManager = loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm')
await installPackage(context, packageManager, null, packageName, !internalPlugin)
stopSpinner()
log()
log(`${chalk.green('✔')} Successfully installed plugin: ${chalk.cyan(packageName)}`)
log()
const generatorPath = resolveModule(`${packageName}/generator`, context)
if (generatorPath) {
invoke(pluginName, options, context)
} else if (internalPlugin) {
log()
warn(`Internal Plugin: ${packageName} cannot modify files post creation. See https://${pluginName}.vuejs.org for instructions to add ${packageName} to your code`)
log()
} else {
log(`Plugin ${packageName} does not have a generator to invoke`)
}
}
module.exports = (...args) => {
return add(...args).catch(err => {
error(err)
if (!process.env.VUE_CLI_TEST) {
process.exit(1)
}
})
}