*Add new pluginConfig api that scopes all get, set, and has methods to a plugin.${plugin} scope to access only that plugins config

This commit is contained in:
Derrick Hammer 2022-12-21 15:12:20 -05:00
parent 5a4537ffd3
commit 3d29026a22
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 33 additions and 0 deletions

View File

@ -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>(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];
},
})