From 638d972acf3a9d5fcc2e3327a7d83e942ecd7edf Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Wed, 4 Mar 2020 11:25:39 +0100 Subject: [PATCH] [APM] Make typescript optimization process compatible with NP (#58984) * [APM] Make typescript optimization process compatible with NP Rather than creating an extra tsconfig.json file in the APM folder, simply change the one in x-pack root, and include APM files from both legacy + NP. * Update dev_docs/typescript.md * Use spread op instead of assign * Use console.error instead of console.log --- .gitignore | 2 - .../legacy/plugins/apm/dev_docs/typescript.md | 2 +- .../plugins/apm/scripts/optimize-tsconfig.js | 5 +- .../apm/scripts/optimize-tsconfig/optimize.js | 54 ++++++++++--------- .../apm/scripts/optimize-tsconfig/paths.js | 4 +- .../scripts/optimize-tsconfig/tsconfig.json | 9 ++-- .../scripts/optimize-tsconfig/unoptimize.js | 21 ++------ .../apm/scripts/unoptimize-tsconfig.js | 5 +- x-pack/plugins/apm/tsconfig.json | 3 -- 9 files changed, 47 insertions(+), 58 deletions(-) delete mode 100644 x-pack/plugins/apm/tsconfig.json diff --git a/.gitignore b/.gitignore index e7391a5c292d0..f435a448406e5 100644 --- a/.gitignore +++ b/.gitignore @@ -45,5 +45,3 @@ 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 index 105c6edabf48f..6858e93ec09e0 100644 --- a/x-pack/legacy/plugins/apm/dev_docs/typescript.md +++ b/x-pack/legacy/plugins/apm/dev_docs/typescript.md @@ -1,6 +1,6 @@ #### 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. +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 modifying `tsconfig.json` in the X-Pack folder to only include APM files, and editing the Kibana configuration to not include any files. The script configures git to ignore any changes in these files, and has an undo script as well. To run the optimization: diff --git a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js index c1f1472dc9024..745f0db45e4fa 100644 --- a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js @@ -6,4 +6,7 @@ const { optimizeTsConfig } = require('./optimize-tsconfig/optimize'); -optimizeTsConfig(); +optimizeTsConfig().catch(err => { + console.error(err); + process.exit(1); +}); diff --git a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js index ef9e393db3eca..3a5809e564691 100644 --- a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js @@ -7,29 +7,26 @@ /* eslint-disable import/no-extraneous-dependencies */ const fs = require('fs'); -const promisify = require('util').promisify; +const { promisify } = require('util'); 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() { +function prepareParentTsConfigs() { return Promise.all( [ - path.resolve(xpackRoot, 'apm.tsconfig.json'), + path.resolve(xpackRoot, 'tsconfig.json'), path.resolve(kibanaRoot, 'tsconfig.json') ].map(async filename => { const config = json5.parse(await readFile(filename, 'utf-8')); @@ -50,32 +47,37 @@ function updateParentTsConfigs() { ); } +async function addApmFilesToXpackTsConfig() { + const template = json5.parse(await readFile(tsconfigTpl, 'utf-8')); + const xpackTsConfig = path.join(xpackRoot, 'tsconfig.json'); + const config = json5.parse(await readFile(xpackTsConfig, 'utf-8')); + + await writeFile( + xpackTsConfig, + JSON.stringify({ ...config, ...template }, 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`' - ); - }); -}; +async function optimizeTsConfig() { + await unoptimizeTsConfig(); + + await prepareParentTsConfigs(); + + await addApmFilesToXpackTsConfig(); + + await setIgnoreChanges(); + // 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 index cdb8e4d878ea3..cab55a2526202 100644 --- a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/paths.js +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/paths.js @@ -5,8 +5,7 @@ */ const path = require('path'); -const apmRoot = path.resolve(__dirname, '../..'); -const xpackRoot = path.resolve(apmRoot, '../../..'); +const xpackRoot = path.resolve(__dirname, '../../../../..'); const kibanaRoot = path.resolve(xpackRoot, '..'); const tsconfigTpl = path.resolve(__dirname, './tsconfig.json'); @@ -17,7 +16,6 @@ const filesToIgnore = [ ]; module.exports = { - apmRoot, xpackRoot, kibanaRoot, tsconfigTpl, diff --git a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/tsconfig.json b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/tsconfig.json index 5021694ff04ac..8f6b0f35e4b52 100644 --- a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/tsconfig.json +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/tsconfig.json @@ -1,12 +1,11 @@ { - "extends": "../../../apm.tsconfig.json", "include": [ - "./**/*", - "../../../plugins/apm/**/*", - "../../../typings/**/*" + "./plugins/apm/**/*", + "./legacy/plugins/apm/**/*", + "./typings/**/*" ], "exclude": [ "**/__fixtures__/**/*", - "./e2e/cypress/**/*" + "./legacy/plugins/apm/e2e/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 index 3fdf2a97363a8..33def8c2579fa 100644 --- a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/unoptimize.js +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/unoptimize.js @@ -5,32 +5,21 @@ */ /* 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'); +const { 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'); - }); + unoptimizeTsConfig: async () => { + await unoptimizeTsConfig(); + // 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 index 5362b6a6d52e2..e33dc502a9587 100644 --- a/x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js +++ b/x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js @@ -6,4 +6,7 @@ const { unoptimizeTsConfig } = require('./optimize-tsconfig/unoptimize'); -unoptimizeTsConfig(); +unoptimizeTsConfig().catch(err => { + console.error(err); + process.exit(1); +}); diff --git a/x-pack/plugins/apm/tsconfig.json b/x-pack/plugins/apm/tsconfig.json deleted file mode 100644 index 618c6c3e97b57..0000000000000 --- a/x-pack/plugins/apm/tsconfig.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "../../../tsconfig.json" -}