Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Prototype] Add plugin install hook #7613

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cli_plugin/install/kibana.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export async function rebuildCache(settings, logger) {
},
plugins: {
initialize: false,
install: true,
scanDirs: [settings.pluginDir, fromRoot('src/plugins')]
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/plugins/i18n/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import _ from 'lodash';

export default function ({ Plugin }) {
return new Plugin({

install(installContext) {
_.set(installContext, 'i18n.registerTranslations', function (dir) {
console.log(`Translation registered at ${dir}`);
});
}

});
};
4 changes: 4 additions & 0 deletions src/plugins/i18n/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "i18n",
"version": "1.0.0"
}
3 changes: 2 additions & 1 deletion src/server/config/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ module.exports = () => Joi.object({
plugins: Joi.object({
paths: Joi.array().items(Joi.string()).default([]),
scanDirs: Joi.array().items(Joi.string()).default([]),
initialize: Joi.boolean().default(true)
initialize: Joi.boolean().default(true),
install: Joi.boolean().default(false)
}).default(),

path: Joi.object({
Expand Down
3 changes: 3 additions & 0 deletions src/server/kbn_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ module.exports = class KbnServer {
// tell the config we are done loading plugins
require('./config/complete'),

// run plugin install hooks
require('./plugins/install'),

// setup this.uiExports and this.bundles
require('../ui'),

Expand Down
42 changes: 42 additions & 0 deletions src/server/plugins/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { includes } from 'lodash';

export default async function (kbnServer, server, config) {

if (!config.get('plugins.install')) {
server.log(['info'], 'Plugin installation disabled.');
return [];
}

const installContext = {};
const { plugins } = kbnServer;


const path = [];

const initialize = async function (id) {
const plugin = plugins.byId[id];

if (includes(path, id)) {
throw new Error(`circular dependencies found: "${path.concat(id).join(' -> ')}"`);
}

path.push(id);

for (let reqId of plugin.requiredIds) {
if (!plugins.byId[reqId]) {
throw new Error(`Unmet requirement "${reqId}" for plugin "${id}"`);
}

await initialize(reqId);
}

await plugin.install(installContext);
path.pop();
};

const collection = plugins.toArray();

for (let { id } of collection) {
await initialize(id);
}
};
5 changes: 5 additions & 0 deletions src/server/plugins/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module.exports = class Plugin {
this.version = opts.version || pkg.version;
this.externalPreInit = opts.preInit || _.noop;
this.externalInit = opts.init || _.noop;
this.externalInstall = opts.install || _.noop;
this.configPrefix = opts.configPrefix || this.id;
this.getConfigSchema = opts.config || _.noop;
this.preInit = _.once(this.preInit);
Expand Down Expand Up @@ -157,6 +158,10 @@ module.exports = class Plugin {
this[extendInitFns].push(fn);
}

async install(installContext) {
return await this.externalInstall(installContext);
}

toJSON() {
return this.pkg;
}
Expand Down