Skip to content

Commit

Permalink
Establish pattern for typing legacy plugins (elastic#26045)
Browse files Browse the repository at this point in the history
* Add legacy types and export them for plugins
* Add support for core_plugins to import from 'kibana'
  • Loading branch information
joshdover authored Dec 17, 2018
1 parent 53710d8 commit 08fd427
Show file tree
Hide file tree
Showing 13 changed files with 548 additions and 53 deletions.
48 changes: 48 additions & 0 deletions kibana.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* All exports from TS source files (where the implementation is actually done in TS).
*/
export * from './target/types/type_exports';
/**
* All exports from TS ambient definitions (where types are added for JS source in a .d.ts file).
*/
import * as LegacyElasticsearch from './src/legacy/core_plugins/elasticsearch';
import * as LegacyKibanaServer from './src/server/kbn_server';

/**
* Re-export legacy types under a namespace.
*/
// tslint:disable:no-namespace
export namespace Legacy {
export type KibanaConfig = LegacyKibanaServer.KibanaConfig;
export type Request = LegacyKibanaServer.Request;
export type ResponseToolkit = LegacyKibanaServer.ResponseToolkit;
export type Server = LegacyKibanaServer.Server;

export namespace Plugins {
export namespace elasticsearch {
export type Plugin = LegacyElasticsearch.ElasticsearchPlugin;
export type Cluster = LegacyElasticsearch.Cluster;
export type ClusterConfig = LegacyElasticsearch.ClusterConfig;
export type CallClusterOptions = LegacyElasticsearch.CallClusterOptions;
}
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"private": true,
"version": "7.0.0",
"branch": "master",
"types": "./target/types/type_exports.d.ts",
"types": "./kibana.d.ts",
"build": {
"number": 8467,
"sha": "6cb7fec4e154faa0a4a3fee4b33dfef91b9870d9"
Expand Down Expand Up @@ -264,7 +264,7 @@
"@types/dedent": "^0.7.0",
"@types/del": "^3.0.1",
"@types/delete-empty": "^2.0.0",
"@types/elasticsearch": "^5.0.26",
"@types/elasticsearch": "^5.0.30",
"@types/enzyme": "^3.1.12",
"@types/eslint": "^4.16.2",
"@types/execa": "^0.9.0",
Expand Down
10 changes: 5 additions & 5 deletions src/core/server/legacy_compat/legacy_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import { first } from 'rxjs/operators';
import { LegacyService } from '.';
// @ts-ignore: implicit any for JS file
import MockClusterManager from '../../../cli/cluster/cluster_manager';
// @ts-ignore: implicit any for JS file
import MockKbnServer from '../../../server/kbn_server';
import KbnServer from '../../../server/kbn_server';
import { Config, ConfigService, Env, ObjectToConfigAdapter } from '../config';
import { getEnvOptions } from '../config/__mocks__/env';
import { logger } from '../logging/__mocks__';
import { PluginsServiceStartContract } from '../plugins/plugins_service';
import { LegacyPlatformProxy } from './legacy_platform_proxy';

const MockKbnServer: jest.Mock<KbnServer> = KbnServer as any;
const MockLegacyPlatformProxy: jest.Mock<LegacyPlatformProxy> = LegacyPlatformProxy as any;

let legacyService: LegacyService;
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('once LegacyService is started with connection info', () => {
test('reconfigures logging configuration if new config is received.', async () => {
await legacyService.start(startDeps);

const [mockKbnServer] = MockKbnServer.mock.instances;
const [mockKbnServer] = MockKbnServer.mock.instances as Array<jest.Mocked<KbnServer>>;
expect(mockKbnServer.applyLoggingConfiguration).not.toHaveBeenCalled();

config$.next(new ObjectToConfigAdapter({ logging: { verbose: true } }));
Expand All @@ -224,7 +224,7 @@ describe('once LegacyService is started with connection info', () => {
test('logs error if re-configuring fails.', async () => {
await legacyService.start(startDeps);

const [mockKbnServer] = MockKbnServer.mock.instances;
const [mockKbnServer] = MockKbnServer.mock.instances as Array<jest.Mocked<KbnServer>>;
expect(mockKbnServer.applyLoggingConfiguration).not.toHaveBeenCalled();
expect(logger.mockCollect().error).toEqual([]);

Expand Down Expand Up @@ -292,7 +292,7 @@ describe('once LegacyService is started without connection info', () => {
});

test('reconfigures logging configuration if new config is received.', async () => {
const [mockKbnServer] = MockKbnServer.mock.instances;
const [mockKbnServer] = MockKbnServer.mock.instances as Array<jest.Mocked<KbnServer>>;
expect(mockKbnServer.applyLoggingConfiguration).not.toHaveBeenCalled();

config$.next(new ObjectToConfigAdapter({ logging: { verbose: true } }));
Expand Down
1 change: 1 addition & 0 deletions src/dev/build/tasks/copy_source_task.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const CopySourceTask = {
'webpackShims/**',
'config/kibana.yml',
'tsconfig*.json',
'kibana.d.ts'
],
});
},
Expand Down
14 changes: 13 additions & 1 deletion src/dev/build/tasks/transpile_typescript_task.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export const TranspileTypescriptTask = {
description: 'Transpiling sources with typescript compiler',

async run(config, log, build) {
// the types project is built inside the repo so x-pack can use it for it's in-repo build.
const typesProjectRepo = new Project(config.resolveFromRepo('tsconfig.types.json'));
const typesProjectBuild = new Project(build.resolvePath('tsconfig.types.json'));

// these projects are built in the build folder
const defaultProject = new Project(build.resolvePath('tsconfig.json'));
const browserProject = new Project(build.resolvePath('tsconfig.browser.json'));

Expand All @@ -46,8 +51,15 @@ export const TranspileTypescriptTask = {
]
}));

const projects = [
typesProjectRepo.tsConfigPath,
typesProjectBuild.tsConfigPath,
defaultProject.tsConfigPath,
browserProject.tsConfigPath
];

// compile each typescript config file
for (const tsConfigPath of [defaultProject.tsConfigPath, browserProject.tsConfigPath]) {
for (const tsConfigPath of projects) {
log.info(`Compiling`, tsConfigPath, 'project');
await exec(
log,
Expand Down
Loading

0 comments on commit 08fd427

Please sign in to comment.