Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function add (pluginName, options = {}, context = process.cwd()) {
// special internal "plugins"
// 内部 plugin
if (/^(@vue\/)?router$/.test(pluginName)) { // 匹配 @vue/router,router。 ? 表示匹配前面的子表达式零次或一次
return addRouter(context)
}
if (/^(@vue\/)?vuex$/.test(pluginName)) { // 匹配 @vue/vuex,vuex
return addVuex(context)
}
// 解析插件名称
// full id, scoped short, or default short
// @bar/foo => @bar/vue-cli-plugin-foo
// @vue/foo => @vue/cli-plugin-foo
// foo => vue-cli-plugin-foo
const packageName = resolvePluginId(pluginName)
log()
log(`📦 Installing ${chalk.cyan(packageName)}...`)
log()
const packageManager = loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : 'npm')
await installPackage(context, packageManager, options.registry, packageName)
log(`${chalk.green('✔')} Successfully installed plugin: ${chalk.cyan(packageName)}`)
log()
const generatorPath = resolveModule(`${packageName}/generator`, context)
if (generatorPath) {
invoke(pluginName, options, context)
} else {
log(`Plugin ${packageName} does not have a generator to invoke`)
return
}
// for `vue add` command in 3.x projects
const servicePkg = loadModule('@vue/cli-service/package.json', context)
if (servicePkg && semver.satisfies(servicePkg.version, '3.x')) {
// special internal "plugins"
if (/^(@vue\/)?router$/.test(pluginName)) {
return addRouter(context)
}
if (/^(@vue\/)?vuex$/.test(pluginName)) {
return addVuex(context)
}
}
const packageName = resolvePluginId(pluginName)
log()
log(`📦 Installing ${chalk.cyan(packageName)}...`)
log()
const pm = new PackageManager({ context })
const cliVersion = require('../package.json').version
if (isOfficialPlugin(packageName) && semver.prerelease(cliVersion)) {
await pm.add(`${packageName}@^${cliVersion}`)
} else {
await pm.add(packageName)
}
log(`${chalk.green('✔')} Successfully installed plugin: ${chalk.cyan(packageName)}`)
log()
const findPlugin = deps => {
if (!deps) return
let name
// official
if (deps[(name = `@vue/cli-plugin-${pluginName}`)]) {
return name
}
// full id, scoped short, or default short
// @bar/foo => @bar/vue-cli-plugin-foo
// @vue/foo => @vue/cli-plugin-foo
// foo => vue-cli-plugin-foo
if (deps[(name = resolvePluginId(pluginName))]) {
return name
}
}
async upgrade (packageName) {
const realname = stripVersion(packageName)
if (
isTestOrDebug &&
(packageName === '@vue/cli-service' || isOfficialPlugin(resolvePluginId(realname)))
) {
// link packages in current repo for test
const src = path.resolve(__dirname, `../../../../${realname}`)
const dest = path.join(this.context, 'node_modules', realname)
await fs.remove(dest)
await fs.symlink(src, dest, 'dir')
return
}
await this.setBinaryMirrors()
const args = await this.addRegistryToArgs([
...PACKAGE_MANAGER_CONFIG[this.bin].add,
packageName
])
return executeCommand(this.bin, args, this.context)
}
const findPlugin = deps => {
if (!deps) return
let name
// official
if (deps[(name = `@vue/cli-plugin-${pluginName}`)]) {
return name
}
// full id, scoped short, or default short
if (deps[(name = resolvePluginId(pluginName))]) {
return name
}
}
? new Set(skipPlugins.split(',').map(id => resolvePluginId(id)))
: new Set()