1
1
import PicGo from '../core/PicGo'
2
2
import spawn from 'cross-spawn'
3
- import { IResult , IProcessEnv , Undefinable , IPluginProcessResult } from '../types'
3
+ import {
4
+ IResult ,
5
+ IProcessEnv ,
6
+ IPluginProcessResult ,
7
+ IPluginHandler ,
8
+ IPluginHandlerOptions ,
9
+ Undefinable
10
+ } from '../types'
4
11
import { IBuildInEvent } from '../utils/enum'
5
12
import { getProcessPluginName , getNormalPluginName } from '../utils/common'
6
13
7
- class PluginHandler {
14
+ class PluginHandler implements IPluginHandler {
8
15
// Thanks to feflow -> https://github.com/feflow/feflow/blob/master/lib/internal/install/plugin.js
9
- ctx : PicGo
16
+ private readonly ctx : PicGo
10
17
constructor ( ctx : PicGo ) {
11
18
this . ctx = ctx
12
19
}
13
20
14
- async install ( plugins : string [ ] , proxy : string = '' , env ?: IProcessEnv ) : Promise < void > {
21
+ async install ( plugins : string [ ] , options : IPluginHandlerOptions = { } , env ?: IProcessEnv ) : Promise < void > {
15
22
const installedPlugins : string [ ] = [ ]
16
23
const processPlugins = plugins
17
24
. map ( ( item : string ) => handlePluginNameProcess ( this . ctx , item ) )
@@ -35,7 +42,7 @@ class PluginHandler {
35
42
// install plugins must use fullNameList:
36
43
// 1. install remote pacage
37
44
// 2. install local pacage
38
- const result = await this . execCommand ( 'install' , fullNameList , this . ctx . baseDir , proxy , env )
45
+ const result = await this . execCommand ( 'install' , fullNameList , this . ctx . baseDir , options , env )
39
46
if ( ! result . code ) {
40
47
pkgNameList . forEach ( ( pluginName : string ) => {
41
48
this . ctx . pluginLoader . registerPlugin ( pluginName )
@@ -103,13 +110,13 @@ class PluginHandler {
103
110
}
104
111
}
105
112
106
- async update ( plugins : string [ ] , proxy : string = '' , env ?: IProcessEnv ) : Promise < void > {
113
+ async update ( plugins : string [ ] , options : IPluginHandlerOptions = { } , env ?: IProcessEnv ) : Promise < void > {
107
114
const processPlugins = plugins . map ( ( item : string ) => handlePluginNameProcess ( this . ctx , item ) ) . filter ( item => item . success )
108
115
const pkgNameList = processPlugins . map ( item => item . pkgName )
109
116
if ( pkgNameList . length > 0 ) {
110
117
// update plugins must use pkgNameList:
111
118
// npm update will use the package.json's name
112
- const result = await this . execCommand ( 'update' , pkgNameList , this . ctx . baseDir , proxy , env )
119
+ const result = await this . execCommand ( 'update' , pkgNameList , this . ctx . baseDir , options , env )
113
120
if ( ! result . code ) {
114
121
this . ctx . log . success ( '插件更新成功' )
115
122
this . ctx . emit ( 'updateSuccess' , {
@@ -134,8 +141,10 @@ class PluginHandler {
134
141
}
135
142
}
136
143
137
- async execCommand ( cmd : string , modules : string [ ] , where : string , proxy : string = '' , env : IProcessEnv = { } ) : Promise < IResult > {
138
- const registry = this . ctx . getConfig < Undefinable < string > > ( 'registry' )
144
+ private async execCommand ( cmd : string , modules : string [ ] , where : string , options : IPluginHandlerOptions = { } , env : IProcessEnv = { } ) : Promise < IResult > {
145
+ // options first
146
+ const registry = options . registry || this . ctx . getConfig < Undefinable < string > > ( 'settings.registry' )
147
+ const proxy = options . proxy || this . ctx . getConfig < Undefinable < string > > ( 'settings.proxy' )
139
148
return await new Promise ( ( resolve : any ) : void => {
140
149
let args = [ cmd ] . concat ( modules ) . concat ( '--color=always' ) . concat ( '--save' )
141
150
if ( registry ) {
0 commit comments