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 0000000000000..ff4002b076e5d --- /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 0000000000000..d3615d2870ef9 --- /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 0000000000000..9c04e0b69b738 --- /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 0000000000000..ca26203e17d2e --- /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 0000000000000..bec6988bdebd9 --- /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 0000000000000..21fc1aa2a1a33 --- /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 0000000000000..d5392c76d9f8f --- /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); +});