reworked logic to remove config when deleting a plugin from plugin_collection

This commit is contained in:
Jim Unger 2016-09-15 15:11:43 -05:00
parent 2802410573
commit 2f52be62ae
2 changed files with 33 additions and 19 deletions

View file

@ -89,17 +89,14 @@ module.exports = class Plugin {
};
}
async readConfig() {
async readConfigSchema() {
let schema = await this.getConfigSchema(Joi);
let { config } = this.kbnServer;
config.extendSchema(this.configPrefix, schema || defaultConfigSchema);
return schema || defaultConfigSchema;
}
if (config.get([...toPath(this.configPrefix), 'enabled'])) {
return true;
} else {
config.removeSchema(this.configPrefix);
return false;
}
get enabled() {
const { config } = this.kbnServer;
return config.get([...toPath(this.configPrefix), 'enabled']);
}
async preInit() {

View file

@ -2,11 +2,23 @@
import PluginApi from './plugin_api';
import { inspect } from 'util';
import { get, indexBy } from 'lodash';
import toPath from 'lodash/internal/toPath';
import Collection from '../../utils/collection';
let byIdCache = Symbol('byIdCache');
let pluginApis = Symbol('pluginApis');
async function addPluginConfig(pluginCollection, plugin) {
const configSchema = await plugin.readConfigSchema();
let { config } = pluginCollection.kbnServer;
config.extendSchema(plugin.configPrefix, configSchema);
}
function removePluginConfig(pluginCollection, plugin) {
let { config } = pluginCollection.kbnServer;
config.removeSchema(plugin.configPrefix);
}
module.exports = class Plugins extends Collection {
constructor(kbnServer) {
@ -27,21 +39,26 @@ module.exports = class Plugins extends Collection {
// clear the byIdCache
this[byIdCache] = null;
for (let product of output) {
if (product instanceof api.Plugin) {
let plugin = product;
this.add(plugin);
let enabled = await plugin.readConfig();
if (!enabled) this.delete(plugin);
continue;
for (let plugin of output) {
if (!plugin instanceof api.Plugin) {
throw new TypeError('unexpected plugin export ' + inspect(plugin));
}
throw new TypeError('unexpected plugin export ' + inspect(product));
await this.add(plugin);
if (!plugin.enabled) this.delete(plugin);
}
}
async add(plugin) {
await addPluginConfig(this, plugin);
super.add(plugin);
}
delete(plugin) {
removePluginConfig(this, plugin);
super.delete(plugin);
}
get byId() {
return this[byIdCache] || (this[byIdCache] = indexBy([...this], 'id'));
}