From 5a5b04b385cac7b20e3f3b0c81933a0a040207ae Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Fri, 18 Sep 2020 12:53:41 +0200 Subject: [PATCH 1/3] [APM] APM jest script --- x-pack/plugins/apm/scripts/jest.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 x-pack/plugins/apm/scripts/jest.js diff --git a/x-pack/plugins/apm/scripts/jest.js b/x-pack/plugins/apm/scripts/jest.js new file mode 100644 index 0000000000000..5c29dd9126937 --- /dev/null +++ b/x-pack/plugins/apm/scripts/jest.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-next-line import/no-extraneous-dependencies +require('@babel/register')({ + extensions: ['.js'], + plugins: [], + presets: [ + '@babel/typescript', + ['@babel/preset-env', { targets: { node: 'current' } }], + ], +}); + +// eslint-disable-next-line import/no-extraneous-dependencies +const { run } = require('jest'); + +process.env.NODE_ENV = process.env.NODE_ENV || 'test'; + +const config = require('../jest.config.js'); + +const argv = [...process.argv.slice(2), '--config', JSON.stringify(config)]; + +run(argv); From 1e4b918d4c35788d5ff493f39673d13a1eb1544a Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Fri, 18 Sep 2020 16:05:28 +0200 Subject: [PATCH 2/3] [APM] Script static checkers --- .../aggregate-latency-metrics/index.ts | 2 +- x-pack/plugins/apm/scripts/eslint.js | 37 ++++++++++ x-pack/plugins/apm/scripts/precommit.js | 68 +++++++++++++++++++ .../scripts/shared/create-or-update-index.ts | 2 +- .../scripts/upload-telemetry-data/index.ts | 2 +- 5 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 x-pack/plugins/apm/scripts/eslint.js create mode 100644 x-pack/plugins/apm/scripts/precommit.js diff --git a/x-pack/plugins/apm/scripts/aggregate-latency-metrics/index.ts b/x-pack/plugins/apm/scripts/aggregate-latency-metrics/index.ts index ef85112918712..c1cb903a0bb3e 100644 --- a/x-pack/plugins/apm/scripts/aggregate-latency-metrics/index.ts +++ b/x-pack/plugins/apm/scripts/aggregate-latency-metrics/index.ts @@ -389,7 +389,7 @@ export async function aggregateLatencyMetrics() { return; } - const response = await destClient?.bulk({ + const response = await (destClient as any)?.bulk({ refresh: 'wait_for', body: flatten( docs.map((doc) => [ diff --git a/x-pack/plugins/apm/scripts/eslint.js b/x-pack/plugins/apm/scripts/eslint.js new file mode 100644 index 0000000000000..f221fc6dab23d --- /dev/null +++ b/x-pack/plugins/apm/scripts/eslint.js @@ -0,0 +1,37 @@ +/* + * 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-next-line import/no-extraneous-dependencies +const { CLIEngine } = require('eslint'); +const { resolve } = require('path'); +//eslint-disable-next-line import/no-extraneous-dependencies +const { argv } = require('yargs'); + +async function run() { + const fix = !!argv.fix; + + const engine = new CLIEngine({ + fix, + cache: true, + extensions: ['.js', '.jsx', '.ts', '.tsx'], + }); + + const report = engine.executeOnFiles(resolve(__dirname, '..')); + + const formatter = engine.getFormatter(); + + return formatter(report.results); +} + +run() + .then((text) => { + //eslint-disable-next-line no-console + console.log(text); + process.exit(0); + }) + .catch((err) => { + console.error(err); + process.exit(1); + }); diff --git a/x-pack/plugins/apm/scripts/precommit.js b/x-pack/plugins/apm/scripts/precommit.js new file mode 100644 index 0000000000000..158dfdfc0c3f0 --- /dev/null +++ b/x-pack/plugins/apm/scripts/precommit.js @@ -0,0 +1,68 @@ +/* + * 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 no-console*/ +/* eslint-disable import/no-extraneous-dependencies*/ + +const execa = require('execa'); +const Listr = require('listr'); +const { resolve } = require('path'); + +const cwd = resolve(__dirname, '../../../..'); + +const execaOpts = { cwd, stderr: 'pipe' }; + +const tasks = new Listr( + [ + { + title: 'Jest', + task: () => + execa( + 'node', + [ + resolve(__dirname, './jest.js'), + '--reporters', + resolve(__dirname, './node_modules/jest-silent-reporter'), + '--collect-coverage', + ], + execaOpts + ), + }, + { + title: 'Typescript', + task: () => + execa('node', [resolve(__dirname, 'optimize-tsconfig.js')]).then(() => + execa( + require.resolve('typescript/bin/tsc'), + [ + '--project', + resolve(__dirname, '../../../tsconfig.json'), + '--pretty', + '--noEmit', + '--skipLibCheck', + ], + execaOpts + ) + ), + }, + { + title: 'Lint', + task: () => execa('node', [resolve(__dirname, 'eslint.js')], execaOpts), + }, + ], + { exitOnError: false, concurrent: true } +); + +tasks.run().catch((error) => { + // from src/dev/typescript/exec_in_projects.ts + process.exitCode = 1; + + const errors = error.errors || [error]; + + for (const e of errors) { + process.stderr.write(e.stdout); + } +}); diff --git a/x-pack/plugins/apm/scripts/shared/create-or-update-index.ts b/x-pack/plugins/apm/scripts/shared/create-or-update-index.ts index 01fa5b0509bcd..83de83ace07b3 100644 --- a/x-pack/plugins/apm/scripts/shared/create-or-update-index.ts +++ b/x-pack/plugins/apm/scripts/shared/create-or-update-index.ts @@ -39,7 +39,7 @@ export async function createOrUpdateIndex({ await client.indices.exists({ index: indexName, }) - ).body as boolean; + ).body as unknown; if (!indexExists) { await client.indices.create({ diff --git a/x-pack/plugins/apm/scripts/upload-telemetry-data/index.ts b/x-pack/plugins/apm/scripts/upload-telemetry-data/index.ts index fd628f77eb519..ca47540b04d82 100644 --- a/x-pack/plugins/apm/scripts/upload-telemetry-data/index.ts +++ b/x-pack/plugins/apm/scripts/upload-telemetry-data/index.ts @@ -83,7 +83,7 @@ async function uploadData() { apmAgentConfigurationIndex: '.apm-agent-configuration', }, search: (body) => { - return client.search(body as any).then((res) => res.body); + return client.search(body as any).then((res) => res.body as any); }, indicesStats: (body) => { return client.indices.stats(body as any).then((res) => res.body); From 0d9673584c609d8f64457578a8e5bd0d5ae39eb1 Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Fri, 18 Sep 2020 17:33:01 +0200 Subject: [PATCH 3/3] Disable collection of coverage --- x-pack/plugins/apm/scripts/precommit.js | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/plugins/apm/scripts/precommit.js b/x-pack/plugins/apm/scripts/precommit.js index 158dfdfc0c3f0..87da3c1db8b28 100644 --- a/x-pack/plugins/apm/scripts/precommit.js +++ b/x-pack/plugins/apm/scripts/precommit.js @@ -27,6 +27,7 @@ const tasks = new Listr( '--reporters', resolve(__dirname, './node_modules/jest-silent-reporter'), '--collect-coverage', + 'false', ], execaOpts ),