1
+ /* eslint-disable @typescript-eslint/no-misused-promises */
1
2
import { Command } from 'commander'
2
3
import inquirer , { Inquirer } from 'inquirer'
3
4
import { IPlugin , ICommander , IPicGo } from '../types'
4
5
import commanders from '../plugins/commander'
6
+ import { getCurrentPluginName } from './LifecyclePlugins'
5
7
6
8
export class Commander implements ICommander {
7
- private list : {
8
- [ propName : string ] : IPlugin
9
- }
9
+ private readonly name = 'commander'
10
+ static currentPlugin : string | null
11
+ private readonly list : Map < string , IPlugin > = new Map ( )
12
+ private readonly pluginIdMap : Map < string , string [ ] > = new Map ( )
13
+ private readonly ctx : IPicGo
10
14
11
15
program : Command
12
16
inquirer : Inquirer
13
- private readonly ctx : IPicGo
14
17
15
18
constructor ( ctx : IPicGo ) {
16
- this . list = { }
17
19
this . program = new Command ( )
18
20
this . inquirer = inquirer
19
21
this . ctx = ctx
20
22
}
21
23
24
+ getName ( ) : string {
25
+ return this . name
26
+ }
27
+
22
28
init ( ) : void {
23
29
this . program
24
30
. version ( process . env . PICGO_VERSION , '-v, --version' )
@@ -41,24 +47,50 @@ export class Commander implements ICommander {
41
47
commanders ( this . ctx )
42
48
}
43
49
44
- register ( name : string , plugin : IPlugin ) : void {
45
- if ( ! name ) throw new TypeError ( 'name is required!' )
50
+ register ( id : string , plugin : IPlugin ) : void {
51
+ if ( ! id ) throw new TypeError ( 'name is required!' )
46
52
if ( typeof plugin . handle !== 'function' ) throw new TypeError ( 'plugin.handle must be a function!' )
47
- if ( name in this . list ) throw new TypeError ( 'duplicate name!' )
53
+ if ( this . list . has ( id ) ) throw new TypeError ( `${ this . name } plugin duplicate id: ${ id } !` )
54
+ this . list . set ( id , plugin )
55
+ const currentPluginName = getCurrentPluginName ( )
56
+ if ( currentPluginName !== null ) {
57
+ if ( this . pluginIdMap . has ( currentPluginName ) ) {
58
+ this . pluginIdMap . get ( currentPluginName ) ?. push ( id )
59
+ } else {
60
+ this . pluginIdMap . set ( currentPluginName , [ id ] )
61
+ }
62
+ }
63
+ }
48
64
49
- this . list [ name ] = plugin
65
+ unregister ( pluginName : string ) : void {
66
+ if ( this . pluginIdMap . has ( pluginName ) ) {
67
+ const pluginList = this . pluginIdMap . get ( pluginName )
68
+ pluginList ?. forEach ( ( plugin : string ) => {
69
+ this . list . delete ( plugin )
70
+ } )
71
+ }
50
72
}
51
73
52
74
loadCommands ( ) : void {
53
- Object . keys ( this . list ) . map ( ( item : string ) => this . list [ item ] . handle ( this . ctx ) )
75
+ this . getList ( ) . forEach ( ( item : IPlugin ) => {
76
+ try {
77
+ item . handle ( this . ctx )
78
+ } catch ( e : any ) {
79
+ this . ctx . log . error ( e )
80
+ }
81
+ } )
54
82
}
55
83
56
- get ( name : string ) : IPlugin {
57
- return this . list [ name ]
84
+ get ( id : string ) : IPlugin | undefined {
85
+ return this . list . get ( id )
58
86
}
59
87
60
88
getList ( ) : IPlugin [ ] {
61
- return Object . keys ( this . list ) . map ( ( item : string ) => this . list [ item ] )
89
+ return [ ...this . list . values ( ) ]
90
+ }
91
+
92
+ getIdList ( ) : string [ ] {
93
+ return [ ...this . list . keys ( ) ]
62
94
}
63
95
}
64
96
0 commit comments