File tree Expand file tree Collapse file tree 3 files changed +58
-0
lines changed
packages/@vue/cli-service Expand file tree Collapse file tree 3 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 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** :
Original file line number Diff line number Diff 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+
173188test ( 'api: registerCommand' , ( ) => {
174189 let args
175190 const service = createMockService ( [ {
Original file line number Diff line number Diff line change 11const path = require ( 'path' )
22const hash = require ( 'hash-sum' )
3+ const semver = require ( 'semver' )
34const { 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 */
You can’t perform that action at this time.
0 commit comments