-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlint-staged.config.mjs
More file actions
205 lines (176 loc) · 5.66 KB
/
Copy pathlint-staged.config.mjs
File metadata and controls
205 lines (176 loc) · 5.66 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* eslint-disable @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return */
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
const workspaceRoot = process.cwd();
const jsTsPattern = "**/*.{cjs,cts,js,jsx,mjs,mts,ts,tsx}";
const prettierOnlyPattern = "**/*.{css,html,json,md,mdx}";
const eslintPackageRoots = ["apps", "packages"];
const rootScopedIgnoredEslintPrefixes = [".github/scripts/fixtures/"];
const rootScopedIgnoredLintPrefixes = ["packages/api-contract/src/generated/"];
/**
* Quote a file path for safe shell usage.
* @param {string} value
* @returns {string}
*/
const quote = (value) => `'${value.replace(/'/g, `'\\''`)}'`;
/**
* Normalize a lint-staged file argument to a workspace-relative path.
* lint-staged may pass absolute paths to task functions.
* @param {string} file
* @returns {string}
*/
const normalizePathSeparators = (file) => file.replace(/\\/g, "/");
const toWorkspaceRelativePath = (file) => {
if (path.isAbsolute(file)) {
return normalizePathSeparators(path.relative(workspaceRoot, file));
}
return normalizePathSeparators(file);
};
/**
* Keep generated contract files out of staged format/lint routing.
* @param {string} file
* @returns {boolean}
*/
const isIgnoredByLintRouting = (file) =>
rootScopedIgnoredLintPrefixes.some((prefix) => file.startsWith(prefix));
/**
* Keep generated files out of formatting routing as well.
* @param {string} file
* @returns {boolean}
*/
const isIgnoredByFormattingRouting = (file) => isIgnoredByLintRouting(file);
/**
* Discover package directories that own their own ESLint config.
* @returns {string[]}
*/
const findPackageScopedEslintDirs = () =>
eslintPackageRoots
.flatMap((rootDir) => {
const absoluteRootDir = path.join(workspaceRoot, rootDir);
if (!fs.existsSync(absoluteRootDir)) {
return [];
}
return fs
.readdirSync(absoluteRootDir, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => path.join(rootDir, entry.name))
.filter((packageDir) => {
const absolutePackageDir = path.join(workspaceRoot, packageDir);
return (
fs.existsSync(path.join(absolutePackageDir, "package.json")) &&
fs.existsSync(path.join(absolutePackageDir, "eslint.config.mjs"))
);
});
})
.sort((left, right) => right.length - left.length);
const packageScopedEslintDirs = findPackageScopedEslintDirs();
const packageScopedIgnoredEslintFiles = new Map([
[
"apps/api",
new Set([
"prisma.config.ts",
"prisma.test.config.ts",
"provision-ci-dbs.mjs",
]),
],
]);
/**
* @param {string} file
* @returns {string | null}
*/
const findOwningEslintPackageDir = (file) =>
packageScopedEslintDirs.find(
(packageDir) => file === packageDir || file.startsWith(`${packageDir}/`),
) ?? null;
/**
* Keep root lint-staged ESLint aligned with root eslint.config.mjs ignores.
* @param {string} file
* @returns {boolean}
*/
const isIgnoredByRootEslint = (file) =>
rootScopedIgnoredEslintPrefixes.some((prefix) => file.startsWith(prefix));
/**
* @param {string[]} files
* @returns {string[]}
*/
const runRootEslint = (files) => {
const lintableFiles = files.filter((file) => !isIgnoredByRootEslint(file));
if (lintableFiles.length === 0) {
return [];
}
return [
`eslint --fix --max-warnings 0 ${lintableFiles.map(quote).join(" ")}`,
];
};
/**
* @param {string[]} files
* @returns {string[]}
*/
const runPackageEslint = (packageDir, files) => {
const ignoredFiles = packageScopedIgnoredEslintFiles.get(packageDir) ?? null;
const lintableFiles =
ignoredFiles === null
? files
: files.filter((file) => !ignoredFiles.has(path.basename(file)));
if (lintableFiles.length === 0) {
return [];
}
const packageAbsoluteDir = path.join(workspaceRoot, packageDir);
const packageRelativeFiles = lintableFiles.map((file) =>
quote(
normalizePathSeparators(
path.relative(packageAbsoluteDir, path.join(workspaceRoot, file)),
),
),
);
return [
`pnpm --dir ${quote(packageDir)} exec eslint --fix --max-warnings 0 ${packageRelativeFiles.join(" ")}`,
];
};
/**
* Build a prettier command only when there are files left to format.
* @param {string[]} files
* @returns {string[]}
*/
const runPrettier = (files) => {
const formattedFiles = files
.map(toWorkspaceRelativePath)
.filter((file) => !isIgnoredByFormattingRouting(file));
if (formattedFiles.length === 0) {
return [];
}
return [`prettier --write ${formattedFiles.map(quote).join(" ")}`];
};
export default {
/**
* @param {string[]} files
* @returns {string[]}
*/
[jsTsPattern]: (files) => {
const normalizedFiles = files
.map(toWorkspaceRelativePath)
.filter((file) => !isIgnoredByLintRouting(file));
const rootFiles = [];
const packageFiles = new Map();
for (const file of normalizedFiles) {
const packageDir = findOwningEslintPackageDir(file);
if (packageDir === null) {
rootFiles.push(file);
continue;
}
const existingFiles = packageFiles.get(packageDir) ?? [];
existingFiles.push(file);
packageFiles.set(packageDir, existingFiles);
}
return [
...runPrettier(normalizedFiles),
...runRootEslint(rootFiles),
...Array.from(packageFiles.entries()).flatMap(
([packageDir, packageDirFiles]) =>
runPackageEslint(packageDir, packageDirFiles),
),
];
},
[prettierOnlyPattern]: (files) => runPrettier(files),
};