1
1
const path = require ( 'path' )
2
2
const fs = require ( 'fs' )
3
3
const LRU = require ( 'lru-cache' )
4
+ const semver = require ( 'semver' )
4
5
const {
5
6
isPlugin,
6
7
isOfficialPlugin,
@@ -11,7 +12,8 @@ const getPackageVersion = require('@vue/cli/lib/util/getPackageVersion')
11
12
const {
12
13
progress : installProgress ,
13
14
installPackage,
14
- uninstallPackage
15
+ uninstallPackage,
16
+ updatePackage
15
17
} = require ( '@vue/cli/lib/util/installDeps' )
16
18
const { loadOptions } = require ( '@vue/cli/lib/options' )
17
19
const invoke = require ( '@vue/cli/lib/invoke' )
@@ -20,6 +22,7 @@ const cwd = require('./cwd')
20
22
const folders = require ( './folders' )
21
23
const prompts = require ( './prompts' )
22
24
const progress = require ( './progress' )
25
+ const logs = require ( './logs' )
23
26
24
27
const metadataCache = new LRU ( {
25
28
max : 200 ,
@@ -34,9 +37,12 @@ const PROGRESS_ID = 'plugin-installation'
34
37
35
38
let currentPluginId
36
39
let eventsInstalled = false
40
+ let plugins = [ ]
37
41
38
42
function getPath ( id ) {
39
- return path . join ( cwd . get ( ) , 'node_modules' , id )
43
+ return path . dirname ( require . resolve ( id , {
44
+ paths : [ cwd . get ( ) ]
45
+ } ) )
40
46
}
41
47
42
48
function findPlugins ( deps ) {
@@ -55,12 +61,18 @@ function findPlugins (deps) {
55
61
56
62
function list ( file , context ) {
57
63
const pkg = folders . readPackage ( file , context )
58
- let plugins = [ ]
64
+ plugins = [ ]
59
65
plugins = plugins . concat ( findPlugins ( pkg . dependencies || { } ) )
60
66
plugins = plugins . concat ( findPlugins ( pkg . devDependencies || { } ) )
61
67
return plugins
62
68
}
63
69
70
+ function findOne ( id , context ) {
71
+ return plugins . find (
72
+ p => p . id === id
73
+ )
74
+ }
75
+
64
76
function readPackage ( id , context ) {
65
77
return folders . readPackage ( getPath ( id ) , context )
66
78
}
@@ -70,18 +82,10 @@ async function getMetadata (id, context) {
70
82
if ( metadata ) {
71
83
return metadata
72
84
}
73
- if ( isOfficialPlugin ( id ) ) {
74
- const res = await getPackageVersion ( 'vue-cli-version-marker' , 'latest' )
75
- if ( res . statusCode === 200 ) {
76
- metadata = res . body
77
- }
78
- const pkg = folders . readPackage ( path . dirname ( require . resolve ( id ) ) , context )
79
- metadata . description = pkg . description
80
- } else {
81
- const res = await getPackageVersion ( id , id . indexOf ( '@' ) === - 1 ? 'latest' : '' )
82
- if ( res . statusCode === 200 ) {
83
- metadata = res . body
84
- }
85
+
86
+ const res = await getPackageVersion ( id )
87
+ if ( res . statusCode === 200 ) {
88
+ metadata = res . body
85
89
}
86
90
87
91
if ( metadata ) {
@@ -98,20 +102,22 @@ async function getVersion ({ id, installed, versionRange }, context) {
98
102
} else {
99
103
current = null
100
104
}
101
- let latest
105
+ let latest , wanted
102
106
const metadata = await getMetadata ( id , context )
103
107
if ( metadata ) {
104
- latest = ( metadata [ 'dist-tags' ] && metadata [ 'dist-tags' ] . latest ) || metadata . version
105
- }
108
+ latest = metadata [ 'dist-tags' ] . latest
106
109
107
- if ( ! latest ) {
108
- // fallback to local version
109
- latest = current
110
+ const versions = Object . keys ( metadata . versions )
111
+ wanted = semver . maxSatisfying ( versions , versionRange )
110
112
}
111
113
114
+ if ( ! latest ) latest = current
115
+ if ( ! wanted ) wanted = current
116
+
112
117
return {
113
118
current,
114
119
latest,
120
+ wanted,
115
121
range : versionRange
116
122
}
117
123
}
@@ -230,13 +236,41 @@ async function initPrompts (id, context) {
230
236
prompts . start ( )
231
237
}
232
238
239
+ function update ( id , context ) {
240
+ return progress . wrap ( 'plugin-update' , context , async setProgress => {
241
+ setProgress ( {
242
+ status : 'plugin-update' ,
243
+ args : [ id ]
244
+ } )
245
+
246
+ currentPluginId = id
247
+
248
+ const plugin = findOne ( id , context )
249
+ const { current, wanted } = await getVersion ( plugin , context )
250
+
251
+ const packageManager = loadOptions ( ) . packageManager || ( hasYarn ( ) ? 'yarn' : 'npm' )
252
+ await updatePackage ( cwd . get ( ) , packageManager , null , id )
253
+
254
+ logs . add ( {
255
+ message : `Plugin ${ id } updated from ${ current } to ${ wanted } ` ,
256
+ type : 'info'
257
+ } , context )
258
+
259
+ currentPluginId = null
260
+
261
+ return findOne ( id )
262
+ } )
263
+ }
264
+
233
265
module . exports = {
234
266
list,
267
+ findOne,
235
268
getVersion,
236
269
getDescription,
237
270
getLogo,
238
271
getInstallation,
239
272
install,
240
273
uninstall,
274
+ update,
241
275
runInvoke
242
276
}
0 commit comments