File tree 3 files changed +58
-0
lines changed
packages/@vue/cli-service
3 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1
1
# Plugin API
2
2
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
+
3
22
## getCwd
4
23
5
24
- ** Usage** :
Original file line number Diff line number Diff line change @@ -170,6 +170,21 @@ test('load project options from vue.config.js', () => {
170
170
expect ( service . projectOptions . lintOnSave ) . toBe ( false )
171
171
} )
172
172
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
+
173
188
test ( 'api: registerCommand' , ( ) => {
174
189
let args
175
190
const service = createMockService ( [ {
Original file line number Diff line number Diff line change 1
1
const path = require ( 'path' )
2
2
const hash = require ( 'hash-sum' )
3
+ const semver = require ( 'semver' )
3
4
const { matchesPluginId } = require ( '@vue/cli-shared-utils' )
4
5
5
6
// Note: if a plugin-registered command needs to run in a specific default mode,
@@ -17,6 +18,29 @@ class PluginAPI {
17
18
this . service = service
18
19
}
19
20
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
+
20
44
/**
21
45
* Current working directory.
22
46
*/
You can’t perform that action at this time.
0 commit comments