-
-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy path.lintstagedrc.cjs
More file actions
137 lines (125 loc) · 4.82 KB
/
Copy path.lintstagedrc.cjs
File metadata and controls
137 lines (125 loc) · 4.82 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
const path = require('path');
const fs = require('fs');
const {quote: shellQuote} = require('shell-quote');
const ROOT = process.cwd();
function normalize(p) {
return p.split(path.sep).join('/');
}
// Parse the `packages:` list from pnpm-workspace.yaml. We only need the simple
// glob shapes pnpm allows here (`apps/*`, `ghost/*`, `e2e`); anything fancier
// would warrant a real YAML parser.
function loadWorkspacePatterns() {
const yaml = fs.readFileSync(path.join(ROOT, 'pnpm-workspace.yaml'), 'utf8');
const lines = yaml.split('\n');
const start = lines.findIndex(line => /^packages:\s*$/.test(line));
if (start === -1) {
return [];
}
const patterns = [];
for (let i = start + 1; i < lines.length; i++) {
const line = lines[i];
if (/^\s+-\s+/.test(line)) {
const match = line.match(/^\s+-\s+['"]?([^'"\s]+)['"]?\s*$/);
if (match) {
patterns.push(match[1]);
}
} else if (line.trim() !== '' && !/^\s/.test(line)) {
break;
}
}
return patterns;
}
function expandPattern(pattern) {
const segments = pattern.split('/');
let candidates = [''];
for (const segment of segments) {
const next = [];
for (const base of candidates) {
const dir = base ? path.join(ROOT, base) : ROOT;
if (segment === '*') {
if (!fs.existsSync(dir)) {
continue;
}
for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {
if (entry.isDirectory()) {
next.push(base ? `${base}/${entry.name}` : entry.name);
}
}
} else {
const candidate = base ? `${base}/${segment}` : segment;
if (fs.existsSync(path.join(ROOT, candidate))) {
next.push(candidate);
}
}
}
candidates = next;
}
return candidates;
}
const WORKSPACES = new Set(
loadWorkspacePatterns().flatMap(expandPattern)
);
function findWorkspace(file) {
let dir = path.dirname(path.resolve(file));
while (dir.startsWith(ROOT) && dir !== ROOT) {
const rel = normalize(path.relative(ROOT, dir));
if (WORKSPACES.has(rel)) {
return rel;
}
dir = path.dirname(dir);
}
return null;
}
function buildCommand(workspace, files) {
const base = workspace ? path.join(ROOT, workspace) : ROOT;
const relativeFiles = files
.map(file => normalize(path.relative(base, file)));
const dirArg = workspace ? `--dir ${shellQuote([workspace])} ` : '';
return `pnpm ${dirArg}exec eslint --cache -- ${shellQuote(relativeFiles)}`;
}
function buildBoundaryCommand(files) {
const relativeFiles = files
.map(file => normalize(path.relative(ROOT, file)));
return `pnpm exec depcruise --config .dependency-cruiser.cjs -- ${shellQuote(relativeFiles)}`;
}
function buildMarkdownCommands(files) {
const relativeFiles = files
.map(file => normalize(path.relative(ROOT, file)))
.filter(file => !file.startsWith('.changeset/'))
.filter(file => !file.split('/').some(part => part === 'fixture' || part === 'fixtures'));
if (relativeFiles.length === 0) {
return [];
}
const quotedFiles = shellQuote(relativeFiles);
return [
`pnpm exec markdownlint-cli2 --config .markdownlint-cli2.jsonc ${quotedFiles}`,
`pnpm exec remark --use remark-validate-links --frail --quiet --no-stdout ${quotedFiles}`
];
}
module.exports = {
'*.{js,ts,tsx,jsx,cjs}': (files) => {
const groups = new Map();
for (const file of files) {
const workspace = findWorkspace(file);
const key = workspace ?? '';
if (!groups.has(key)) {
groups.set(key, []);
}
groups.get(key).push(file);
}
return [...groups.entries()].map(([workspace, wsFiles]) =>
buildCommand(workspace || null, wsFiles)
);
},
'ghost/core/core/{server,shared,frontend}/**/*.{js,ts}': (files) =>
buildBoundaryCommand(files),
'apps/{shade,admin-x-framework,activitypub,portal,comments-ui,signup-form,sodo-search,announcement-bar,admin-toolbar}/src/**/*.{js,ts,tsx,jsx}': (files) =>
buildBoundaryCommand(files),
'**/*.md': buildMarkdownCommands,
'{**/AGENTS.md,scripts/check-agent-guidance.js}': () =>
'pnpm lint:agent-guidance',
'{.agents/skills/**,.claude/skills/**,scripts/check-agent-skill-links.js}': () =>
'pnpm lint:agent-skills',
'{package.json,pnpm-workspace.yaml,packages/**/package.json,packages/_template/**,scripts/check-internal-packages.js,scripts/create-package.js,scripts/lib/constants.js,scripts/lib/package-template.js}': () =>
'pnpm lint:packages'
};