Repository navigation
Expand file tree
/
Copy pathlint-staged.config.mjs
More file actions
54 lines (48 loc) · 2.4 KB
/
Copy pathlint-staged.config.mjs
File metadata and controls
54 lines (48 loc) · 2.4 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
// @ts-check
import { existsSync } from "node:fs";
import path from "node:path";
const gitRoot = process.cwd();
/**
* Nearest ancestor (including the file's own directory) that owns an eslint.config.mjs, defaulting to the repo root.
*/
function findOwningEslintConfig(absoluteFile) {
let dir = path.dirname(absoluteFile);
while (dir !== path.dirname(dir)) {
const candidate = path.join(dir, "eslint.config.mjs");
if (existsSync(candidate)) return candidate;
if (dir === gitRoot) break;
dir = path.dirname(dir);
}
return path.join(gitRoot, "eslint.config.mjs");
}
const toArg = file => JSON.stringify(path.relative(gitRoot, file));
const eslintBin = path.join(gitRoot, "node_modules", ".bin", "eslint");
/**
* Runs eslint from the owning config's directory because the shared import resolver reads `./tsconfig.json` relative to
* cwd, so linting from the git root stops resolving `@src` aliases and silently drops every rule that follows them.
*/
function eslintCommandsByOwningConfig(files) {
const filesByConfig = new Map();
for (const file of files) {
const config = findOwningEslintConfig(file);
filesByConfig.set(config, [...(filesByConfig.get(config) ?? []), file]);
}
return [...filesByConfig].map(([config, group]) => {
const configDir = path.dirname(config);
const args = group.map(file => JSON.stringify(path.relative(configDir, file))).join(" ");
return `sh -c 'bin="$1"; cd "$2" || exit 1; shift 2; exec "$bin" --fix --quiet "$@"' sh ${JSON.stringify(eslintBin)} ${JSON.stringify(configDir)} ${args}`;
});
}
export default {
"*": "secretlint --no-glob",
"*.{mjs,js,jsx,ts,tsx}": files => [...eslintCommandsByOwningConfig(files), `prettier --write ${files.map(toArg).join(" ")}`],
"package.json": "npx sort-package-json",
"package-lock.json,**/*/package.json": "npm ci --dry-run --ignore-scripts > /dev/null",
"./packages/ui/**/*.ts": "npm run validate:types -w packages/ui",
"./packages/net/**/*.ts": "npm run validate:types -w packages/net",
"./packages/network-store/**/*.ts": "npm run validate:types -w packages/network-store",
"./packages/http-sdk/**/*.ts": "npm run validate:types -w packages/http-sdk",
"./packages/logging/**/*.ts": "npm run validate:types -w packages/logging",
"./packages/drizzle-ability/**/*.ts": "npm run validate:types -w packages/drizzle-ability",
"./packages/database/**/*.ts": "npm run validate:types -w packages/database"
};