Skip to content

Commit a3e0858

Browse files
authored
feat: add .version field and assertVersion helper to plugin api (#3861)
partially addresses #2336 (GeneratorAPI TBD)
1 parent a351cba commit a3e0858

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

docs/dev-guide/plugin-api.md

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
# Plugin API
22

3+
## version
4+
5+
Type: `string`
6+
7+
The version string for the `@vue/cli-service` version that is loading the plugin.
8+
9+
10+
## assertVersion(range)
11+
12+
- **Arguments**
13+
- `{integer | string} range` - a semver range that `@vue/cli-service` 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+
322
## getCwd
423

524
- **Usage**:

packages/@vue/cli-service/__tests__/Service.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ test('load project options from vue.config.js', () => {
170170
expect(service.projectOptions.lintOnSave).toBe(false)
171171
})
172172

173+
test('api: assertVersion', () => {
174+
const plugin = {
175+
id: 'test-assertVersion',
176+
apply: api => {
177+
expect(() => api.assertVersion(3)).not.toThrow()
178+
expect(() => api.assertVersion('3')).not.toThrow()
179+
expect(() => api.assertVersion('>= 3')).not.toThrow()
180+
181+
expect(() => api.assertVersion(3.1)).toThrow('Expected string or integer value')
182+
expect(() => api.assertVersion('^100')).toThrow('Require @vue/cli-service "^100"')
183+
}
184+
}
185+
createMockService([plugin], true /* init */)
186+
})
187+
173188
test('api: registerCommand', () => {
174189
let args
175190
const service = createMockService([{

packages/@vue/cli-service/lib/PluginAPI.js

+24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path')
22
const hash = require('hash-sum')
3+
const semver = require('semver')
34
const { matchesPluginId } = require('@vue/cli-shared-utils')
45

56
// Note: if a plugin-registered command needs to run in a specific default mode,
@@ -17,6 +18,29 @@ class PluginAPI {
1718
this.service = service
1819
}
1920

21+
get version () {
22+
return require('../package.json').version
23+
}
24+
25+
assertVersion (range) {
26+
if (typeof range === 'number') {
27+
console.log(range, Number.isInteger(range))
28+
if (!Number.isInteger(range)) {
29+
throw new Error('Expected string or integer value.')
30+
}
31+
range = `^${range}.0.0-0`
32+
}
33+
if (typeof range !== 'string') {
34+
throw new Error('Expected string or integer value.')
35+
}
36+
37+
if (semver.satisfies(this.version, range)) return
38+
39+
throw new Error(
40+
`Require @vue/cli-service "${range}", but was loaded with "${this.version}".`
41+
)
42+
}
43+
2044
/**
2145
* Current working directory.
2246
*/

0 commit comments

Comments
 (0)