Skip to content

Commit

Permalink
Add --plugin-path support for new platform plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Mar 27, 2019
1 parent 57c7572 commit 42fc671
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/cli/serve/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export default function (program) {
repl: !!opts.repl,
basePath: !!opts.basePath,
optimize: !!opts.optimize,
pluginPath: opts.pluginPath,
},
features: {
isClusterModeSupported: CAN_CLUSTER,
Expand Down
10 changes: 9 additions & 1 deletion src/core/server/config/__mocks__/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@

import { EnvOptions } from '../env';

type Optional<T> = T | undefined;

// Prettier changes this syntax to be invalid.
// tslint:disable prettier
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Array<infer R> ? Array<DeepPartial<R>> : DeepPartial<T[P]>
[P in keyof T]?:
T[P] extends Optional<Array<infer R>> ? Optional<R[]> :
T[P] extends Array<infer U> ? Array<DeepPartial<U>> : DeepPartial<T[P]>
};
// tslint:enable prettier

export function getEnvOptions(options: DeepPartial<EnvOptions> = {}): EnvOptions {
return {
Expand All @@ -37,6 +44,7 @@ export function getEnvOptions(options: DeepPartial<EnvOptions> = {}): EnvOptions
repl: false,
basePath: false,
optimize: false,
pluginPath: undefined,
...(options.cliArgs || {}),
},
isDevClusterMaster:
Expand Down
6 changes: 6 additions & 0 deletions src/core/server/config/__snapshots__/env.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Env {
"envName": "development",
"open": false,
"optimize": false,
"pluginPath": undefined,
"quiet": false,
"repl": false,
"silent": false,
Expand Down Expand Up @@ -50,6 +51,7 @@ Env {
"envName": "production",
"open": false,
"optimize": false,
"pluginPath": undefined,
"quiet": false,
"repl": false,
"silent": false,
Expand Down Expand Up @@ -90,6 +92,7 @@ Env {
"dev": true,
"open": false,
"optimize": false,
"pluginPath": undefined,
"quiet": false,
"repl": false,
"silent": false,
Expand Down Expand Up @@ -130,6 +133,7 @@ Env {
"dev": false,
"open": false,
"optimize": false,
"pluginPath": undefined,
"quiet": false,
"repl": false,
"silent": false,
Expand Down Expand Up @@ -170,6 +174,7 @@ Env {
"dev": false,
"open": false,
"optimize": false,
"pluginPath": undefined,
"quiet": false,
"repl": false,
"silent": false,
Expand Down Expand Up @@ -210,6 +215,7 @@ Env {
"dev": false,
"open": false,
"optimize": false,
"pluginPath": undefined,
"quiet": false,
"repl": false,
"silent": false,
Expand Down
1 change: 1 addition & 0 deletions src/core/server/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface CliArgs {
basePath: boolean;
optimize: boolean;
open: boolean;
pluginPath?: string[];
}

export class Env {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Array [
"dev": true,
"open": false,
"optimize": false,
"pluginPath": undefined,
"quiet": true,
"repl": false,
"silent": false,
Expand Down Expand Up @@ -65,6 +66,7 @@ Array [
"dev": true,
"open": false,
"optimize": false,
"pluginPath": undefined,
"quiet": false,
"repl": false,
"silent": true,
Expand Down
10 changes: 8 additions & 2 deletions src/core/server/plugins/discovery/plugin_discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const TEST_PLUGIN_SEARCH_PATHS = {
emptyPlugins: resolve(process.cwd(), 'plugins'),
nonExistentKibanaExtra: resolve(process.cwd(), '..', 'kibana-extra'),
};
const TEST_EXTRA_PLUGIN_PATH = resolve(process.cwd(), 'my-extra-plugin');

const logger = loggingServiceMock.create();
beforeEach(() => {
Expand Down Expand Up @@ -122,7 +123,11 @@ test('properly iterates through plugin search locations', async () => {
},
};

const env = Env.createDefault(getEnvOptions());
const env = Env.createDefault(
getEnvOptions({
cliArgs: { envName: 'development', pluginPath: [TEST_EXTRA_PLUGIN_PATH] },
})
);
const configService = new ConfigService(
new BehaviorSubject<Config>(new ObjectToConfigAdapter({})),
env,
Expand All @@ -136,12 +141,13 @@ test('properly iterates through plugin search locations', async () => {
const { plugin$, error$ } = discover(pluginsConfig, { configService, env, logger });

const plugins = await plugin$.pipe(toArray()).toPromise();
expect(plugins).toHaveLength(3);
expect(plugins).toHaveLength(4);

for (const path of [
resolve(TEST_PLUGIN_SEARCH_PATHS.nonEmptySrcPlugins, '1'),
resolve(TEST_PLUGIN_SEARCH_PATHS.nonEmptySrcPlugins, '3'),
resolve(TEST_PLUGIN_SEARCH_PATHS.nonEmptySrcPlugins, '6'),
TEST_EXTRA_PLUGIN_PATH,
]) {
const discoveredPlugin = plugins.find(plugin => plugin.path === path)!;
expect(discoveredPlugin).toBeInstanceOf(Plugin);
Expand Down
7 changes: 5 additions & 2 deletions src/core/server/plugins/discovery/plugins_discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { readdir, stat } from 'fs';
import { resolve } from 'path';
import { bindNodeCallback, from } from 'rxjs';
import { bindNodeCallback, from, merge } from 'rxjs';
import { catchError, filter, map, mergeMap, shareReplay } from 'rxjs/operators';
import { CoreContext } from '../../core_context';
import { Logger } from '../../logging';
Expand All @@ -46,7 +46,10 @@ export function discover(config: PluginsConfig, coreContext: CoreContext) {
const log = coreContext.logger.get('plugins-discovery');
log.debug('Discovering plugins...');

const discoveryResults$ = processPluginSearchPaths$(config.pluginSearchPaths, log).pipe(
const discoveryResults$ = merge(
from(config.knownPluginPaths),
processPluginSearchPaths$(config.pluginSearchPaths, log)
).pipe(
mergeMap(pluginPathOrError => {
return typeof pluginPathOrError === 'string'
? createPlugin$(pluginPathOrError, log, coreContext)
Expand Down
8 changes: 8 additions & 0 deletions src/core/server/plugins/plugins_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ export class PluginsConfig {
*/
public readonly pluginSearchPaths: ReadonlyArray<string>;

/**
* Defines directories where a known plugin exists.
*/
public readonly knownPluginPaths: ReadonlyArray<string>;

constructor(config: PluginsConfigType, env: Env) {
this.initialize = config.initialize;
this.pluginSearchPaths = env.pluginSearchPaths;
// Only allow custom pluginPaths in dev.
this.knownPluginPaths =
env.cliArgs.envName === 'development' ? env.cliArgs.pluginPath || [] : [];
}
}
1 change: 1 addition & 0 deletions src/core/server/plugins/plugins_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ test('`setup` properly invokes `discover` and ignores non-critical errors.', asy
expect(mockDiscover).toHaveBeenCalledWith(
{
initialize: true,
knownPluginPaths: [],
pluginSearchPaths: [
resolve(process.cwd(), 'src', 'plugins'),
resolve(process.cwd(), 'plugins'),
Expand Down

0 comments on commit 42fc671

Please sign in to comment.