From 3d29026a2282b4658b07a91c489b262358bb57c2 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 21 Dec 2022 15:12:20 -0500 Subject: [PATCH] *Add new pluginConfig api that scopes all get, set, and has methods to a plugin.${plugin} scope to access only that plugins config --- src/modules/plugin.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/modules/plugin.ts b/src/modules/plugin.ts index 44a5819..ce13a8d 100644 --- a/src/modules/plugin.ts +++ b/src/modules/plugin.ts @@ -60,6 +60,10 @@ class PluginAPI extends EventEmitter2 { return this._config; } + get pluginConfig(): Config { + throw new Error("not implemented and should not be called"); + } + private _logger: Logger; get logger(): Logger { @@ -163,6 +167,35 @@ export class PluginAPIManager { }; } + if (prop === "pluginConfig") { + return new Proxy(config, { + get(target: Config, prop: string): any { + if (prop === "set") { + return (key: string, value: any): void => { + target.set(`plugin.${plugin.name}.${key}`, value); + }; + } + + if (prop === "get") { + return (key: string, fallback = null): any => { + return target.get( + `plugin.${plugin.name}.${key}`, + fallback + ); + }; + } + + if (prop === "has") { + return (key: string): any => { + return target.has(`plugin.${plugin.name}.${key}`); + }; + } + + throw new Error(`Invalid method accessed ${prop}`); + }, + }); + } + return (target as any)[prop]; }, })