Skip to content

Commit afff86a

Browse files
committed
plugin.ts: Adjust to implement pluginapi.d.ts correctly
1 parent fed545e commit afff86a

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

src/node/plugin.ts

+23-10
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ const fsp = fs.promises
1010

1111
interface 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
*/
3636
export 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 {
183197
interface PackageJSON {
184198
name: string
185199
version: string
186-
description: string
187200
engines: {
188201
"code-server": string
189202
}

test/plugin.test.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ describe("plugin", () => {
1717
assert.deepEqual(
1818
[
1919
{
20-
name: "goland",
20+
name: "test app",
2121
version: "4.0.0",
22+
23+
description: "my description",
2224
iconPath: "/icon.svg",
25+
2326
plugin: {
2427
name: "test-plugin",
2528
version: "1.0.0",
26-
description: "Fake plugin for testing code-server's plugin API",
2729
modulePath: path.join(__dirname, "test-plugin"),
30+
31+
description: "Plugin used in code-server tests.",
32+
displayName: "Test Plugin",
33+
path: "/test-plugin",
2834
},
2935
},
3036
],

test/test-plugin/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"private": true,
33
"name": "test-plugin",
44
"version": "1.0.0",
5-
"description": "Fake plugin for testing code-server's plugin API",
65
"engines": {
76
"code-server": "^3.6.0"
87
},

test/test-plugin/src/index.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
import * as express from "express"
2-
import * as path from "path"
2+
import * as fspath from "path"
33
import * as pluginapi from "../../../typings/pluginapi"
44

5+
export const displayName = "Test Plugin"
6+
export const path = "/test-plugin"
7+
export const description = "Plugin used in code-server tests."
8+
59
export function init(config: pluginapi.PluginConfig) {
610
config.logger.debug("test-plugin loaded!")
711
}
812

913
export function router(): express.Router {
1014
const r = express.Router()
1115
r.get("/goland/icon.svg", (req, res) => {
12-
res.sendFile(path.resolve(__dirname, "../public/icon.svg"))
16+
res.sendFile(fspath.resolve(__dirname, "../public/icon.svg"))
1317
})
1418
return r
1519
}
1620

1721
export function applications(): pluginapi.Application[] {
1822
return [
1923
{
20-
name: "goland",
24+
name: "test app",
2125
version: "4.0.0",
2226
iconPath: "/icon.svg",
27+
28+
description: "my description",
2329
},
2430
]
2531
}

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"tsBuildInfoFile": "./.cache/tsbuildinfo",
1717
"incremental": true,
1818
"rootDir": "./src",
19-
"typeRoots": ["./node_modules/@types", "./typings"]
19+
"typeRoots": ["./node_modules/@types", "./typings"],
20+
"downlevelIteration": true
2021
},
2122
"include": ["./src/**/*.ts"]
2223
}

typings/pluginapi.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,14 @@ export interface Plugin {
7070
version?: string
7171

7272
/**
73-
* These two are used in the overlay.
73+
* Name used in the overlay.
7474
*/
7575
displayName: string
76+
77+
/**
78+
* Used in overlay.
79+
* Should be a full sentence describing the plugin.
80+
*/
7681
description: string
7782

7883
/**

0 commit comments

Comments
 (0)