Skip to content

Commit

Permalink
[ts] add script to verify that all ts is in a project (#32727)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Spencer authored Mar 8, 2019
1 parent 5f6ff8b commit 8d92a94
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 5 deletions.
21 changes: 21 additions & 0 deletions scripts/check_ts_projects.js
Original file line number Diff line number Diff line change
@@ -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();
6 changes: 4 additions & 2 deletions src/dev/run/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { relative } from 'path';

import getopts from 'getopts';

import { Options } from './run';

export interface Flags {
verbose: boolean;
quiet: boolean;
Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions src/dev/run/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
Expand Down
91 changes: 91 additions & 0 deletions src/dev/typescript/run_check_ts_projects_cli.ts
Original file line number Diff line number Diff line change
@@ -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',
}
);
}
9 changes: 9 additions & 0 deletions tasks/config/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
1 change: 1 addition & 0 deletions tasks/jenkins.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = function (grunt) {
'run:eslint',
'run:tslint',
'run:sasslint',
'run:checkTsProjects',
'run:typeCheck',
'run:i18nCheck',
'run:checkFileCasing',
Expand Down
1 change: 1 addition & 0 deletions tasks/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 8d92a94

Please sign in to comment.