@@ -10,11 +10,11 @@ const fsp = fs.promises
10
10
11
11
interface Plugin extends pluginapi . Plugin {
12
12
/**
13
- * These fields are populated from the plugin's package.json.
13
+ * These fields are populated from the plugin's package.json
14
+ * and now guaranteed to exist.
14
15
*/
15
16
name : string
16
17
version : string
17
- description : string
18
18
19
19
/**
20
20
* path to the node module on the disk.
@@ -34,7 +34,7 @@ interface Application extends pluginapi.Application {
34
34
* Please see that file for details.
35
35
*/
36
36
export class PluginAPI {
37
- private readonly plugins = new Array < Plugin > ( )
37
+ private readonly plugins = new Map < string , Plugin > ( )
38
38
private readonly logger : Logger
39
39
40
40
public constructor (
@@ -54,7 +54,7 @@ export class PluginAPI {
54
54
*/
55
55
public async applications ( ) : Promise < Application [ ] > {
56
56
const apps = new Array < Application > ( )
57
- for ( const p of this . plugins ) {
57
+ for ( const [ _ , p ] of this . plugins ) {
58
58
const pluginApps = await p . applications ( )
59
59
60
60
// Add plugin key to each app.
@@ -65,8 +65,11 @@ export class PluginAPI {
65
65
plugin : {
66
66
name : p . name ,
67
67
version : p . version ,
68
- description : p . description ,
69
68
modulePath : p . modulePath ,
69
+
70
+ displayName : p . displayName ,
71
+ description : p . description ,
72
+ path : p . path ,
70
73
} ,
71
74
}
72
75
} ) ,
@@ -79,7 +82,7 @@ export class PluginAPI {
79
82
* mount mounts all plugin routers onto r.
80
83
*/
81
84
public mount ( r : express . Router ) : void {
82
- for ( const p of this . plugins ) {
85
+ for ( const [ _ , p ] of this . plugins ) {
83
86
r . use ( `/${ p . name } ` , p . router ( ) )
84
87
}
85
88
}
@@ -129,7 +132,7 @@ export class PluginAPI {
129
132
encoding : "utf8" ,
130
133
} )
131
134
const packageJSON : PackageJSON = JSON . parse ( str )
132
- for ( const p of this . plugins ) {
135
+ for ( const [ _ , p ] of this . plugins ) {
133
136
if ( p . name === packageJSON . name ) {
134
137
this . logger . warn (
135
138
`ignoring duplicate plugin ${ q ( p . name ) } at ${ q ( dir ) } , using previously loaded ${ q ( p . modulePath ) } ` ,
@@ -138,7 +141,7 @@ export class PluginAPI {
138
141
}
139
142
}
140
143
const p = this . _loadPlugin ( dir , packageJSON )
141
- this . plugins . push ( p )
144
+ this . plugins . set ( p . name , p )
142
145
} catch ( err ) {
143
146
if ( err . code !== "ENOENT" ) {
144
147
this . logger . warn ( `failed to load plugin: ${ err . message } ` )
@@ -147,6 +150,8 @@ export class PluginAPI {
147
150
}
148
151
149
152
private _loadPlugin ( dir : string , packageJSON : PackageJSON ) : Plugin {
153
+ dir = path . resolve ( dir )
154
+
150
155
const logger = this . logger . named ( packageJSON . name )
151
156
logger . debug ( "loading plugin" , field ( "plugin_dir" , dir ) , field ( "package_json" , packageJSON ) )
152
157
@@ -165,11 +170,20 @@ export class PluginAPI {
165
170
const p = {
166
171
name : packageJSON . name ,
167
172
version : packageJSON . version ,
168
- description : packageJSON . description ,
169
173
modulePath : dir ,
170
174
...require ( dir ) ,
171
175
} as Plugin
172
176
177
+ if ( ! p . displayName ) {
178
+ throw new Error ( "plugin missing displayName" )
179
+ }
180
+ if ( ! p . description ) {
181
+ throw new Error ( "plugin missing description" )
182
+ }
183
+ if ( ! p . path ) {
184
+ throw new Error ( "plugin missing path" )
185
+ }
186
+
173
187
p . init ( {
174
188
logger : logger ,
175
189
} )
@@ -183,7 +197,6 @@ export class PluginAPI {
183
197
interface PackageJSON {
184
198
name : string
185
199
version : string
186
- description : string
187
200
engines : {
188
201
"code-server" : string
189
202
}
0 commit comments