Skip to content

Commit

Permalink
[ts] add script to verify that all ts is in a project
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Mar 7, 2019
1 parent 115e47e commit 88ab98c
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 5 deletions.
21 changes: 21 additions & 0 deletions 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();
6 changes: 4 additions & 2 deletions src/dev/run/flags.ts
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
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
60 changes: 60 additions & 0 deletions src/dev/typescript/run_check_ts_projects_cli.ts
@@ -0,0 +1,60 @@
/*
* 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 { getTsProjectForAbsolutePath } from './get_ts_project_for_absolute_path';

export async function runCheckTsProjectsCli() {
run(
async ({ log }) => {
const files = await execa.stdout('git', ['ls-tree', '--name-only', '-r', 'HEAD'], {
cwd: REPO_ROOT,
});

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());
getTsProjectForAbsolutePath(file.getAbsolutePath());
}

log.success('All ts files belong to a ts project');
},
{
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
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
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
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 88ab98c

Please sign in to comment.