-
-
Notifications
You must be signed in to change notification settings - Fork 723
/
Copy pathplugins.ts
63 lines (58 loc) · 2.23 KB
/
plugins.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { isAbsolute } from "path";
import { pathToFileURL } from "url";
import type { Application } from "../application.js";
import { nicePath } from "./paths.js";
import type { TranslatedString } from "../internationalization/internationalization.js";
export async function loadPlugins(
app: Application,
plugins: readonly string[],
) {
for (const plugin of plugins) {
const pluginDisplay = getPluginDisplayName(plugin);
try {
let instance: any;
// Try importing first to avoid warnings about requiring ESM being experimental.
// If that fails due to importing a directory, fall back to require.
try {
// On Windows, we need to ensure this path is a file path.
// Or we'll get ERR_UNSUPPORTED_ESM_URL_SCHEME
const esmPath = isAbsolute(plugin)
? pathToFileURL(plugin).toString()
: plugin;
instance = await import(esmPath);
} catch (error: any) {
if (error.code === "ERR_UNSUPPORTED_DIR_IMPORT") {
// eslint-disable-next-line @typescript-eslint/no-require-imports
instance = require(plugin);
} else {
throw error;
}
}
const initFunction = instance.load;
if (typeof initFunction === "function") {
await initFunction(app);
app.logger.info(app.i18n.loaded_plugin_0(pluginDisplay));
} else {
app.logger.error(
app.i18n.invalid_plugin_0_missing_load_function(
pluginDisplay,
),
);
}
} catch (error) {
app.logger.error(
app.i18n.plugin_0_could_not_be_loaded(pluginDisplay),
);
if (error instanceof Error && error.stack) {
app.logger.error(error.stack as TranslatedString);
}
}
}
}
function getPluginDisplayName(plugin: string) {
const path = nicePath(plugin);
if (path.startsWith("./node_modules/")) {
return path.substring("./node_modules/".length);
}
return plugin;
}