-
-
Save fabiocorneti/4719f8149cf7573e4d961eb79b8340fe to your computer and use it in GitHub Desktop.
hotfix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var _plugin_api = _interopRequireDefault(require("./plugin_api")); | |
var _util = require("util"); | |
var _lodash = require("lodash"); | |
var _collection = require("../../utils/collection"); | |
var _transform_deprecations = require("../config/transform_deprecations"); | |
var _deprecation = require("../../deprecation"); | |
var _joi = _interopRequireDefault(require("joi")); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
const byIdCache = Symbol('byIdCache'); | |
const pluginApis = Symbol('pluginApis'); | |
async function addPluginConfig(pluginCollection, plugin) { | |
const { | |
config, | |
server, | |
settings | |
} = pluginCollection.kbnServer; | |
const transformedSettings = (0, _transform_deprecations.transformDeprecations)(settings); | |
const pluginSettings = (0, _lodash.get)(transformedSettings, plugin.configPrefix); | |
const deprecations = plugin.getDeprecations(); | |
const transformedPluginSettings = (0, _deprecation.createTransform)(deprecations)(pluginSettings, message => { | |
server.log(['warning', plugin.configPrefix, 'config', 'deprecation'], message); | |
}); | |
const configSchema = await plugin.getConfigSchema(); | |
config.extendSchema(configSchema, transformedPluginSettings, plugin.configPrefix); | |
} | |
function disablePluginConfig(pluginCollection, plugin) { | |
// when disabling a plugin's config we remove the existing schema and | |
// replace it with a simple schema/config that only has enabled set to false | |
const { | |
config | |
} = pluginCollection.kbnServer; | |
config.removeSchema(plugin.configPrefix); | |
const schema = _joi.default.object({ | |
enabled: _joi.default.bool() | |
}); | |
config.extendSchema(schema, { | |
enabled: false | |
}, plugin.configPrefix); | |
} | |
module.exports = class Plugins extends _collection.Collection { | |
constructor(kbnServer) { | |
super(); | |
this.kbnServer = kbnServer; | |
this[pluginApis] = new Set(); | |
} | |
async new(path) { | |
const api = new _plugin_api.default(this.kbnServer, path); | |
this[pluginApis].add(api); | |
let pluginEntrypoint; | |
const pluginExport = require(path); | |
if (pluginExport.default) { | |
pluginEntrypoint = pluginExport.default; | |
} else { | |
pluginEntrypoint = pluginExport; | |
} | |
const output = [].concat(pluginEntrypoint(api) || []); | |
if (!output.length) return; // clear the byIdCache | |
this[byIdCache] = null; | |
for (const plugin of output) { | |
if (!plugin instanceof api.Plugin) { | |
throw new TypeError('unexpected plugin export ' + (0, _util.inspect)(plugin)); | |
} | |
await addPluginConfig(this, plugin); | |
this.add(plugin); | |
} | |
} | |
async disable(plugin) { | |
disablePluginConfig(this, plugin); | |
this.delete(plugin); | |
} | |
get byId() { | |
return this[byIdCache] || (this[byIdCache] = (0, _lodash.keyBy)([...this], 'id')); | |
} | |
getPluginApis() { | |
return this[pluginApis]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment