Skip to content

Commit

Permalink
Add lint --all support. (#1186)
Browse files Browse the repository at this point in the history
Run `lint` on all files in the test workflow, but only on changed files
locally.
  • Loading branch information
goodov authored Aug 21, 2024
1 parent a0fefba commit 5fbe5c0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 40 deletions.
18 changes: 8 additions & 10 deletions .github/workflows/generate-test-seed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Generate Test Seed
on:
pull_request:
paths:
- '.github/workflows/generate-test-seed.yml'
- 'seed/seed.json'
- 'studies/**'

Expand Down Expand Up @@ -50,19 +51,16 @@ jobs:
await comment(github, context, commentBody)
- name: Install
run: |
npm ci
run: npm ci

- name: Build & Test
run: |
npm run typecheck:scripts
npm run build:proto
npm run typecheck
npm run test
- name: Typecheck
run: npm run typecheck

- name: Lint
run: |
npm run lint -- --base "$BASE_SHA"
run: npm run lint -- --base "$BASE_SHA"

- name: Test
run: npm run test

- name: Generate seed
run: |
Expand Down
18 changes: 6 additions & 12 deletions .github/workflows/test-src.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,20 @@ on:
jobs:
build:
runs-on: ubuntu-latest
env:
BASE_SHA: '${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }}'

steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4

- name: fetch base commit
run: git fetch --depth=1 origin "$BASE_SHA"

- name: npm install
run: npm install

- name: typecheck:scripts
run: npm run typecheck:scripts
- name: typecheck
run: npm run typecheck

- name: build
run: npm run build
- name: lint
run: npm run lint -- --all

- name: run tests
run: npm run test

- name: lint
run: npm run lint -- --base "$BASE_SHA"
- name: build
run: npm run build
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
seed/seed.json
src/proto/generated/*
src/test/data/**/*unformatted*
src/web/css/bootstrap.min.css
59 changes: 41 additions & 18 deletions src/scripts/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// You can obtain one at https://mozilla.org/MPL/2.0/.

import { program } from '@commander-js/extra-typings';
import { spawnSync } from 'child_process';
import { execSync, spawnSync } from 'child_process';

// @ts-expect-error lint-staged is not typed.
import lintStaged from 'lint-staged';
Expand All @@ -16,19 +16,57 @@ program
'types, and runs the corresponding linters. Use this command to ensure code\n' +
'quality before committing or pushing changes.',
)
.option('-a, --all', 'lint all files in the repository')
.option('-b, --base <value>', 'base branch to compare against')
.option('-s, --staged', 'use staged files instead of the branch diff')
.option('-f, --fix', 'automatically fix problems')
.action(main)
.parse();

interface Options {
all?: true;
base?: string;
staged?: true;
fix?: true;
}

async function main(options: Options) {
process.exitCode = options.all
? lintAllFiles(options)
: await lintDiffFiles(options);
}

// Returns an array of commands to lint all files.
function getLintAllCommands(options: Options): string[] {
return [
'prettier . --ignore-unknown' + (options.fix ? ' --write' : ' --check'),
'eslint . --config src/.eslintrc.js' + (options.fix ? ' --fix' : ''),
// TODO(goodov): Add a command to lint JSON studies when per-file structure
// appears.
];
}

// Returns a lint-staged configuration to lint modified files.
function getLintDiffCommands(options: Options): Record<string, any> {
return {
'*': 'prettier --ignore-unknown' + (options.fix ? ' --write' : ' --check'),
'*.{ts,js,tsx,jsx}':
'eslint --config src/.eslintrc.js' + (options.fix ? ' --fix' : ''),
'studies/**/*.json':
'npm run seed_tools -- check_study' + (options.fix ? ' --fix' : ''),
};
}

function lintAllFiles(options: Options): number {
for (const command of getLintAllCommands(options)) {
console.log(`Running: ${command}`);
execSync(command, { stdio: 'inherit' });
}

return 0;
}

async function lintDiffFiles(options: Options): Promise<number> {
if (options.base !== undefined && options.staged) {
console.error('The --base and --staged options are mutually exclusive');
process.exit(1);
Expand All @@ -40,27 +78,12 @@ async function main(options: Options) {
const passed: boolean = await lintStaged({
allowEmpty: false,
concurrent: !options.fix,
config: createLintStagedConfig(options),
config: getLintDiffCommands(options),
cwd: process.cwd(),
diff: options.base !== undefined ? `${options.base}..HEAD` : undefined,
});
process.exitCode = passed ? 0 : 1;
}

function createLintStagedConfig(options: Options): any {
const config: Record<string, any> = {
'*': 'prettier --ignore-unknown' + (options.fix ? ' --write' : ' --check'),
'studies/**/*.json':
'npm run seed_tools -- check_study' + (options.fix ? ' --fix' : ''),
'*.{ts,js,tsx,jsx}':
'eslint --config src/.eslintrc.js' + (options.fix ? ' --fix' : ''),
};

if (!options.fix) {
config['*.ts?(x)'] = () => 'npm run typecheck';
}

return config;
return passed ? 0 : 1;
}

function isCurrentBranchAncestorOf(base: string): boolean {
Expand Down

0 comments on commit 5fbe5c0

Please sign in to comment.