Skip to content

Commit

Permalink
back to canvas way of loading plugins (#26463) (#26676)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored Dec 5, 2018
1 parent a0cc018 commit cdc5825
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 216 deletions.
1 change: 0 additions & 1 deletion packages/kbn-interpreter/src/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

export { pathsRegistry } from './lib/paths_registry';
export { functionsRegistry } from './lib/functions_registry';
export { typesRegistry } from './lib/types_registry';
export { createError } from './interpreter/create_error';
Expand Down
65 changes: 0 additions & 65 deletions packages/kbn-interpreter/src/common/lib/paths_registry.js

This file was deleted.

92 changes: 0 additions & 92 deletions packages/kbn-interpreter/src/common/lib/paths_registry.test.js

This file was deleted.

93 changes: 73 additions & 20 deletions packages/kbn-interpreter/src/server/get_plugin_paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,92 @@
* under the License.
*/

import path from 'path';
import fs from 'fs';
import { resolve } from 'path';
import { promisify } from 'util';
import { flatten } from 'lodash';
import { pathsRegistry } from '../common/lib/paths_registry';
import { pluginPaths } from './plugin_paths';

const lstat = promisify(fs.lstat);
const readdir = promisify(fs.readdir);

const canvasPluginDirectoryName = 'canvas_plugin';

const isDirectory = path =>
lstat(path)
.then(stat => stat.isDirectory())
.catch(() => false);

const isDirname = (p, name) => path.basename(p) === name;

const getPackagePluginPath = () => {
let basePluginPath = path.resolve(__dirname, '..');

if (isDirname(basePluginPath, 'target')) {
basePluginPath = path.join(basePluginPath, '..');
}
return basePluginPath;
};

const getKibanaPluginsPath = () => {
let kibanaPath = path.resolve(getPackagePluginPath(), '..', '..');

// in dev mode we are in kibana folder, else we are in node_modules
if (!isDirname(kibanaPath, 'kibana')) {
kibanaPath = path.join(kibanaPath, '..');
}

return path.join(kibanaPath, 'plugins');
};

const getXPackPluginsPath = () => {
const kibanaPath = path.resolve(getPackagePluginPath(), '..', '..');

// in dev mode we are in kibana folder, else we are in node_modules
return path.join(kibanaPath, 'x-pack/plugins');
};

// These must all exist
const paths = [
getPackagePluginPath(),
getXPackPluginsPath(), // Canvas core plugins
getKibanaPluginsPath(), // Kibana plugin directory
].filter(Boolean);

export const getPluginPaths = type => {
const typePaths = pathsRegistry.get(type);
if (!typePaths) {
throw new Error(`Unknown type: ${type}`);
const typePath = pluginPaths[type];
if (!typePath) throw new Error(`Unknown type: ${type}`);

async function findPlugins(directory) {
const isDir = await isDirectory(directory);
if (!isDir) return;

const names = await readdir(directory); // Get names of everything in the directory
return names
.filter(name => name[0] !== '.')
.map(name => path.resolve(directory, name, canvasPluginDirectoryName, ...typePath));
}

return Promise.all(typePaths.map(async path => {
const isDir = await isDirectory(path);
if (!isDir) {
return;
}
// Get the full path of all js files in the directory
return readdir(path).then(files => {
return files.reduce((acc, file) => {
if (file.endsWith('.js')) {
acc.push(resolve(path, file));
}
return acc;
}, []);
}).catch();
})).then(flatten);
return Promise.all(paths.map(findPlugins))
.then(dirs =>
dirs.reduce((list, dir) => {
if (!dir) return list;
return list.concat(dir);
}, [])
)
.then(possibleCanvasPlugins => {
// Check how many are directories. If lstat fails it doesn't exist anyway.
return Promise.all(
// An array
possibleCanvasPlugins.map(pluginPath => isDirectory(pluginPath))
).then(isDirectory => possibleCanvasPlugins.filter((pluginPath, i) => isDirectory[i]));
})
.then(canvasPluginDirectories => {
return Promise.all(
canvasPluginDirectories.map(dir =>
// Get the full path of all files in the directory
readdir(dir).then(files => files.map(file => path.resolve(dir, file)))
)
).then(flatten);
});
};
1 change: 1 addition & 0 deletions packages/kbn-interpreter/src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@

export { populateServerRegistries, getServerRegistries } from './server_registries';
export { getPluginPaths } from './get_plugin_paths';
export { pluginPaths } from './plugin_paths';
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@
* under the License.
*/

const { resolve } = require('path');

exports.pluginPaths = {
commonFunctions: resolve(__dirname, 'target/plugin/functions/common'),
types: resolve(__dirname, 'target/plugin/types'),
export const pluginPaths = {
serverFunctions: ['functions', 'server'],
browserFunctions: ['functions', 'browser'],
commonFunctions: ['functions', 'common'],
types: ['types'],
elements: ['elements'],
renderers: ['renderers'],
interfaces: ['interfaces'],
transformUIs: ['uis', 'transforms'],
datasourceUIs: ['uis', 'datasources'],
modelUIs: ['uis', 'models'],
viewUIs: ['uis', 'views'],
argumentUIs: ['uis', 'arguments'],
};
2 changes: 1 addition & 1 deletion packages/kbn-interpreter/tasks/build/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.SOURCE_DIR = resolve(exports.ROOT_DIR, 'src');
exports.BUILD_DIR = resolve(exports.ROOT_DIR, 'target');

exports.PLUGIN_SOURCE_DIR = resolve(exports.SOURCE_DIR, 'plugin');
exports.PLUGIN_BUILD_DIR = resolve(exports.BUILD_DIR, 'plugin');
exports.PLUGIN_BUILD_DIR = resolve(exports.BUILD_DIR, 'canvas_plugin');

exports.WEBPACK_CONFIG_PATH = require.resolve('./webpack.config');
exports.BABEL_PRESET_PATH = require.resolve('@kbn/babel-preset/webpack_preset');
Expand Down
5 changes: 0 additions & 5 deletions src/legacy/core_plugins/interpreter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import { resolve } from 'path';
import init from './init';
import { pathsRegistry } from '@kbn/interpreter/common';
import { pluginPaths } from '@kbn/interpreter/plugin_paths';

export default function (kibana) {
return new kibana.Plugin({
Expand All @@ -32,9 +30,6 @@ export default function (kibana) {
'plugins/interpreter/load_browser_plugins.js',
],
},
preInit: () => {
pathsRegistry.registerAll(pluginPaths);
},
init,
});
}
Expand Down
7 changes: 6 additions & 1 deletion src/legacy/core_plugins/interpreter/server/routes/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@
* under the License.
*/

import { pluginPaths } from '@kbn/interpreter/server';
import { getPluginStream } from '../lib/get_plugin_stream';

export function plugins(server) {
server.route({
method: 'GET',
path: '/api/canvas/plugins',
handler: function (request) {
handler: function (request, h) {
const { type } = request.query;

if (!pluginPaths[type]) {
return h.response({ error: 'Invalid type' }).code(400);
}

return getPluginStream(type);
},
config: {
Expand Down
5 changes: 0 additions & 5 deletions x-pack/plugins/canvas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
*/

import { resolve } from 'path';
import { pathsRegistry } from '@kbn/interpreter/common';
import init from './init';
import { mappings } from './server/mappings';
import { CANVAS_APP } from './common/lib';
import { pluginPaths } from './plugin_paths';

export function canvas(kibana) {
return new kibana.Plugin({
Expand Down Expand Up @@ -41,9 +39,6 @@ export function canvas(kibana) {
}).default();
},

preInit: () => {
pathsRegistry.registerAll(pluginPaths);
},
init,
});
}
Loading

0 comments on commit cdc5825

Please sign in to comment.