@@ -10,11 +10,11 @@ const fsp = fs.promises
1010
1111interface Plugin extends pluginapi . Plugin {
1212 /**
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.
1415 */
1516 name : string
1617 version : string
17- description : string
1818
1919 /**
2020 * path to the node module on the disk.
@@ -34,7 +34,7 @@ interface Application extends pluginapi.Application {
3434 * Please see that file for details.
3535 */
3636export class PluginAPI {
37- private readonly plugins = new Array < Plugin > ( )
37+ private readonly plugins = new Map < string , Plugin > ( )
3838 private readonly logger : Logger
3939
4040 public constructor (
@@ -54,7 +54,7 @@ export class PluginAPI {
5454 */
5555 public async applications ( ) : Promise < Application [ ] > {
5656 const apps = new Array < Application > ( )
57- for ( const p of this . plugins ) {
57+ for ( const [ _ , p ] of this . plugins ) {
5858 const pluginApps = await p . applications ( )
5959
6060 // Add plugin key to each app.
@@ -65,8 +65,11 @@ export class PluginAPI {
6565 plugin : {
6666 name : p . name ,
6767 version : p . version ,
68- description : p . description ,
6968 modulePath : p . modulePath ,
69+
70+ displayName : p . displayName ,
71+ description : p . description ,
72+ path : p . path ,
7073 } ,
7174 }
7275 } ) ,
@@ -79,7 +82,7 @@ export class PluginAPI {
7982 * mount mounts all plugin routers onto r.
8083 */
8184 public mount ( r : express . Router ) : void {
82- for ( const p of this . plugins ) {
85+ for ( const [ _ , p ] of this . plugins ) {
8386 r . use ( `/${ p . name } ` , p . router ( ) )
8487 }
8588 }
@@ -129,7 +132,7 @@ export class PluginAPI {
129132 encoding : "utf8" ,
130133 } )
131134 const packageJSON : PackageJSON = JSON . parse ( str )
132- for ( const p of this . plugins ) {
135+ for ( const [ _ , p ] of this . plugins ) {
133136 if ( p . name === packageJSON . name ) {
134137 this . logger . warn (
135138 `ignoring duplicate plugin ${ q ( p . name ) } at ${ q ( dir ) } , using previously loaded ${ q ( p . modulePath ) } ` ,
@@ -138,7 +141,7 @@ export class PluginAPI {
138141 }
139142 }
140143 const p = this . _loadPlugin ( dir , packageJSON )
141- this . plugins . push ( p )
144+ this . plugins . set ( p . name , p )
142145 } catch ( err ) {
143146 if ( err . code !== "ENOENT" ) {
144147 this . logger . warn ( `failed to load plugin: ${ err . message } ` )
@@ -147,6 +150,8 @@ export class PluginAPI {
147150 }
148151
149152 private _loadPlugin ( dir : string , packageJSON : PackageJSON ) : Plugin {
153+ dir = path . resolve ( dir )
154+
150155 const logger = this . logger . named ( packageJSON . name )
151156 logger . debug ( "loading plugin" , field ( "plugin_dir" , dir ) , field ( "package_json" , packageJSON ) )
152157
@@ -165,11 +170,20 @@ export class PluginAPI {
165170 const p = {
166171 name : packageJSON . name ,
167172 version : packageJSON . version ,
168- description : packageJSON . description ,
169173 modulePath : dir ,
170174 ...require ( dir ) ,
171175 } as Plugin
172176
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+
173187 p . init ( {
174188 logger : logger ,
175189 } )
@@ -183,7 +197,6 @@ export class PluginAPI {
183197interface PackageJSON {
184198 name : string
185199 version : string
186- description : string
187200 engines : {
188201 "code-server" : string
189202 }
0 commit comments