Skip to content

Commit 818e5dd

Browse files
committed
feat: implement version-related APIs for GeneratorAPI (#4000)
closes #2336 (cherry picked from commit 77092b2)
1 parent de2f68d commit 818e5dd

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

docs/dev-guide/generator-api.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
# Generator API
22

3+
## cliVersion
4+
5+
Type: `string`
6+
7+
The version string for the **global** `@vue/cli` version that is invoking the plugin.
8+
9+
10+
## assertCliVersion
11+
12+
- **Arguments**
13+
- `{integer | string} range` - a semver range that `@vue/cli` needs to satisfy
14+
15+
- **Usage**
16+
17+
While api.version can be useful in general, it's sometimes nice to just declare your version.
18+
This API exposes a simple way to do that.
19+
20+
Nothing happens if the provided version is satified. Otherwise, an error will be thrown.
21+
22+
23+
## cliServiceVersion
24+
25+
Type: `string`
26+
27+
The version string for the **project local** `@vue/cli-service` version that is invoking the plugin.
28+
29+
30+
## assertCliServiceVersion
31+
32+
- **Arguments**
33+
- `{integer | string} range` - a semver range that `@vue/cli-service` needs to satisfy
34+
35+
- **Usage**
36+
37+
This API exposes a simple way to declare the required project local `@vue/cli-service` version.
38+
39+
Nothing happens if the provided version is satified. Otherwise, an error will be thrown.
40+
41+
342
## resolve
443

544
- **Arguments**

docs/dev-guide/plugin-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Type: `string`
77
The version string for the `@vue/cli-service` version that is loading the plugin.
88

99

10-
## assertVersion(range)
10+
## assertVersion
1111

1212
- **Arguments**
1313
- `{integer | string} range` - a semver range that `@vue/cli-service` needs to satisfy

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ const path = require('path')
44
const merge = require('deepmerge')
55
const resolve = require('resolve')
66
const { isBinaryFileSync } = require('isbinaryfile')
7+
const semver = require('semver')
78
const mergeDeps = require('./util/mergeDeps')
89
const stringifyJS = require('./util/stringifyJS')
910
const ConfigTransform = require('./ConfigTransform')
10-
const { getPluginLink, toShortPluginId } = require('@vue/cli-shared-utils')
11+
const { getPluginLink, toShortPluginId, loadModule } = require('@vue/cli-shared-utils')
1112

1213
const isString = val => typeof val === 'string'
1314
const isFunction = val => typeof val === 'function'
@@ -71,6 +72,55 @@ class GeneratorAPI {
7172
return path.resolve(this.generator.context, _path)
7273
}
7374

75+
get cliVersion () {
76+
return require('../package.json').version
77+
}
78+
79+
assertCliVersion (range) {
80+
if (typeof range === 'number') {
81+
if (!Number.isInteger(range)) {
82+
throw new Error('Expected string or integer value.')
83+
}
84+
range = `^${range}.0.0-0`
85+
}
86+
if (typeof range !== 'string') {
87+
throw new Error('Expected string or integer value.')
88+
}
89+
90+
if (semver.satisfies(this.cliVersion, range)) return
91+
92+
throw new Error(
93+
`Require global @vue/cli "${range}", but was invoked by "${this.cliVersion}".`
94+
)
95+
}
96+
97+
get cliServiceVersion () {
98+
const servicePkg = loadModule(
99+
'@vue/cli-service/package.json',
100+
this.generator.context
101+
)
102+
103+
return servicePkg.version
104+
}
105+
106+
assertCliServiceVersion (range) {
107+
if (typeof range === 'number') {
108+
if (!Number.isInteger(range)) {
109+
throw new Error('Expected string or integer value.')
110+
}
111+
range = `^${range}.0.0-0`
112+
}
113+
if (typeof range !== 'string') {
114+
throw new Error('Expected string or integer value.')
115+
}
116+
117+
if (semver.satisfies(this.cliServiceVersion, range)) return
118+
119+
throw new Error(
120+
`Require @vue/cli-service "${range}", but was loaded with "${this.cliServiceVersion}".`
121+
)
122+
}
123+
74124
/**
75125
* Check if the project has a given plugin.
76126
*

0 commit comments

Comments
 (0)