2023-06-02 10:45:51 +00:00
|
|
|
import ignore from 'ignore'
|
|
|
|
import * as path from 'path'
|
2023-04-25 11:01:25 +00:00
|
|
|
import { exec } from './lib/exec'
|
2023-06-02 10:45:51 +00:00
|
|
|
import { REPO_ROOT, readFileIfExists } from './lib/file'
|
|
|
|
|
|
|
|
const ESLINT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx']
|
2024-02-07 16:02:22 +00:00
|
|
|
const PRETTIER_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx', 'json']
|
2023-04-25 11:01:25 +00:00
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const shouldFix = process.argv.includes('--fix')
|
|
|
|
|
2023-06-02 10:45:51 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-02-07 16:02:22 +00:00
|
|
|
let prettierFiles = PRETTIER_EXTENSIONS.flatMap((ext) => filesByExtension.get(ext) ?? [])
|
2023-06-02 10:45:51 +00:00
|
|
|
let eslintFiles = ESLINT_EXTENSIONS.flatMap((ext) => filesByExtension.get(ext) ?? [])
|
|
|
|
|
|
|
|
const relativeCwd = path.relative(REPO_ROOT, process.cwd())
|
|
|
|
|
2024-02-07 16:02:22 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2023-06-02 10:45:51 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2023-04-25 11:01:25 +00:00
|
|
|
try {
|
2024-02-07 16:02:22 +00:00
|
|
|
await exec('yarn', [
|
|
|
|
'run',
|
|
|
|
'-T',
|
|
|
|
'prettier',
|
|
|
|
shouldFix ? '--write' : '--check',
|
|
|
|
'--cache',
|
|
|
|
...prettierFiles,
|
|
|
|
])
|
2023-04-25 11:01:25 +00:00
|
|
|
await exec('yarn', [
|
|
|
|
'run',
|
|
|
|
'-T',
|
|
|
|
'eslint',
|
|
|
|
'--report-unused-disable-directives',
|
|
|
|
shouldFix ? '--fix' : null,
|
2023-06-02 10:45:51 +00:00
|
|
|
...eslintFiles,
|
2023-04-25 11:01:25 +00:00
|
|
|
])
|
|
|
|
} catch (error) {
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|