From 14f408a837cbdd301545d4241da4e6ecd90857cb Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Wed, 4 Nov 2020 23:10:41 -0500 Subject: [PATCH] plugin: Plugin modules now export a single top level identifier Makes typing much easier. Addresse's Will's last comment. --- src/node/plugin.ts | 7 ++++- test/test-plugin/src/index.ts | 58 ++++++++++++++++++----------------- typings/pluginapi.d.ts | 5 +-- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 0ac3abfc..71831f50 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -189,11 +189,16 @@ export class PluginAPI { ) } + const pluginModule = require(dir) + if (!pluginModule.plugin) { + throw new Error("plugin module does not export a plugin") + } + const p = { name: packageJSON.name, version: packageJSON.version, modulePath: dir, - ...require(dir), + ...pluginModule.plugin, } as Plugin if (!p.displayName) { diff --git a/test/test-plugin/src/index.ts b/test/test-plugin/src/index.ts index 2fc1ddab..2b00c2ec 100644 --- a/test/test-plugin/src/index.ts +++ b/test/test-plugin/src/index.ts @@ -2,33 +2,35 @@ import * as express from "express" import * as fspath from "path" import * as pluginapi from "../../../typings/pluginapi" -export const displayName = "Test Plugin" -export const routerPath = "/test-plugin" -export const homepageURL = "https://example.com" -export const description = "Plugin used in code-server tests." +export const plugin: pluginapi.Plugin = { + displayName: "Test Plugin", + routerPath: "/test-plugin", + homepageURL: "https://example.com", + description: "Plugin used in code-server tests.", -export function init(config: pluginapi.PluginConfig) { - config.logger.debug("test-plugin loaded!") -} - -export function router(): express.Router { - const r = express.Router() - r.get("/goland/icon.svg", (req, res) => { - res.sendFile(fspath.resolve(__dirname, "../public/icon.svg")) - }) - return r -} - -export function applications(): pluginapi.Application[] { - return [ - { - name: "Test App", - version: "4.0.0", - iconPath: "/icon.svg", - path: "/test-app", - - description: "This app does XYZ.", - homepageURL: "https://example.com", - }, - ] + init: (config) => { + config.logger.debug("test-plugin loaded!") + }, + + router: () => { + const r = express.Router() + r.get("/goland/icon.svg", (req, res) => { + res.sendFile(fspath.resolve(__dirname, "../public/icon.svg")) + }) + return r + }, + + applications: () => { + return [ + { + name: "Test App", + version: "4.0.0", + iconPath: "/icon.svg", + path: "/test-app", + + description: "This app does XYZ.", + homepageURL: "https://example.com", + }, + ] + }, } diff --git a/typings/pluginapi.d.ts b/typings/pluginapi.d.ts index 4e3971ee..d0846a28 100644 --- a/typings/pluginapi.d.ts +++ b/typings/pluginapi.d.ts @@ -16,7 +16,8 @@ import * as express from "express" /** * Plugins * - * Plugins are just node modules. + * Plugins are just node modules that contain a top level export "plugin" that implements + * the Plugin interface. * * 1. code-server uses $CS_PLUGIN to find plugins. * @@ -78,7 +79,7 @@ import * as express from "express" */ /** - * Your plugin module must implement this interface. + * Your plugin module must have a top level export "plugin" that implements this interface. * * The plugin's router will be mounted at / */