@@ -4,11 +4,15 @@ import * as path from "path"
44import * as util from "util"
55import { Args } from "./cli"
66import { HttpServer } from "./http"
7+ import { paths } from "./util"
78
89/* eslint-disable @typescript-eslint/no-var-requires */
910
1011export type Activate = ( httpServer : HttpServer , args : Args ) => void
1112
13+ /**
14+ * Plugins must implement this interface.
15+ */
1216export interface Plugin {
1317 activate : Activate
1418}
@@ -23,38 +27,60 @@ require("module")._load = function (request: string, parent: object, isMain: boo
2327 return originalLoad . apply ( this , [ request . replace ( / ^ c o d e - s e r v e r / , path . resolve ( __dirname , "../.." ) ) , parent , isMain ] )
2428}
2529
30+ /**
31+ * Load a plugin and run its activation function.
32+ */
2633const loadPlugin = async ( pluginPath : string , httpServer : HttpServer , args : Args ) : Promise < void > => {
2734 try {
2835 const plugin : Plugin = require ( pluginPath )
2936 plugin . activate ( httpServer , args )
30- logger . debug ( "Loaded plugin" , field ( "name" , path . basename ( pluginPath ) ) )
37+
38+ const packageJson = require ( path . join ( pluginPath , "package.json" ) )
39+ logger . debug (
40+ "Loaded plugin" ,
41+ field ( "name" , packageJson . name || path . basename ( pluginPath ) ) ,
42+ field ( "path" , pluginPath ) ,
43+ field ( "version" , packageJson . version || "n/a" ) ,
44+ )
3145 } catch ( error ) {
32- if ( error . code !== "MODULE_NOT_FOUND" ) {
33- logger . warn ( error . message )
34- } else {
35- logger . error ( error . message )
36- }
46+ logger . error ( error . message )
3747 }
3848}
3949
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 > => {
50+ /**
51+ * Load all plugins in the specified directory.
52+ */
53+ const _loadPlugins = async ( pluginDir : string , httpServer : HttpServer , args : Args ) : Promise < void > => {
4954 try {
50- await _loadPlugins ( httpServer , args )
55+ const files = await util . promisify ( fs . readdir ) ( pluginDir , {
56+ withFileTypes : true ,
57+ } )
58+ await Promise . all ( files . map ( ( file ) => loadPlugin ( path . join ( pluginDir , file . name ) , httpServer , args ) ) )
5159 } catch ( error ) {
5260 if ( error . code !== "ENOENT" ) {
5361 logger . warn ( error . message )
5462 }
5563 }
64+ }
5665
57- if ( process . env . PLUGIN_DIR ) {
58- await loadPlugin ( process . env . PLUGIN_DIR , httpServer , args )
59- }
66+ /**
67+ * Load all plugins from the `plugins` directory, directories specified by
68+ * `CS_PLUGIN_PATH` (colon-separated), and individual plugins specified by
69+ * `CS_PLUGIN` (also colon-separated).
70+ */
71+ export const loadPlugins = async ( httpServer : HttpServer , args : Args ) : Promise < void > => {
72+ const pluginPath = process . env . CS_PLUGIN_PATH || `${ path . join ( paths . data , "plugins" ) } :/usr/share/code-server/plugins`
73+ const plugin = process . env . CS_PLUGIN || ""
74+ await Promise . all ( [
75+ // Built-in plugins.
76+ _loadPlugins ( path . resolve ( __dirname , "../../plugins" ) , httpServer , args ) ,
77+ // User-added plugins.
78+ ...pluginPath . split ( ":" ) . map ( ( dir ) => _loadPlugins ( path . resolve ( dir ) , httpServer , args ) ) ,
79+ // Individual plugins so you don't have to symlink or move them into a
80+ // directory specifically for plugins. This lets you load plugins that are
81+ // on the same level as other directories that are not plugins (if you tried
82+ // to use CS_PLUGIN_PATH code-server would try to load those other
83+ // directories as plugins). Intended for development.
84+ ...plugin . split ( ":" ) . map ( ( dir ) => loadPlugin ( path . resolve ( dir ) , httpServer , args ) ) ,
85+ ] )
6086}
0 commit comments