Skip to content

Commit

Permalink
[SIEM] Add developer optimization scripts for their environments (#61375
Browse files Browse the repository at this point in the history
)

## Summary

This adds an optimization script very copied and slightly modified from:
* #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.
  • Loading branch information
FrankHassanabad authored Mar 26, 2020
1 parent 09d6667 commit 0f421ad
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 0 deletions.
13 changes: 13 additions & 0 deletions x-pack/legacy/plugins/siem/scripts/optimize_tsconfig.js
Original file line number Diff line number Diff line change
@@ -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);
});
16 changes: 16 additions & 0 deletions x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/README.md
Original file line number Diff line number Diff line change
@@ -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`

76 changes: 76 additions & 0 deletions x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/optimize.js
Original file line number Diff line number Diff line change
@@ -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,
};
23 changes: 23 additions & 0 deletions x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/paths.js
Original file line number Diff line number Diff line change
@@ -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,
};
15 changes: 15 additions & 0 deletions x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
25 changes: 25 additions & 0 deletions x-pack/legacy/plugins/siem/scripts/optimize_tsconfig/unoptimize.js
Original file line number Diff line number Diff line change
@@ -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');
},
};
13 changes: 13 additions & 0 deletions x-pack/legacy/plugins/siem/scripts/unoptimize_tsconfig.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 0f421ad

Please sign in to comment.