-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.lintstagedrc.mjs
80 lines (67 loc) · 2.5 KB
/
.lintstagedrc.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { ESLint } from 'eslint';
import { glob } from 'glob';
import { relative } from 'node:path';
import process from 'node:process';
const ignoredFiles = [
'**/node_modules/**',
'**/.next/**',
'**/.vitest/**',
'**/*.d.ts',
'**/dist/**',
'**/storybook-static/**',
'**/vite.config.ts',
'**/vitest.config.ts',
'**/vitest.setup.ts',
'**/vitest.workspace.ts'
];
const filterIgnoredFiles = async (requestedFiles) => {
const cwd = process.cwd();
const eslint = new ESLint({
// Override ESLint's cwd to resolve to the project root, so that
// relative paths work as intended. Otherwise, the paths resolve
// to the directory where lint-staged is run.
cwd
});
const files = requestedFiles.map((file) => relative(cwd, file));
const eslintIgnores = await Promise.all(files.map((file) => eslint.isPathIgnored(file)));
const filesToLint = files
.filter((_, index) => !eslintIgnores[index])
.filter((file) => {
// Filter using `ignoredFiles` and `glob`
return !glob.hasMagic(file, { ignore: ignoredFiles });
})
.map((file) => `./${file}`);
return filesToLint.join(' ');
};
const testAndLintAll = async (_) => {
const cwd = process.cwd();
const allFiles = await glob(`${cwd}/**/*.{ts,tsx}`, {
ignore: ignoredFiles
});
const allPrettierFiles = (
await glob(`${cwd}/**/*.{mjs,ts,tsx,json,scss}`, {
ignore: ignoredFiles
})
)
.map((file) => relative(cwd, file))
.map((file) => `./${file}`)
.join(' ');
const filesToLint = await filterIgnoredFiles(allFiles);
const prettier = `prettier --write ${allPrettierFiles}`;
const eslint = `eslint --max-warnings=0 --fix ${filesToLint}`;
return [...(filesToLint.length > 0 ? [eslint] : []), ...(allPrettierFiles.length > 0 ? [prettier] : [])];
};
export default {
'**/*.{js,ts,jsx,tsx}': async (files) => {
const filesToLint = await filterIgnoredFiles(files);
if (filesToLint.length === 0) return [];
return [`prettier --write ${filesToLint}`, `eslint --max-warnings=0 --fix ${filesToLint}`];
},
'**/*.{css,scss}': async (files) => {
const filesToLint = await filterIgnoredFiles(files);
if (filesToLint.length === 0) return [];
return [`prettier --write ${filesToLint}`];
},
// Lint and test all files since dependency updates could potentially break tests.
'**/package.json': testAndLintAll
};