Skip to content

Commit 78dcc7a

Browse files
committed
refactor: use consistent plugin name resolution between add and invoke
1 parent 896aec5 commit 78dcc7a

File tree

6 files changed

+40
-26
lines changed

6 files changed

+40
-26
lines changed

packages/@vue/cli-shared-utils/__tests__/pluginResolution.spec.js

Whitespace-only changes.
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
const pluginRE = /^(@vue\/|vue-|@[\w-]+\/vue-)cli-plugin-/
2+
const scopeRE = /^@[\w-]+\//
23

34
exports.isPlugin = id => pluginRE.test(id)
45

5-
exports.isOfficial = id => /^@vue\//.test(id)
6+
exports.isOfficialPlugin = id => /^@vue\//.test(id)
67

7-
exports.toShortId = id => id.replace(pluginRE, '')
8+
exports.toShortPluginId = id => id.replace(pluginRE, '')
9+
10+
exports.resolvePluginId = id => {
11+
// already full id
12+
// e.g. vue-cli-plugin-foo, @vue/cli-plugin-foo, @bar/vue-cli-plugin-foo
13+
if (pluginRE.test(id)) {
14+
return id
15+
}
16+
// scoped short
17+
// e.g. @vue/foo, @bar/foo
18+
if (id.charAt(0) === '@') {
19+
const scopeMatch = id.match(scopeRE)
20+
if (scopeMatch) {
21+
const shortId = id.replace(scopeRE, '')
22+
return `${scopeMatch[0]}vue-cli-plugin-${shortId}`
23+
}
24+
}
25+
// default short
26+
// e.g. foo
27+
return `vue-cli-plugin-${id}`
28+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const debug = require('debug')
44
const GeneratorAPI = require('./GeneratorAPI')
55
const sortObject = require('./util/sortObject')
66
const writeFileTree = require('./util/writeFileTree')
7-
const { toShortId } = require('@vue/cli-shared-utils')
87
const configTransforms = require('./util/configTransforms')
8+
const { toShortPluginId } = require('@vue/cli-shared-utils')
99

1010
const logger = require('@vue/cli-shared-utils/lib/logger')
1111
const logTypes = {
@@ -151,7 +151,7 @@ module.exports = class Generator {
151151
printExitLogs () {
152152
if (this.exitLogs.length) {
153153
this.exitLogs.forEach(({ id, msg, type }) => {
154-
const shortId = toShortId(id)
154+
const shortId = toShortPluginId(id)
155155
const logFn = logTypes[type]
156156
if (!logFn) {
157157
logger.error(`Invalid api.exitLog type '${type}'.`, shortId)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const globby = require('globby')
55
const isBinary = require('isbinaryfile')
66
const yaml = require('yaml-front-matter')
77
const mergeDeps = require('./util/mergeDeps')
8-
const { isOfficial, toShortId } = require('@vue/cli-shared-utils')
8+
const { isOfficialPlugin, toShortPluginId } = require('@vue/cli-shared-utils')
99

1010
const isString = val => typeof val === 'string'
1111
const isFunction = val => typeof val === 'function'
@@ -40,10 +40,10 @@ class GeneratorAPI {
4040
this.pluginsData = generator.plugins
4141
.filter(({ id }) => id !== `@vue/cli-service`)
4242
.map(({ id }) => {
43-
const name = toShortId(id)
43+
const name = toShortPluginId(id)
4444
return {
4545
name: name,
46-
link: isOfficial(id)
46+
link: isOfficialPlugin(id)
4747
? `https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-${name}`
4848
: getLink(id)
4949
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
const chalk = require('chalk')
2+
const invoke = require('./invoke')
23
const { loadOptions } = require('./options')
34
const { installPackage } = require('./util/installDeps')
45
const {
56
log,
67
error,
78
hasYarn,
8-
stopSpinner
9+
stopSpinner,
10+
resolvePluginId
911
} = require('@vue/cli-shared-utils')
10-
const invoke = require('./invoke')
1112

1213
async function add (pluginName, options = {}, context = process.cwd()) {
13-
const packageName = pluginName.includes('vue-cli-plugin-') ? pluginName : `vue-cli-plugin-${pluginName}`
14+
const packageName = resolvePluginId(pluginName)
1415

1516
log()
1617
log(`📦 Installing ${chalk.cyan(packageName)}...`)

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const {
1313
hasYarn,
1414
hasGit,
1515
logWithSpinner,
16-
stopSpinner
16+
stopSpinner,
17+
resolvePluginId
1718
} = require('@vue/cli-shared-utils')
1819

1920
function load (request, context) {
@@ -40,22 +41,13 @@ async function invoke (pluginName, options = {}, context = process.cwd()) {
4041
// attempt to locate the plugin in package.json
4142
const findPlugin = deps => {
4243
if (!deps) return
43-
44-
// custom scoped plugin
4544
let name
46-
if (pluginName.charAt(0) === '@') {
47-
const scopeRE = /^@[\w-]+\//
48-
const scopeMatch = pluginName.match(scopeRE)
49-
const shortId = pluginName.replace(scopeRE, '')
50-
if (scopeMatch && deps[name = `${scopeMatch[0]}vue-cli-plugin-${shortId}`]) {
51-
return name
52-
}
45+
// official
46+
if (deps[name = `@vue/cli-plugin-${pluginName}`]) {
47+
return name
5348
}
54-
55-
// official, non-scoped & full name
56-
if (deps[name = `@vue/cli-plugin-${pluginName}`] ||
57-
deps[name = `vue-cli-plugin-${pluginName}`] ||
58-
deps[name = pluginName]) {
49+
// full id, scoped short, or default short
50+
if (deps[name = resolvePluginId(pluginName)]) {
5951
return name
6052
}
6153
}
@@ -135,7 +127,7 @@ async function invoke (pluginName, options = {}, context = process.cwd()) {
135127
log()
136128
}
137129
}
138-
log(` You should review and commit the changes.`)
130+
log(` You should review these changes with ${chalk.cyan(`git diff`)} and commit them.`)
139131
log()
140132

141133
generator.printExitLogs()

0 commit comments

Comments
 (0)