From fedd7facf15fe7b88ef94137453ab48217f96558 Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Sat, 29 Feb 2020 21:55:08 +0100 Subject: [PATCH 1/4] [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. --- .gitignore | 2 - .../plugins/apm/scripts/optimize-tsconfig.js | 6 +- .../apm/scripts/optimize-tsconfig/optimize.js | 55 ++++++++++--------- .../apm/scripts/optimize-tsconfig/paths.js | 4 +- .../scripts/optimize-tsconfig/tsconfig.json | 9 ++- .../scripts/optimize-tsconfig/unoptimize.js | 21 ++----- .../apm/scripts/unoptimize-tsconfig.js | 6 +- x-pack/plugins/apm/tsconfig.json | 3 - 8 files changed, 49 insertions(+), 57 deletions(-) delete mode 100644 x-pack/plugins/apm/tsconfig.json diff --git a/.gitignore b/.gitignore index 02b20da297fc6..efb5c57774633 100644 --- a/.gitignore +++ b/.gitignore @@ -44,5 +44,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/scripts/optimize-tsconfig.js b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js index c1f1472dc9024..fec49336e2635 100644 --- a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js @@ -6,4 +6,8 @@ const { optimizeTsConfig } = require('./optimize-tsconfig/optimize'); -optimizeTsConfig(); +optimizeTsConfig().catch(err => { + // eslint-disable-next-line no-console + console.log(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..83147bce0d6bc 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,27 @@ /* 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 { assign } = require('lodash'); -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 +48,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(assign(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..8d1ef68f1daa6 100644 --- a/x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js +++ b/x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js @@ -6,4 +6,8 @@ const { unoptimizeTsConfig } = require('./optimize-tsconfig/unoptimize'); -unoptimizeTsConfig(); +unoptimizeTsConfig().catch(err => { + // eslint-disable-next-line no-console + console.log(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" -} From 2f572cc324bfa3a395cc1c979228cbfd0de23234 Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Mon, 2 Mar 2020 12:48:45 +0100 Subject: [PATCH 2/4] Update dev_docs/typescript.md --- x-pack/legacy/plugins/apm/dev_docs/typescript.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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: From b0b181d2fec582aa8e9f9e31b43900199076a053 Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Mon, 2 Mar 2020 13:31:25 +0100 Subject: [PATCH 3/4] Use spread op instead of assign --- .../legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 83147bce0d6bc..3a5809e564691 100644 --- a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js @@ -11,7 +11,6 @@ const { promisify } = require('util'); const path = require('path'); const json5 = require('json5'); const execa = require('execa'); -const { assign } = require('lodash'); const readFile = promisify(fs.readFile); const writeFile = promisify(fs.writeFile); @@ -55,7 +54,7 @@ async function addApmFilesToXpackTsConfig() { await writeFile( xpackTsConfig, - JSON.stringify(assign(config, template), null, 2), + JSON.stringify({ ...config, ...template }, null, 2), { encoding: 'utf-8' } ); } From 6cc043cc9e5c83b3322e92c91699928be1e8ed19 Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Tue, 3 Mar 2020 14:55:41 +0100 Subject: [PATCH 4/4] Use console.error instead of console.log --- x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js | 3 +-- x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js index fec49336e2635..745f0db45e4fa 100644 --- a/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js +++ b/x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js @@ -7,7 +7,6 @@ const { optimizeTsConfig } = require('./optimize-tsconfig/optimize'); optimizeTsConfig().catch(err => { - // eslint-disable-next-line no-console - console.log(err); + console.error(err); process.exit(1); }); diff --git a/x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js b/x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js index 8d1ef68f1daa6..e33dc502a9587 100644 --- a/x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js +++ b/x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js @@ -7,7 +7,6 @@ const { unoptimizeTsConfig } = require('./optimize-tsconfig/unoptimize'); unoptimizeTsConfig().catch(err => { - // eslint-disable-next-line no-console - console.log(err); + console.error(err); process.exit(1); });