From 15758c7da2e0efcfd07ad7bd73c7b0b8f9cd650b Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Mon, 11 Nov 2019 15:56:16 +0100 Subject: [PATCH] =?UTF-8?q?[7.x]=20[APM]=20Script=20optimization=20of=20AP?= =?UTF-8?q?M-specific=20tsconfig=20(#498=E2=80=A6=20(#50151)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [APM] Script optimization of APM-specific tsconfig * Don't break bootstrap process * Document TS optimizations --- .gitignore | 2 + .../legacy/plugins/apm/dev_docs/typescript.md | 11 +++ .../plugins/apm/scripts/optimize-tsconfig.js | 9 ++ .../apm/scripts/optimize-tsconfig/optimize.js | 82 +++++++++++++++++++ .../apm/scripts/optimize-tsconfig/paths.js | 25 ++++++ .../scripts/optimize-tsconfig/tsconfig.json | 11 +++ .../scripts/optimize-tsconfig/unoptimize.js | 36 ++++++++ .../apm/scripts/unoptimize-tsconfig.js | 9 ++ .../apm/typings/{common.ts => common.d.ts} | 7 ++ 9 files changed, 192 insertions(+) create mode 100644 x-pack/legacy/plugins/apm/dev_docs/typescript.md create mode 100644 x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js create mode 100644 x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js create mode 100644 x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/paths.js create mode 100644 x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/tsconfig.json create mode 100644 x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/unoptimize.js create mode 100644 x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js rename x-pack/legacy/plugins/apm/typings/{common.ts => common.d.ts} (77%) diff --git a/.gitignore b/.gitignore index efb5c5777463..02b20da297fc 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,5 @@ package-lock.json *.sublime-* npm-debug.log* .tern-project +x-pack/legacy/plugins/apm/tsconfig.json +apm.tsconfig.json diff --git a/x-pack/legacy/plugins/apm/dev_docs/typescript.md b/x-pack/legacy/plugins/apm/dev_docs/typescript.md new file mode 100644 index 000000000000..105c6edabf48 --- /dev/null +++ b/x-pack/legacy/plugins/apm/dev_docs/typescript.md @@ -0,0 +1,11 @@ +#### Optimizing TypeScript + +Kibana and X-Pack are very large TypeScript projects, and it comes at a cost. Editor responsiveness is not great, and the CLI type check for X-Pack takes about a minute. To get faster feedback, we create a smaller APM TypeScript project that only type checks the APM project and the files it uses. This optimization consists of creating a `tsconfig.json` in APM that includes the Kibana/X-Pack typings, and editing the Kibana/X-Pack configurations to not include any files, or removing the configurations altogether. The script configures git to ignore any changes in these files, and has an undo script as well. + +To run the optimization: + +`$ node x-pack/legacy/plugins/apm/scripts/optimize-tsconfig` + +To undo the optimization: + +`$ node x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig` diff --git a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js new file mode 100644 index 000000000000..c1f1472dc902 --- /dev/null +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js @@ -0,0 +1,9 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +const { optimizeTsConfig } = require('./optimize-tsconfig/optimize'); + +optimizeTsConfig(); diff --git a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js new file mode 100644 index 000000000000..ef9e393db3ec --- /dev/null +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js @@ -0,0 +1,82 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +/* eslint-disable import/no-extraneous-dependencies */ + +const fs = require('fs'); +const promisify = require('util').promisify; +const path = require('path'); +const json5 = require('json5'); +const execa = require('execa'); + +const copyFile = promisify(fs.copyFile); +const rename = promisify(fs.rename); +const readFile = promisify(fs.readFile); +const writeFile = promisify(fs.writeFile); + +const { + xpackRoot, + kibanaRoot, + apmRoot, + tsconfigTpl, + filesToIgnore +} = require('./paths'); +const { unoptimizeTsConfig } = require('./unoptimize'); + +function updateParentTsConfigs() { + return Promise.all( + [ + path.resolve(xpackRoot, 'apm.tsconfig.json'), + path.resolve(kibanaRoot, 'tsconfig.json') + ].map(async filename => { + const config = json5.parse(await readFile(filename, 'utf-8')); + + await writeFile( + filename, + JSON.stringify( + { + ...config, + include: [] + }, + null, + 2 + ), + { encoding: 'utf-8' } + ); + }) + ); +} + +async function setIgnoreChanges() { + for (const filename of filesToIgnore) { + await execa('git', ['update-index', '--skip-worktree', filename]); + } +} + +const optimizeTsConfig = () => { + return unoptimizeTsConfig() + .then(() => + Promise.all([ + copyFile(tsconfigTpl, path.resolve(apmRoot, './tsconfig.json')), + rename( + path.resolve(xpackRoot, 'tsconfig.json'), + path.resolve(xpackRoot, 'apm.tsconfig.json') + ) + ]) + ) + .then(() => updateParentTsConfigs()) + .then(() => setIgnoreChanges()) + .then(() => { + // eslint-disable-next-line no-console + console.log( + 'Created an optimized tsconfig.json for APM. To undo these changes, run `./scripts/unoptimize-tsconfig.js`' + ); + }); +}; + +module.exports = { + optimizeTsConfig +}; diff --git a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/paths.js b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/paths.js new file mode 100644 index 000000000000..cdb8e4d878ea --- /dev/null +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/paths.js @@ -0,0 +1,25 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +const path = require('path'); + +const apmRoot = path.resolve(__dirname, '../..'); +const xpackRoot = path.resolve(apmRoot, '../../..'); +const kibanaRoot = path.resolve(xpackRoot, '..'); + +const tsconfigTpl = path.resolve(__dirname, './tsconfig.json'); + +const filesToIgnore = [ + path.resolve(xpackRoot, 'tsconfig.json'), + path.resolve(kibanaRoot, 'tsconfig.json') +]; + +module.exports = { + apmRoot, + xpackRoot, + kibanaRoot, + tsconfigTpl, + filesToIgnore +}; diff --git a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/tsconfig.json b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/tsconfig.json new file mode 100644 index 000000000000..e7d9abea65a3 --- /dev/null +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../../apm.tsconfig.json", + "include": [ + "./**/*", + "../../../typings/**/*" + ], + "exclude": [ + "**/__fixtures__/**/*", + "./cypress/**/*" + ] +} diff --git a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/unoptimize.js b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/unoptimize.js new file mode 100644 index 000000000000..3fdf2a97363a --- /dev/null +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/unoptimize.js @@ -0,0 +1,36 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +/* eslint-disable import/no-extraneous-dependencies */ + +const path = require('path'); +const execa = require('execa'); +const fs = require('fs'); +const promisify = require('util').promisify; +const removeFile = promisify(fs.unlink); +const exists = promisify(fs.exists); + +const { apmRoot, filesToIgnore } = require('./paths'); + +async function unoptimizeTsConfig() { + for (const filename of filesToIgnore) { + await execa('git', ['update-index', '--no-skip-worktree', filename]); + await execa('git', ['checkout', filename]); + } + + const apmTsConfig = path.join(apmRoot, 'tsconfig.json'); + if (await exists(apmTsConfig)) { + await removeFile(apmTsConfig); + } +} + +module.exports = { + unoptimizeTsConfig: () => { + return unoptimizeTsConfig().then(() => { + // eslint-disable-next-line no-console + console.log('Removed APM TypeScript optimizations'); + }); + } +}; diff --git a/x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js b/x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js new file mode 100644 index 000000000000..5362b6a6d52e --- /dev/null +++ b/x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js @@ -0,0 +1,9 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +const { unoptimizeTsConfig } = require('./optimize-tsconfig/unoptimize'); + +unoptimizeTsConfig(); diff --git a/x-pack/legacy/plugins/apm/typings/common.ts b/x-pack/legacy/plugins/apm/typings/common.d.ts similarity index 77% rename from x-pack/legacy/plugins/apm/typings/common.ts rename to x-pack/legacy/plugins/apm/typings/common.d.ts index 2fafceb32209..d79b05ed99b4 100644 --- a/x-pack/legacy/plugins/apm/typings/common.ts +++ b/x-pack/legacy/plugins/apm/typings/common.d.ts @@ -4,6 +4,13 @@ * you may not use this file except in compliance with the Elastic License. */ +import '../../infra/types/rison_node'; +import '../../infra/types/eui'; +// EUIBasicTable +import {} from '../../reporting/public/components/report_listing'; +// .svg +import '../../canvas/types/webpack'; + // Allow unknown properties in an object export type AllowUnknownProperties = T extends Array ? Array>