tldraw/scripts/lint.ts
Mitja Bezenšek 9e7d258b5f
Add prettier caching (#2212)
Adds `--cache` flag to prettier which significantly speeds up `yarn
format`:
https://prettier.io/docs/en/cli#--cache

One downside is that changing the plugins we use with prettier will not
cause the cache to invalidate. Stills seems worth it though.

> Plugins version and implementation are not used as cache keys. We
recommend that you delete the cache when updating plugins.

### Change Type

- [ ] `patch` — Bug fix
- [ ] `minor` — New feature
- [ ] `major` — Breaking change
- [ ] `dependencies` — Changes to package dependencies[^1]
- [ ] `documentation` — Changes to the documentation only[^2]
- [ ] `tests` — Changes to any test code only[^2]
- [x] `internal` — Any other changes that don't affect the published
package[^2]
- [ ] I don't know

[^1]: publishes a `patch` release, for devDependencies use `internal`
[^2]: will not publish a new version

### Test Plan

1. Run `yarn format`
2. Run `yarn format` again, this time it should be significantly faster.

### Release Notes

- Speed up formatting of files via `yarn format`.
2023-11-14 09:06:52 +00:00

73 lines
1.9 KiB
TypeScript
Executable file

import ignore from 'ignore'
import * as path from 'path'
import { exec } from './lib/exec'
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',
'--cache',
...prettierFiles,
])
await exec('yarn', [
'run',
'-T',
'eslint',
'--report-unused-disable-directives',
shouldFix ? '--fix' : null,
...eslintFiles,
])
} catch (error) {
process.exit(1)
}
}
main()