Skip to content

Commit 06ffc09

Browse files
committed
Add variables to better customize plugin directories
1 parent 9d87c53 commit 06ffc09

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/node/plugin.ts

+28-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { HttpServer } from "./http"
99

1010
export type Activate = (httpServer: HttpServer, args: Args) => void
1111

12+
/**
13+
* Plugins must implement this interface.
14+
*/
1215
export interface Plugin {
1316
activate: Activate
1417
}
@@ -23,6 +26,9 @@ require("module")._load = function (request: string, parent: object, isMain: boo
2326
return originalLoad.apply(this, [request.replace(/^code-server/, path.resolve(__dirname, "../..")), parent, isMain])
2427
}
2528

29+
/**
30+
* Load a plugin and run its activation function.
31+
*/
2632
const loadPlugin = async (pluginPath: string, httpServer: HttpServer, args: Args): Promise<void> => {
2733
try {
2834
const plugin: Plugin = require(pluginPath)
@@ -37,24 +43,34 @@ const loadPlugin = async (pluginPath: string, httpServer: HttpServer, args: Args
3743
}
3844
}
3945

40-
const _loadPlugins = async (httpServer: HttpServer, args: Args): Promise<void> => {
41-
const pluginPath = path.resolve(__dirname, "../../plugins")
42-
const files = await util.promisify(fs.readdir)(pluginPath, {
43-
withFileTypes: true,
44-
})
45-
await Promise.all(files.map((file) => loadPlugin(path.join(pluginPath, file.name), httpServer, args)))
46-
}
47-
48-
export const loadPlugins = async (httpServer: HttpServer, args: Args): Promise<void> => {
46+
/**
47+
* Load all plugins in the specified directory.
48+
*/
49+
const _loadPlugins = async (pluginDir: string, httpServer: HttpServer, args: Args): Promise<void> => {
4950
try {
50-
await _loadPlugins(httpServer, args)
51+
const files = await util.promisify(fs.readdir)(pluginDir, {
52+
withFileTypes: true,
53+
})
54+
await Promise.all(files.map((file) => loadPlugin(path.join(pluginDir, file.name), httpServer, args)))
5155
} catch (error) {
5256
if (error.code !== "ENOENT") {
5357
logger.warn(error.message)
5458
}
5559
}
60+
}
61+
62+
/**
63+
* Load all plugins from the plugin directory. Defaults to `plugins` inside the
64+
* code-server directory but can be set with `PLUGIN_DIR`.
65+
66+
* Also load any individual plugins found in `PLUGIN_DIRS` (colon-separated).
67+
* This allows you to test and develop plugins without having to move them into
68+
* one directory.
69+
*/
70+
export const loadPlugins = async (httpServer: HttpServer, args: Args): Promise<void> => {
71+
await _loadPlugins(path.resolve(process.env.PLUGIN_DIR || path.join(__dirname, "../../plugins")), httpServer, args)
5672

57-
if (process.env.PLUGIN_DIR) {
58-
await loadPlugin(process.env.PLUGIN_DIR, httpServer, args)
73+
if (process.env.PLUGIN_DIRS) {
74+
await Promise.all(process.env.PLUGIN_DIRS.split(":").map((dir) => loadPlugin(dir, httpServer, args)))
5975
}
6076
}

0 commit comments

Comments
 (0)