This diff reverts09c36781
and tweaks how some of our linting was working. I'm not actually sure what caused the regression that09c36781
was fixing - it was something to do with typescript being used to transpile eslintrc.js, but that being excluded from the tsconfig for those projects. I fixed that by removing `rootDir` from those, but that revealed some other issues with files not getting ignored correctly. I fixed the ignoring issue with a change I've wanted to make to these scripts for a while: only running them on files that are actually tracked by git, instead of on everything with a relevant extension. A side effect of that is that we have to re-implement .eslintignore support ourselves, but that's very straight forward: the `ignore` package that eslint uses is very easy to include. ### Change Type - [x] `internal` — Any other changes that don't affect the published package (will not publish a new version) ### Test Plan - ### Release Notes [internal-only]
This commit is contained in:
parent
da35e0da27
commit
640bc9de24
9 changed files with 62 additions and 35 deletions
|
@ -1,33 +1,68 @@
|
|||
import { join, relative } from 'path'
|
||||
|
||||
import ignore from 'ignore'
|
||||
import * as path from 'path'
|
||||
import { exec } from './lib/exec'
|
||||
import { REPO_ROOT } from './lib/file'
|
||||
import { REPO_ROOT, readFileIfExists } from './lib/file'
|
||||
|
||||
const ESLINT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx']
|
||||
const PRETTIER_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx', 'json']
|
||||
|
||||
async function main() {
|
||||
const shouldFix = process.argv.includes('--fix')
|
||||
|
||||
const lsFiles = await exec('git', ['ls-files', '.'], {
|
||||
processStdoutLine: () => {
|
||||
// don't print anything
|
||||
},
|
||||
})
|
||||
|
||||
const filesByExtension = new Map<string, string[]>()
|
||||
for (const file of lsFiles.trim().split('\n')) {
|
||||
const ext = file.split('.').pop()
|
||||
if (!ext) continue
|
||||
let files = filesByExtension.get(ext)
|
||||
if (!files) {
|
||||
files = []
|
||||
filesByExtension.set(ext.toLowerCase(), files)
|
||||
}
|
||||
files.push(file)
|
||||
}
|
||||
|
||||
let prettierFiles = PRETTIER_EXTENSIONS.flatMap((ext) => filesByExtension.get(ext) ?? [])
|
||||
let eslintFiles = ESLINT_EXTENSIONS.flatMap((ext) => filesByExtension.get(ext) ?? [])
|
||||
|
||||
const relativeCwd = path.relative(REPO_ROOT, process.cwd())
|
||||
|
||||
const prettierIgnoreFile = await readFileIfExists(path.join(REPO_ROOT, '.prettierignore'))
|
||||
if (prettierIgnoreFile) {
|
||||
prettierFiles = prettierFiles
|
||||
.map((f) => path.join(relativeCwd, f))
|
||||
.filter(ignore().add(prettierIgnoreFile).createFilter())
|
||||
.map((f) => path.relative(relativeCwd, f))
|
||||
}
|
||||
|
||||
const eslintIgnoreFile = await readFileIfExists(path.join(REPO_ROOT, '.eslintignore'))
|
||||
if (eslintIgnoreFile) {
|
||||
eslintFiles = eslintFiles
|
||||
.map((f) => path.join(relativeCwd, f))
|
||||
.filter(ignore().add(eslintIgnoreFile).createFilter())
|
||||
.map((f) => path.relative(relativeCwd, f))
|
||||
}
|
||||
|
||||
try {
|
||||
await exec(
|
||||
'yarn',
|
||||
[
|
||||
'run',
|
||||
'-T',
|
||||
'prettier',
|
||||
shouldFix ? '--write' : '--check',
|
||||
// we have to run prettier from root so it picks up the ignore file
|
||||
join(relative(REPO_ROOT, process.cwd()), '**', '*.{ts,tsx,js,jsx,json}'),
|
||||
],
|
||||
{ pwd: REPO_ROOT }
|
||||
)
|
||||
await exec('yarn', [
|
||||
'run',
|
||||
'-T',
|
||||
'prettier',
|
||||
shouldFix ? '--write' : '--check',
|
||||
...prettierFiles,
|
||||
])
|
||||
await exec('yarn', [
|
||||
'run',
|
||||
'-T',
|
||||
'eslint',
|
||||
'--report-unused-disable-directives',
|
||||
'--ignore-path',
|
||||
join(REPO_ROOT, '.eslintignore'),
|
||||
shouldFix ? '--fix' : null,
|
||||
'.',
|
||||
...eslintFiles,
|
||||
])
|
||||
} catch (error) {
|
||||
process.exit(1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue