Skip to content

Commit

Permalink
feat(plugin): ✨ Add override settings to plugin loading
Browse files Browse the repository at this point in the history
  • Loading branch information
CPlusPatch committed Oct 6, 2024
1 parent c0805ff commit f26ab0f
Show file tree
Hide file tree
Showing 7 changed files with 697 additions and 540 deletions.
2 changes: 1 addition & 1 deletion .github/config.workflow.toml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,6 @@ max_coeff = 1.0

[plugins]

[plugins."@versia/openid".keys]
[plugins.config."@versia/openid".keys]
private = "MC4CAQAwBQYDK2VwBCIEID+H5n9PY3zVKZQcq4jrnE1IiRd2EWWr8ApuHUXmuOzl"
public = "MCowBQYDK2VwAyEAzenliNkgpXYsh3gXTnAoUWzlCPjIOppmAVx2DBlLsC8="
11 changes: 8 additions & 3 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,24 @@ export const appFactory = async () => {

const loader = new PluginLoader();

const plugins = await loader.loadPlugins(join(process.cwd(), "plugins"));
const plugins = await loader.loadPlugins(
join(process.cwd(), "plugins"),
config.plugins?.autoload,
config.plugins?.overrides.enabled,
config.plugins?.overrides.disabled,
);

for (const data of plugins) {
serverLogger.info`Loading plugin ${chalk.blueBright(data.manifest.name)} ${chalk.blueBright(data.manifest.version)} ${chalk.gray(`[${plugins.indexOf(data) + 1}/${plugins.length}]`)}`;
try {
// biome-ignore lint/complexity/useLiteralKeys: loadConfig is a private method
await data.plugin["_loadConfig"](
config.plugins?.[data.manifest.name],
config.plugins?.config?.[data.manifest.name],
);
} catch (e) {
serverLogger.fatal`Plugin configuration is invalid: ${chalk.redBright(e as ValidationError)}`;
serverLogger.fatal`Put your configuration at ${chalk.blueBright(
"plugins.<plugin-name>",
"plugins.config.<plugin-name>",
)}`;
throw new Error("Plugin configuration is invalid");
}
Expand Down
2 changes: 1 addition & 1 deletion classes/plugin/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 31 additions & 1 deletion classes/plugin/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,49 @@ 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`,
);

return { manifest, plugin: pluginInstance };
}),
);
).then((data) => data.filter((d) => d !== null));
}
}
39 changes: 37 additions & 2 deletions config/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4007,7 +4007,41 @@
},
"plugins": {
"type": "object",
"additionalProperties": {}
"properties": {
"autoload": {
"type": "boolean",
"default": true
},
"overrides": {
"type": "object",
"properties": {
"enabled": {
"type": "array",
"items": {
"type": "string"
},
"default": []
},
"disabled": {
"type": "array",
"items": {
"type": "string"
},
"default": []
}
},
"additionalProperties": false,
"default": {
"enabled": [],
"disabled": []
}
},
"config": {
"type": "object",
"additionalProperties": {}
}
},
"additionalProperties": false
}
},
"required": [
Expand All @@ -4019,7 +4053,8 @@
"http",
"smtp",
"filters",
"ratelimits"
"ratelimits",
"plugins"
],
"additionalProperties": false,
"$schema": "http://json-schema.org/draft-07/schema#"
Expand Down
Loading

0 comments on commit f26ab0f

Please sign in to comment.