-
Notifications
You must be signed in to change notification settings - Fork 884
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.x] [APM] Script optimization of APM-specific tsconfig (#498… (#50151)
* [APM] Script optimization of APM-specific tsconfig * Don't break bootstrap process * Document TS optimizations
- Loading branch information
1 parent
5fa5ec5
commit 15758c7
Showing
9 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#### 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. | ||
|
||
To run the optimization: | ||
|
||
`$ node x-pack/legacy/plugins/apm/scripts/optimize-tsconfig` | ||
|
||
To undo the optimization: | ||
|
||
`$ node x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* 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(); |
82 changes: 82 additions & 0 deletions
82
x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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').promisify; | ||
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() { | ||
return Promise.all( | ||
[ | ||
path.resolve(xpackRoot, 'apm.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 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`' | ||
); | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
optimizeTsConfig | ||
}; |
25 changes: 25 additions & 0 deletions
25
x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/paths.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
const path = require('path'); | ||
|
||
const apmRoot = path.resolve(__dirname, '../..'); | ||
const xpackRoot = path.resolve(apmRoot, '../../..'); | ||
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 = { | ||
apmRoot, | ||
xpackRoot, | ||
kibanaRoot, | ||
tsconfigTpl, | ||
filesToIgnore | ||
}; |
11 changes: 11 additions & 0 deletions
11
x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "../../../apm.tsconfig.json", | ||
"include": [ | ||
"./**/*", | ||
"../../../typings/**/*" | ||
], | ||
"exclude": [ | ||
"**/__fixtures__/**/*", | ||
"./cypress/**/*" | ||
] | ||
} |
36 changes: 36 additions & 0 deletions
36
x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/unoptimize.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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 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'); | ||
|
||
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'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters