1
1
const path = require ( 'path' )
2
2
const fs = require ( 'fs' )
3
3
const LRU = require ( 'lru-cache' )
4
- const { isPlugin, isOfficialPlugin, getPluginLink } = require ( '@vue/cli-shared-utils' )
4
+ const {
5
+ isPlugin,
6
+ isOfficialPlugin,
7
+ getPluginLink,
8
+ hasYarn
9
+ } = require ( '@vue/cli-shared-utils' )
5
10
const getPackageVersion = require ( '@vue/cli/lib/util/getPackageVersion' )
11
+ const {
12
+ progress : installProgress ,
13
+ installPackage,
14
+ uninstallPackage
15
+ } = require ( '@vue/cli/lib/util/installDeps' )
16
+ const { loadOptions } = require ( '@vue/cli/lib/options' )
6
17
7
18
const cwd = require ( './cwd' )
8
19
const folders = require ( './folders' )
20
+ const prompts = require ( './prompts' )
21
+ const progress = require ( './progress' )
9
22
10
23
const metadataCache = new LRU ( {
11
24
max : 200 ,
@@ -16,6 +29,11 @@ const logoCache = new LRU({
16
29
max : 50
17
30
} )
18
31
32
+ const PROGRESS_ID = 'plugin-installation'
33
+
34
+ let currentPluginId
35
+ let eventsInstalled = false
36
+
19
37
function getPath ( id ) {
20
38
return path . join ( cwd . get ( ) , 'node_modules' , id )
21
39
}
@@ -108,9 +126,82 @@ async function getLogo ({ id }, context) {
108
126
return null
109
127
}
110
128
129
+ function getInstallation ( context ) {
130
+ if ( ! eventsInstalled ) {
131
+ eventsInstalled = true
132
+
133
+ // Package installation progress events
134
+ installProgress . on ( 'progress' , value => {
135
+ if ( progress . get ( PROGRESS_ID ) ) {
136
+ progress . set ( { id : PROGRESS_ID , progress : value } , context )
137
+ }
138
+ } )
139
+ installProgress . on ( 'log' , message => {
140
+ if ( progress . get ( PROGRESS_ID ) ) {
141
+ progress . set ( { id : PROGRESS_ID , info : message } , context )
142
+ }
143
+ } )
144
+ }
145
+
146
+ return {
147
+ id : 'plugin-install' ,
148
+ pluginId : currentPluginId ,
149
+ prompts : prompts . list ( )
150
+ }
151
+ }
152
+
153
+ function install ( id , context ) {
154
+ return progress . wrap ( PROGRESS_ID , context , async setProgress => {
155
+ setProgress ( {
156
+ status : 'plugin-install' ,
157
+ args : [ id ]
158
+ } )
159
+
160
+ currentPluginId = id
161
+
162
+ const packageManager = loadOptions ( ) . packageManager || ( hasYarn ( ) ? 'yarn' : 'npm' )
163
+ await installPackage ( cwd . get ( ) , packageManager , null , id )
164
+
165
+ return getInstallation ( context )
166
+ } )
167
+ }
168
+
169
+ function uninstall ( id , context ) {
170
+ return progress . wrap ( PROGRESS_ID , context , async setProgress => {
171
+ setProgress ( {
172
+ status : 'plugin-uninstall' ,
173
+ args : [ id ]
174
+ } )
175
+
176
+ currentPluginId = id
177
+
178
+ const packageManager = loadOptions ( ) . packageManager || ( hasYarn ( ) ? 'yarn' : 'npm' )
179
+ await uninstallPackage ( cwd . get ( ) , packageManager , null , id )
180
+
181
+ return getInstallation ( context )
182
+ } )
183
+ }
184
+
185
+ function invoke ( id , context ) {
186
+ return progress . wrap ( PROGRESS_ID , context , async setProgress => {
187
+ setProgress ( {
188
+ status : 'plugin-invoke' ,
189
+ args : [ id ]
190
+ } )
191
+
192
+ currentPluginId = id
193
+ // TODO
194
+ return getInstallation ( context )
195
+ } )
196
+ }
197
+
111
198
module . exports = {
112
199
list,
113
200
getVersion,
114
201
getDescription,
115
- getLogo
202
+ getLogo,
203
+ getInstallation,
204
+ install,
205
+ uninstall,
206
+ invoke
116
207
}
0 commit comments