From 8df76c8bb72f10793afdd6e8e3c3dcd71d3d25ff Mon Sep 17 00:00:00 2001 From: Frank Hassanabad Date: Wed, 25 Mar 2020 16:30:53 -0600 Subject: [PATCH] [SIEM] Add developer optimization scripts for their environments ## Summary This adds an optimization script very copied and slightly modified from: * https://github.com/elastic/kibana/pull/49868 Usage: Run this to do an dev tsconfig optimization: ```ts node x-pack/legacy/plugins/siem/scripts/optimize_tsconfig ``` Run this to undo the optimization: ```ts node x-pack/legacy/plugins/siem/scripts/unoptimize_tsconfig ``` Testing and what this does: Run this: ```ts node x-pack/legacy/plugins/siem/scripts/optimize_tsconfig ``` Then run your start-test-all or at least your linter, typescript check, and jest tests to make sure they all operate as expected. Restart your IDE and ensure everything works as expected. Run `git status` and ensure it looks like no new files want to be checked in. Open up your: ```ts kibana/x-pack/tsconfig.json ``` And notice it is now changed when optimization has run to use a smaller set of includes. Open up your: ```ts kibana/tsconfig.json ``` And notice it is now changed when optimization is run to use a smaller set of includes. --- .../plugins/siem/scripts/optimize_tsconfig.js | 13 ++++ .../siem/scripts/optimize_tsconfig/README.md | 16 ++++ .../scripts/optimize_tsconfig/optimize.js | 76 +++++++++++++++++++ .../siem/scripts/optimize_tsconfig/paths.js | 23 ++++++ .../scripts/optimize_tsconfig/tsconfig.json | 15 ++++ .../scripts/optimize_tsconfig/unoptimize.js | 25 ++++++ .../siem/scripts/unoptimize_tsconfig.js | 13 ++++ 7 files changed, 181 insertions(+) create mode 100644 x-pack/legacy/plugins/siem/scripts/optimize_tsconfig.js create mode 100644 x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/README.md create mode 100644 x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/optimize.js create mode 100644 x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/paths.js create mode 100644 x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/tsconfig.json create mode 100644 x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/unoptimize.js create mode 100644 x-pack/legacy/plugins/siem/scripts/unoptimize_tsconfig.js diff --git a/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig.js b/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig.js new file mode 100644 index 000000000000..ff4002b076e5 --- /dev/null +++ b/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig.js @@ -0,0 +1,13 @@ +/* + * 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().catch(err => { + console.error(err); + // eslint-disable-next-line no-process-exit + process.exit(1); +}); diff --git a/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/README.md b/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/README.md new file mode 100644 index 000000000000..d3615d2870ef --- /dev/null +++ b/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/README.md @@ -0,0 +1,16 @@ +Hard forked from here: +x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js + + +#### 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 SIEM TypeScript project that only type checks the SIEM project and the files it uses. This optimization consists of creating a `tsconfig.json` in SIEM 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/siem/scripts/optimize_tsconfig` + +To undo the optimization: + +`$ node x-pack/legacy/plugins/siem/scripts/unoptimize_tsconfig` + diff --git a/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/optimize.js b/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/optimize.js new file mode 100644 index 000000000000..9c04e0b69b73 --- /dev/null +++ b/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/optimize.js @@ -0,0 +1,76 @@ +/* + * 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'); +const path = require('path'); +const json5 = require('json5'); +const execa = require('execa'); + +const readFile = promisify(fs.readFile); +const writeFile = promisify(fs.writeFile); + +const { xpackRoot, kibanaRoot, tsconfigTpl, filesToIgnore } = require('./paths'); +const { unoptimizeTsConfig } = require('./unoptimize'); + +function prepareParentTsConfigs() { + return Promise.all( + [path.resolve(xpackRoot, '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 addFilesToXpackTsConfig() { + 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]); + } +} + +async function optimizeTsConfig() { + await unoptimizeTsConfig(); + + await prepareParentTsConfigs(); + + await addFilesToXpackTsConfig(); + + await setIgnoreChanges(); + // eslint-disable-next-line no-console + console.log( + 'Created an optimized tsconfig.json for SIEM. To undo these changes, run `./scripts/unoptimize_tsconfig.js`' + ); +} + +module.exports = { + optimizeTsConfig, +}; diff --git a/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/paths.js b/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/paths.js new file mode 100644 index 000000000000..ca26203e17d2 --- /dev/null +++ b/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/paths.js @@ -0,0 +1,23 @@ +/* + * 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 xpackRoot = path.resolve(__dirname, '../../../../..'); +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 = { + xpackRoot, + kibanaRoot, + tsconfigTpl, + filesToIgnore, +}; diff --git a/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/tsconfig.json b/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/tsconfig.json new file mode 100644 index 000000000000..bec6988bdebd --- /dev/null +++ b/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/tsconfig.json @@ -0,0 +1,15 @@ +{ + "include": [ + "typings/**/*", + "plugins/siem/**/*", + "legacy/plugins/siem/**/*", + "plugins/apm/typings/numeral.d.ts", + "legacy/plugins/canvas/types/webpack.d.ts" + ], + "exclude": [ + "test/**/*", + "**/__fixtures__/**/*", + "legacy/plugins/siem/cypress/**/*", + "**/typespec_tests.ts" + ] +} diff --git a/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/unoptimize.js b/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/unoptimize.js new file mode 100644 index 000000000000..21fc1aa2a1a3 --- /dev/null +++ b/x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/unoptimize.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. + */ +/* eslint-disable import/no-extraneous-dependencies */ + +const execa = require('execa'); + +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]); + } +} + +module.exports = { + unoptimizeTsConfig: async () => { + await unoptimizeTsConfig(); + // eslint-disable-next-line no-console + console.log('Removed SIEM TypeScript optimizations'); + }, +}; diff --git a/x-pack/legacy/plugins/siem/scripts/unoptimize_tsconfig.js b/x-pack/legacy/plugins/siem/scripts/unoptimize_tsconfig.js new file mode 100644 index 000000000000..d5392c76d9f8 --- /dev/null +++ b/x-pack/legacy/plugins/siem/scripts/unoptimize_tsconfig.js @@ -0,0 +1,13 @@ +/* + * 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().catch(err => { + console.error(err); + // eslint-disable-next-line no-process-exit + process.exit(1); +});