Skip to content

Commit

Permalink
[7.x] [APM] Script optimization of APM-specific tsconfig (#498… (#50151)
Browse files Browse the repository at this point in the history
* [APM] Script optimization of APM-specific tsconfig

* Don't break bootstrap process

* Document TS optimizations
  • Loading branch information
dgieselaar authored Nov 11, 2019
1 parent 5fa5ec5 commit 15758c7
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ package-lock.json
*.sublime-*
npm-debug.log*
.tern-project
x-pack/legacy/plugins/apm/tsconfig.json
apm.tsconfig.json
11 changes: 11 additions & 0 deletions x-pack/legacy/plugins/apm/dev_docs/typescript.md
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`
9 changes: 9 additions & 0 deletions x-pack/legacy/plugins/apm/scripts/optimize-tsconfig.js
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 x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/optimize.js
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 x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/paths.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.
*/
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 x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/tsconfig.json
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 x-pack/legacy/plugins/apm/scripts/optimize-tsconfig/unoptimize.js
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');
});
}
};
9 changes: 9 additions & 0 deletions x-pack/legacy/plugins/apm/scripts/unoptimize-tsconfig.js
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();
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

import '../../infra/types/rison_node';
import '../../infra/types/eui';
// EUIBasicTable
import {} from '../../reporting/public/components/report_listing';
// .svg
import '../../canvas/types/webpack';

// Allow unknown properties in an object
export type AllowUnknownProperties<T> = T extends Array<infer X>
? Array<AllowUnknownObjectProperties<X>>
Expand Down

0 comments on commit 15758c7

Please sign in to comment.