mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
feat(plugin): ✨ Add override settings to plugin loading
This commit is contained in:
parent
c0805ff125
commit
f26ab0f0e6
7 changed files with 705 additions and 548 deletions
|
|
@ -201,7 +201,7 @@ describe("PluginLoader", () => {
|
|||
default: mockPlugin,
|
||||
}));
|
||||
|
||||
const plugins = await pluginLoader.loadPlugins("/some/path");
|
||||
const plugins = await pluginLoader.loadPlugins("/some/path", true);
|
||||
expect(plugins).toEqual([
|
||||
{
|
||||
manifest: manifestContent,
|
||||
|
|
|
|||
|
|
@ -162,12 +162,42 @@ export class PluginLoader {
|
|||
*/
|
||||
public async loadPlugins(
|
||||
dir: string,
|
||||
autoload: boolean,
|
||||
enabled?: string[],
|
||||
disabled?: string[],
|
||||
): Promise<{ manifest: Manifest; plugin: Plugin<ZodTypeAny> }[]> {
|
||||
const plugins = await PluginLoader.findPlugins(dir);
|
||||
|
||||
const enabledOn = (enabled?.length ?? 0) > 0;
|
||||
const disabledOn = (disabled?.length ?? 0) > 0;
|
||||
|
||||
if (enabledOn && disabledOn) {
|
||||
this.logger
|
||||
.fatal`Both enabled and disabled lists are specified. Only one of them can be used.`;
|
||||
throw new Error("Invalid configuration");
|
||||
}
|
||||
|
||||
return Promise.all(
|
||||
plugins.map(async (plugin) => {
|
||||
const manifest = await this.parseManifest(dir, plugin);
|
||||
|
||||
// If autoload is disabled, only load plugins explicitly enabled
|
||||
if (
|
||||
!(autoload || enabledOn || enabled?.includes(manifest.name))
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// If enabled is specified, only load plugins in the enabled list
|
||||
// If disabled is specified, only load plugins not in the disabled list
|
||||
if (enabledOn && !enabled?.includes(manifest.name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (disabled?.includes(manifest.name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const pluginInstance = await this.loadPlugin(
|
||||
dir,
|
||||
`${plugin}/index`,
|
||||
|
|
@ -175,6 +205,6 @@ export class PluginLoader {
|
|||
|
||||
return { manifest, plugin: pluginInstance };
|
||||
}),
|
||||
);
|
||||
).then((data) => data.filter((d) => d !== null));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue