Skip to content

Commit 14f408a

Browse files
committed
plugin: Plugin modules now export a single top level identifier
Makes typing much easier. Addresse's Will's last comment.
1 parent 8a8159c commit 14f408a

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

src/node/plugin.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,16 @@ export class PluginAPI {
189189
)
190190
}
191191

192+
const pluginModule = require(dir)
193+
if (!pluginModule.plugin) {
194+
throw new Error("plugin module does not export a plugin")
195+
}
196+
192197
const p = {
193198
name: packageJSON.name,
194199
version: packageJSON.version,
195200
modulePath: dir,
196-
...require(dir),
201+
...pluginModule.plugin,
197202
} as Plugin
198203

199204
if (!p.displayName) {

test/test-plugin/src/index.ts

+27-25
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,35 @@ import * as express from "express"
22
import * as fspath from "path"
33
import * as pluginapi from "../../../typings/pluginapi"
44

5-
export const displayName = "Test Plugin"
6-
export const routerPath = "/test-plugin"
7-
export const homepageURL = "https://example.com"
8-
export const description = "Plugin used in code-server tests."
5+
export const plugin: pluginapi.Plugin = {
6+
displayName: "Test Plugin",
7+
routerPath: "/test-plugin",
8+
homepageURL: "https://example.com",
9+
description: "Plugin used in code-server tests.",
910

10-
export function init(config: pluginapi.PluginConfig) {
11-
config.logger.debug("test-plugin loaded!")
12-
}
11+
init: (config) => {
12+
config.logger.debug("test-plugin loaded!")
13+
},
1314

14-
export function router(): express.Router {
15-
const r = express.Router()
16-
r.get("/goland/icon.svg", (req, res) => {
17-
res.sendFile(fspath.resolve(__dirname, "../public/icon.svg"))
18-
})
19-
return r
20-
}
15+
router: () => {
16+
const r = express.Router()
17+
r.get("/goland/icon.svg", (req, res) => {
18+
res.sendFile(fspath.resolve(__dirname, "../public/icon.svg"))
19+
})
20+
return r
21+
},
2122

22-
export function applications(): pluginapi.Application[] {
23-
return [
24-
{
25-
name: "Test App",
26-
version: "4.0.0",
27-
iconPath: "/icon.svg",
28-
path: "/test-app",
23+
applications: () => {
24+
return [
25+
{
26+
name: "Test App",
27+
version: "4.0.0",
28+
iconPath: "/icon.svg",
29+
path: "/test-app",
2930

30-
description: "This app does XYZ.",
31-
homepageURL: "https://example.com",
32-
},
33-
]
31+
description: "This app does XYZ.",
32+
homepageURL: "https://example.com",
33+
},
34+
]
35+
},
3436
}

typings/pluginapi.d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import * as express from "express"
1616
/**
1717
* Plugins
1818
*
19-
* Plugins are just node modules.
19+
* Plugins are just node modules that contain a top level export "plugin" that implements
20+
* the Plugin interface.
2021
*
2122
* 1. code-server uses $CS_PLUGIN to find plugins.
2223
*
@@ -78,7 +79,7 @@ import * as express from "express"
7879
*/
7980

8081
/**
81-
* Your plugin module must implement this interface.
82+
* Your plugin module must have a top level export "plugin" that implements this interface.
8283
*
8384
* The plugin's router will be mounted at <code-sever-root>/<plugin-path>
8485
*/

0 commit comments

Comments
 (0)