-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathlint-staged.config.mjs
More file actions
141 lines (119 loc) · 3.13 KB
/
lint-staged.config.mjs
File metadata and controls
141 lines (119 loc) · 3.13 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
import path from "node:path";
import { fileURLToPath } from "node:url";
import { existsSync } from "node:fs";
const repoRoot = path.dirname(fileURLToPath(import.meta.url));
const rootWorkspace = ".";
const eslintConfigFile = "eslint.config.mjs";
const codeExtensions = new Set([
".js",
".jsx",
".ts",
".tsx",
".mjs",
".cjs",
".mts",
".cts",
]);
function quote(file) {
return JSON.stringify(file);
}
function getWorkspaceDir(file) {
const normalized = file.split(path.sep);
if (
normalized.length >= 2 &&
(normalized[0] === "apps" || normalized[0] === "packages")
) {
return path.join(normalized[0], normalized[1]);
}
return ".";
}
function groupByWorkspace(files) {
const groups = new Map();
for (const file of files) {
const workspaceDir = getWorkspaceDir(file);
const workspaceFiles = groups.get(workspaceDir) ?? [];
workspaceFiles.push(file);
groups.set(workspaceDir, workspaceFiles);
}
return groups;
}
function getWorkspacePath(workspaceDir) {
return path.join(repoRoot, workspaceDir);
}
function hasWorkspaceEslintConfig(workspaceDir) {
if (workspaceDir === rootWorkspace) {
return false;
}
return existsSync(
path.join(getWorkspacePath(workspaceDir), eslintConfigFile),
);
}
function toWorkspaceRelativeFile(workspaceDir, file) {
return path.relative(
getWorkspacePath(workspaceDir),
path.join(repoRoot, file),
);
}
function getEslintCommandInput(workspaceDir, files) {
if (!hasWorkspaceEslintConfig(workspaceDir)) {
return {
cwd: rootWorkspace,
config: path.relative(
repoRoot,
path.join(getWorkspacePath(workspaceDir), eslintConfigFile),
),
files,
};
}
return {
cwd: workspaceDir,
config: eslintConfigFile,
files: files.map((file) => toWorkspaceRelativeFile(workspaceDir, file)),
};
}
function buildEslintCommands(files) {
const workspaceFiles = files.filter((file) =>
codeExtensions.has(path.extname(file)),
);
const commands = [];
for (const [workspaceDir, groupedFiles] of groupByWorkspace(workspaceFiles)) {
const {
cwd,
config,
files: eslintFiles,
} = getEslintCommandInput(workspaceDir, groupedFiles);
commands.push(
`pnpm --dir ${quote(cwd)} exec eslint --fix --config ${quote(config)} ${eslintFiles
.map(quote)
.join(" ")}`,
);
}
return commands;
}
function buildPrettierCommand(files) {
if (files.length === 0) {
return [];
}
return [`pnpm -w exec prettier --write ${files.map(quote).join(" ")}`];
}
function buildMediaImageCheckCommand(files) {
if (files.length === 0) {
return [];
}
return [
`pnpm --filter solana-com-media exec node scripts/check-images.mjs ${files
.map(quote)
.join(" ")}`,
];
}
export default {
"*.{js,jsx,ts,tsx,mjs,cjs,mts,cts}": (files) => [
...buildEslintCommands(files),
...buildPrettierCommand(files),
],
"*.{md,mdx,json,scss,yml,yaml}": buildPrettierCommand,
"apps/media/content/**/*.{png,jpg,jpeg,webp,avif}":
buildMediaImageCheckCommand,
"apps/media/public/uploads/posts/**/*.{png,jpg,jpeg,webp,avif}":
buildMediaImageCheckCommand,
};