Skip to content

Commit 5f1fab7

Browse files
committed
Re-export logger field for plugins
1 parent 6ce2931 commit 5f1fab7

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

.eslintrc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ rules:
3535
[error, { alphabetize: { order: "asc" }, groups: [["builtin", "external", "internal"], "parent", "sibling"] }]
3636
no-async-promise-executor: off
3737
# This isn't a real module, just types, which apparently doesn't resolve.
38-
import/no-unresolved: [error, { ignore: ["express-serve-static-core"] }]
38+
import/no-unresolved: [error, { ignore: ["express-serve-static-core", "code-server"] }]
3939

4040
settings:
4141
# Does not work with CommonJS unfortunately.

src/node/plugin.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ import { version } from "./constants"
88
import * as util from "./util"
99
const fsp = fs.promises
1010

11+
/**
12+
* Inject code-server when `require`d. This is required because the API provides
13+
* more than just types so these need to be provided at run-time.
14+
*/
15+
const originalLoad = require("module")._load
16+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
17+
require("module")._load = function (request: string, parent: object, isMain: boolean): any {
18+
if (request === "code-server") {
19+
return {
20+
field,
21+
}
22+
}
23+
return originalLoad.apply(this, [request, parent, isMain])
24+
}
25+
1126
interface Plugin extends pluginapi.Plugin {
1227
/**
1328
* These fields are populated from the plugin's package.json

test/test-plugin/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import * as cs from "code-server"
12
import * as express from "express"
23
import * as fspath from "path"
3-
import * as pluginapi from "../../../typings/pluginapi"
44

5-
export const plugin: pluginapi.Plugin = {
5+
export const plugin: cs.Plugin = {
66
displayName: "Test Plugin",
77
routerPath: "/test-plugin",
88
homepageURL: "https://example.com",

test/test-plugin/tsconfig.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242

4343
/* Module Resolution Options */
4444
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
45-
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
46-
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
45+
"baseUrl": "./" /* Base directory to resolve non-absolute module names. */,
46+
"paths": {
47+
"code-server": ["../../typings/pluginapi"]
48+
} /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */,
4749
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
4850
// "typeRoots": [], /* List of folders to include type definitions from. */
4951
// "types": [], /* Type declaration files to be included in compilation. */

typings/pluginapi.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* This file describes the code-server plugin API for adding new applications.
33
*/
4-
import { Logger } from "@coder/logger"
4+
import { field, Logger } from "@coder/logger"
55
import * as express from "express"
66

77
/**
@@ -78,6 +78,13 @@ import * as express from "express"
7878
* ]
7979
*/
8080

81+
/**
82+
* Use to add a field to a log.
83+
*
84+
* Re-exported so plugins don't have to import duplicate copies of the logger.
85+
*/
86+
export { field }
87+
8188
/**
8289
* Your plugin module must have a top level export "plugin" that implements this interface.
8390
*

0 commit comments

Comments
 (0)