[7.x] [APM] Make typescript optimization process compatible wi… (#59289)

* [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.

* Update dev_docs/typescript.md

* Use spread op instead of assign

* Use console.error instead of console.log
This commit is contained in:
Dario Gieselaar 2020-03-04 13:30:50 +01:00 committed by GitHub
parent b89066baed
commit 649a2f5098
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 47 additions and 58 deletions

2
.gitignore vendored
View file

@ -45,5 +45,3 @@ package-lock.json
*.sublime-*
npm-debug.log*
.tern-project
x-pack/legacy/plugins/apm/tsconfig.json
apm.tsconfig.json

View file

@ -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:

View file

@ -6,4 +6,7 @@
const { optimizeTsConfig } = require('./optimize-tsconfig/optimize');
optimizeTsConfig();
optimizeTsConfig().catch(err => {
console.error(err);
process.exit(1);
});

View file

@ -7,29 +7,26 @@
/* 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 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 +47,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({ ...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

View file

@ -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,

View file

@ -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/**/*"
]
}

View file

@ -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');
}
};

View file

@ -6,4 +6,7 @@
const { unoptimizeTsConfig } = require('./optimize-tsconfig/unoptimize');
unoptimizeTsConfig();
unoptimizeTsConfig().catch(err => {
console.error(err);
process.exit(1);
});

View file

@ -1,3 +0,0 @@
{
"extends": "../../../tsconfig.json"
}