[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.
This commit is contained in:
Spencer 2019-03-08 13:51:55 -08:00 committed by GitHub
parent 5f6ff8b4c2
commit 8d92a940fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 136 additions and 5 deletions

View file

@ -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();

View file

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

View file

@ -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> | void) {
type RunFn = (args: { log: ToolingLog; flags: Flags }) => Promise<void> | 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);

View file

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

View file

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

View file

@ -26,6 +26,7 @@ module.exports = function (grunt) {
'run:eslint',
'run:tslint',
'run:sasslint',
'run:checkTsProjects',
'run:typeCheck',
'run:i18nCheck',
'run:checkFileCasing',

View file

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