From 8d92a940fa15610f52e5c2b1402a9e0787fd9c2e Mon Sep 17 00:00:00 2001 From: Spencer Date: Fri, 8 Mar 2019 13:51:55 -0800 Subject: [PATCH] [ts] add script to verify that all ts is in a project (#32727) Based on #32705 We currently have TypeScript code that was backported to 7.0, which was backported without issue because it falls outside of any TypeScript projects in 7.0. This means that the pre-commit hooks break on changes to these files, and that they are not getting type checked by the type_check script. To fix this we need to verify that every typescript file in the repository is covered by a tsconfig.json file as part of CI. --- scripts/check_ts_projects.js | 21 +++++ src/dev/run/flags.ts | 6 +- src/dev/run/run.ts | 12 ++- .../typescript/run_check_ts_projects_cli.ts | 91 +++++++++++++++++++ tasks/config/run.js | 9 ++ tasks/jenkins.js | 1 + tasks/test.js | 1 + 7 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 scripts/check_ts_projects.js create mode 100644 src/dev/typescript/run_check_ts_projects_cli.ts diff --git a/scripts/check_ts_projects.js b/scripts/check_ts_projects.js new file mode 100644 index 0000000000000..ece7db6843489 --- /dev/null +++ b/scripts/check_ts_projects.js @@ -0,0 +1,21 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +require('../src/setup_node_env'); +require('../src/dev/typescript/run_check_ts_projects_cli').runCheckTsProjectsCli(); diff --git a/src/dev/run/flags.ts b/src/dev/run/flags.ts index 8adbf24567b35..a8fc31391bf80 100644 --- a/src/dev/run/flags.ts +++ b/src/dev/run/flags.ts @@ -21,6 +21,8 @@ import { relative } from 'path'; import getopts from 'getopts'; +import { Options } from './run'; + export interface Flags { verbose: boolean; quiet: boolean; @@ -57,11 +59,11 @@ export function getFlags(argv: string[]): Flags { }; } -export function getHelp() { +export function getHelp(options: Options) { return ` node ${relative(process.cwd(), process.argv[1])} - Runs a dev task + ${options.helpDescription || 'Runs a dev task'} Options: --verbose, -v Log verbosely diff --git a/src/dev/run/run.ts b/src/dev/run/run.ts index 0cdf2fe37df98..45f9e254e0ad0 100644 --- a/src/dev/run/run.ts +++ b/src/dev/run/run.ts @@ -21,11 +21,17 @@ import { pickLevelFromFlags, ToolingLog } from '@kbn/dev-utils'; import { isFailError } from './fail'; import { Flags, getFlags, getHelp } from './flags'; -export async function run(body: (args: { log: ToolingLog; flags: Flags }) => Promise | void) { +type RunFn = (args: { log: ToolingLog; flags: Flags }) => Promise | void; + +export interface Options { + helpDescription?: string; +} + +export async function run(fn: RunFn, options: Options = {}) { const flags = getFlags(process.argv.slice(2)); if (flags.help) { - process.stderr.write(getHelp()); + process.stderr.write(getHelp(options)); process.exit(1); } @@ -35,7 +41,7 @@ export async function run(body: (args: { log: ToolingLog; flags: Flags }) => Pro }); try { - await body({ log, flags }); + await fn({ log, flags }); } catch (error) { if (isFailError(error)) { log.error(error.message); diff --git a/src/dev/typescript/run_check_ts_projects_cli.ts b/src/dev/typescript/run_check_ts_projects_cli.ts new file mode 100644 index 0000000000000..dd8bbf1c5a220 --- /dev/null +++ b/src/dev/typescript/run_check_ts_projects_cli.ts @@ -0,0 +1,91 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { resolve } from 'path'; + +import execa from 'execa'; + +import { run } from '../run'; + +const REPO_ROOT = resolve(__dirname, '../../../'); +import { File } from '../file'; +import { PROJECTS } from './projects'; + +export async function runCheckTsProjectsCli() { + run( + async ({ log }) => { + const files = await execa.stdout('git', ['ls-tree', '--name-only', '-r', 'HEAD'], { + cwd: REPO_ROOT, + }); + + const isNotInTsProject: File[] = []; + const isInMultipleTsProjects: File[] = []; + + for (const lineRaw of files.split('\n')) { + const line = lineRaw.trim(); + + if (!line) { + continue; + } + + const file = new File(resolve(REPO_ROOT, line)); + if (!file.isTypescript() || file.isFixture()) { + continue; + } + + log.verbose('Checking %s', file.getAbsolutePath()); + + const projects = PROJECTS.filter(p => p.isAbsolutePathSelected(file.getAbsolutePath())); + if (projects.length === 0) { + isNotInTsProject.push(file); + } + if (projects.length > 1) { + isInMultipleTsProjects.push(file); + } + } + + if (!isNotInTsProject.length && !isInMultipleTsProjects.length) { + log.success('All ts files belong to a single ts project'); + return; + } + + if (isNotInTsProject.length) { + log.error( + `The following files do not belong to a tsconfig.json file, or that tsconfig.json file is not listed in src/dev/typescript/projects.ts\n${isNotInTsProject + .map(file => ` - ${file.getRelativePath()}`) + .join('\n')}` + ); + } + + if (isInMultipleTsProjects.length) { + log.error( + `The following files belong to multiple tsconfig.json files listed in src/dev/typescript/projects.ts\n${isInMultipleTsProjects + .map(file => ` - ${file.getRelativePath()}`) + .join('\n')}` + ); + } + + process.exit(1); + }, + { + helpDescription: + 'Check that all .ts and .tsx files in the repository are assigned to a tsconfig.json file', + } + ); +} diff --git a/tasks/config/run.js b/tasks/config/run.js index f0bb310ff9806..ba21ce0986d55 100644 --- a/tasks/config/run.js +++ b/tasks/config/run.js @@ -109,6 +109,15 @@ module.exports = function (grunt) { ] }, + // used by the test and jenkins:unit tasks + // ensures that all typescript files belong to a typescript project + checkTsProjects: { + cmd: process.execPath, + args: [ + require.resolve('../../scripts/check_ts_projects') + ] + }, + // used by the test and jenkins:unit tasks // runs the i18n_check script to check i18n engine usage i18nCheck: { diff --git a/tasks/jenkins.js b/tasks/jenkins.js index b56cbdb7618dd..47b9429fed001 100644 --- a/tasks/jenkins.js +++ b/tasks/jenkins.js @@ -26,6 +26,7 @@ module.exports = function (grunt) { 'run:eslint', 'run:tslint', 'run:sasslint', + 'run:checkTsProjects', 'run:typeCheck', 'run:i18nCheck', 'run:checkFileCasing', diff --git a/tasks/test.js b/tasks/test.js index 269afcc4068f0..ee6d0e87d95ad 100644 --- a/tasks/test.js +++ b/tasks/test.js @@ -72,6 +72,7 @@ module.exports = function (grunt) { !grunt.option('quick') && 'run:eslint', !grunt.option('quick') && 'run:tslint', !grunt.option('quick') && 'run:sasslint', + !grunt.option('quick') && 'run:checkTsProjects', !grunt.option('quick') && 'run:typeCheck', !grunt.option('quick') && 'run:i18nCheck', 'run:checkFileCasing',