@crown3/repo-rules 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +102 -1
- package/bin/repo-rules.mjs +9 -0
- package/dist/check-comments.d.ts +3 -0
- package/dist/check-docs.d.ts +13 -0
- package/dist/check-runs.d.ts +23 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +865 -0
- package/dist/comments.d.ts +17 -0
- package/dist/config.d.ts +85 -0
- package/dist/files.d.ts +6 -0
- package/dist/format-sh.d.ts +6 -0
- package/dist/g.js +120 -0
- package/dist/go-comments.d.ts +7 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +16 -0
- package/dist/review-comments.d.ts +14 -0
- package/dist/run-if-main.d.ts +2 -0
- package/dist/scrub-git-hook-env.d.ts +1 -0
- package/dist/scrub-git-hook-env.js +2 -0
- package/package.json +49 -3
package/dist/cli.js
ADDED
|
@@ -0,0 +1,865 @@
|
|
|
1
|
+
import node_path from "node:path";
|
|
2
|
+
import { pathToFileURL } from "node:url";
|
|
3
|
+
import { parseSync } from "oxc-parser";
|
|
4
|
+
import { parse as external_yaml_parse } from "yaml";
|
|
5
|
+
import { format as shfmt_format } from "@wasm-fmt/shfmt";
|
|
6
|
+
import { execFileSync } from "node:child_process";
|
|
7
|
+
import { ownChanges, isShell, repoFiles, ConfigError, settings as config_settings } from "./g.js";
|
|
8
|
+
import * as __rspack_external_node_fs_1b05aee1 from "node:fs";
|
|
9
|
+
const GO_QUOTES = new Set([
|
|
10
|
+
'"',
|
|
11
|
+
"'",
|
|
12
|
+
'`'
|
|
13
|
+
]);
|
|
14
|
+
const GO_BLANK = new Set([
|
|
15
|
+
' ',
|
|
16
|
+
'\t',
|
|
17
|
+
'\r'
|
|
18
|
+
]);
|
|
19
|
+
function skipQuoted(scan, quote) {
|
|
20
|
+
const { source } = scan;
|
|
21
|
+
for(scan.i += 1; scan.i < source.length && source[scan.i] !== quote; scan.i += 1)if ('\\' === source[scan.i] && '`' !== quote) scan.i += 1;
|
|
22
|
+
else if ('\n' === source[scan.i]) scan.line += 1;
|
|
23
|
+
scan.i += 1;
|
|
24
|
+
scan.lineHasCode = true;
|
|
25
|
+
}
|
|
26
|
+
function lineComment(scan) {
|
|
27
|
+
const end = scan.source.indexOf('\n', scan.i);
|
|
28
|
+
const stop = -1 === end ? scan.source.length : end;
|
|
29
|
+
scan.found.push({
|
|
30
|
+
line: scan.line,
|
|
31
|
+
text: scan.source.slice(scan.i + 2, stop),
|
|
32
|
+
chains: !scan.lineHasCode
|
|
33
|
+
});
|
|
34
|
+
scan.i = stop;
|
|
35
|
+
}
|
|
36
|
+
function blockComment(scan) {
|
|
37
|
+
const end = scan.source.indexOf('*/', scan.i + 2);
|
|
38
|
+
const stop = -1 === end ? scan.source.length : end;
|
|
39
|
+
scan.found.push({
|
|
40
|
+
line: scan.line,
|
|
41
|
+
text: scan.source.slice(scan.i + 2, stop).trim(),
|
|
42
|
+
chains: false
|
|
43
|
+
});
|
|
44
|
+
scan.line += scan.source.slice(scan.i, stop).split('\n').length - 1;
|
|
45
|
+
scan.i = stop + 2;
|
|
46
|
+
scan.lineHasCode = true;
|
|
47
|
+
}
|
|
48
|
+
function goStep(scan) {
|
|
49
|
+
const ch = scan.source[scan.i] ?? '';
|
|
50
|
+
const pair = scan.source.slice(scan.i, scan.i + 2);
|
|
51
|
+
if ('\n' === ch) {
|
|
52
|
+
scan.line += 1;
|
|
53
|
+
scan.lineHasCode = false;
|
|
54
|
+
scan.i += 1;
|
|
55
|
+
} else if ('//' === pair) lineComment(scan);
|
|
56
|
+
else if ('/*' === pair) blockComment(scan);
|
|
57
|
+
else if (GO_QUOTES.has(ch)) skipQuoted(scan, ch);
|
|
58
|
+
else {
|
|
59
|
+
if (!GO_BLANK.has(ch)) scan.lineHasCode = true;
|
|
60
|
+
scan.i += 1;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function goComments(source) {
|
|
64
|
+
const scan = {
|
|
65
|
+
source,
|
|
66
|
+
i: 0,
|
|
67
|
+
line: 1,
|
|
68
|
+
lineHasCode: false,
|
|
69
|
+
found: []
|
|
70
|
+
};
|
|
71
|
+
while(scan.i < source.length)goStep(scan);
|
|
72
|
+
return scan.found;
|
|
73
|
+
}
|
|
74
|
+
const EXTERNAL = /^external\([^)\s][^)]*\): \S/;
|
|
75
|
+
const DIRECTIVE = /^(biome-ignore|shellcheck |\/ <reference )/;
|
|
76
|
+
const GO_DIRECTIVE = /^(go:|nolint:)/;
|
|
77
|
+
const SCRIPT = /\.(ts|mts|cts|tsx|js|mjs|cjs|jsx)$/;
|
|
78
|
+
const GENERATED = /^\/\/ Code generated .* DO NOT EDIT\.$/m;
|
|
79
|
+
function externalLines(comments) {
|
|
80
|
+
const lines = new Set();
|
|
81
|
+
let next;
|
|
82
|
+
for (const c of comments){
|
|
83
|
+
const external = EXTERNAL.test(c.text) || c.chains && c.line === next;
|
|
84
|
+
next = external && c.chains ? c.line + 1 : void 0;
|
|
85
|
+
if (external) lines.add(c.line);
|
|
86
|
+
}
|
|
87
|
+
return lines;
|
|
88
|
+
}
|
|
89
|
+
function disallowed(comments) {
|
|
90
|
+
const external = externalLines(comments);
|
|
91
|
+
return comments.filter((c)=>!external.has(c.line) && !c.directive && !c.publicDoc).map((c)=>c.line);
|
|
92
|
+
}
|
|
93
|
+
const lineAt = (source, offset)=>source.slice(0, offset).split('\n').length;
|
|
94
|
+
const ownLine = (source, offset)=>!/[^ \t]/.test(source.slice(source.lastIndexOf('\n', offset) + 1, offset));
|
|
95
|
+
function members(declaration) {
|
|
96
|
+
if (declaration?.type === 'TSInterfaceDeclaration') return declaration.body.body;
|
|
97
|
+
if (declaration?.type === 'TSTypeAliasDeclaration' && 'TSTypeLiteral' === declaration.typeAnnotation.type) return declaration.typeAnnotation.members;
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
function publicStarts(program) {
|
|
101
|
+
const starts = new Set();
|
|
102
|
+
for (const node of program.body)if ('ExportNamedDeclaration' === node.type || 'ExportDefaultDeclaration' === node.type) {
|
|
103
|
+
starts.add(node.start);
|
|
104
|
+
for (const member of members('ExportNamedDeclaration' === node.type ? node.declaration : null))starts.add(member.start);
|
|
105
|
+
}
|
|
106
|
+
return starts;
|
|
107
|
+
}
|
|
108
|
+
const afterHashbang = (program, comments)=>comments.filter((c)=>c.start >= (program.hashbang?.end || 0));
|
|
109
|
+
function scriptComments(file, source, publicApi) {
|
|
110
|
+
const { program, comments, errors } = parseSync(file, source);
|
|
111
|
+
const [error] = errors;
|
|
112
|
+
if (error) throw new Error(error.message);
|
|
113
|
+
const documented = publicApi ? publicStarts(program) : new Set();
|
|
114
|
+
const documents = (end)=>{
|
|
115
|
+
const rest = source.slice(end);
|
|
116
|
+
return documented.has(end + rest.length - rest.trimStart().length);
|
|
117
|
+
};
|
|
118
|
+
return afterHashbang(program, comments).map((c)=>({
|
|
119
|
+
line: lineAt(source, c.start),
|
|
120
|
+
text: c.value.trim(),
|
|
121
|
+
chains: 'Line' === c.type && ownLine(source, c.start),
|
|
122
|
+
directive: DIRECTIVE.test(c.value.trim()),
|
|
123
|
+
publicDoc: 'Block' === c.type && c.value.startsWith('*') && documents(c.end)
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
const SHELL_TOKEN = /'[^']*'|"(?:\\.|[^"\\])*"|\\.|(?<=^|\s)#/g;
|
|
127
|
+
function commentStart(line) {
|
|
128
|
+
for (const m of line.matchAll(SHELL_TOKEN))if ('#' === m[0]) return m.index;
|
|
129
|
+
return -1;
|
|
130
|
+
}
|
|
131
|
+
function shellComments(source) {
|
|
132
|
+
const found = [];
|
|
133
|
+
let heredoc = '';
|
|
134
|
+
source.split('\n').forEach((line, index)=>{
|
|
135
|
+
if (heredoc) {
|
|
136
|
+
if (line.trim() === heredoc) heredoc = '';
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (0 === index && line.startsWith('#!')) return;
|
|
140
|
+
const start = commentStart(line);
|
|
141
|
+
if (start >= 0) {
|
|
142
|
+
const text = line.slice(start + 1).trim();
|
|
143
|
+
found.push({
|
|
144
|
+
line: index + 1,
|
|
145
|
+
text,
|
|
146
|
+
chains: !line.slice(0, start).trim(),
|
|
147
|
+
directive: DIRECTIVE.test(text),
|
|
148
|
+
publicDoc: false
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
const code = start >= 0 ? line.slice(0, start) : line;
|
|
152
|
+
heredoc = code.match(/<<-?\s*['"]?(\w+)['"]?/)?.[1] || '';
|
|
153
|
+
});
|
|
154
|
+
return found;
|
|
155
|
+
}
|
|
156
|
+
const goFile = (source)=>goComments(source).map((c)=>({
|
|
157
|
+
line: c.line,
|
|
158
|
+
text: c.text.trim(),
|
|
159
|
+
chains: c.chains,
|
|
160
|
+
directive: GO_DIRECTIVE.test(c.text),
|
|
161
|
+
publicDoc: false
|
|
162
|
+
}));
|
|
163
|
+
function commentsOf(file, source, publicApi) {
|
|
164
|
+
if (file.endsWith('.go')) return goFile(source);
|
|
165
|
+
if (SCRIPT.test(file)) return scriptComments(file, source, publicApi);
|
|
166
|
+
if (isShell(file, source.split('\n', 1)[0] || '')) return shellComments(source);
|
|
167
|
+
}
|
|
168
|
+
function disallowedLines(file, source, publicApi = false) {
|
|
169
|
+
if (GENERATED.test(source)) return [];
|
|
170
|
+
const comments = commentsOf(file, source, publicApi);
|
|
171
|
+
return comments ? disallowed(comments) : [];
|
|
172
|
+
}
|
|
173
|
+
const EXCLUDED = /(^|\/)(node_modules|fixtures)\//;
|
|
174
|
+
const PACKAGE_SRC = /^packages\/([^/]+)\/src\//;
|
|
175
|
+
const MESSAGE = 'comment not allowed: rename or extract so the code says it, move the constraint to docs/, or mark external(<dependency>): why';
|
|
176
|
+
function isPublicPackage(root, file) {
|
|
177
|
+
const name = file.match(PACKAGE_SRC)?.[1];
|
|
178
|
+
if (!name) return false;
|
|
179
|
+
const manifest = node_path.join(root, 'packages', name, 'package.json');
|
|
180
|
+
if (!__rspack_external_node_fs_1b05aee1.existsSync(manifest)) return false;
|
|
181
|
+
return true !== JSON.parse(__rspack_external_node_fs_1b05aee1.readFileSync(manifest, 'utf8')).private;
|
|
182
|
+
}
|
|
183
|
+
const isFile = (full)=>__rspack_external_node_fs_1b05aee1.statSync(full, {
|
|
184
|
+
throwIfNoEntry: false
|
|
185
|
+
})?.isFile() === true;
|
|
186
|
+
function checkComments(root, files, { exclude, publicApi }) {
|
|
187
|
+
const errors = [];
|
|
188
|
+
const skipped = (file)=>[
|
|
189
|
+
EXCLUDED,
|
|
190
|
+
...exclude
|
|
191
|
+
].some((r)=>r.test(file));
|
|
192
|
+
const isPublicApi = (file)=>publicApi.some((dir)=>file.startsWith(dir)) || isPublicPackage(root, file);
|
|
193
|
+
for (const file of files){
|
|
194
|
+
const full = node_path.join(root, file);
|
|
195
|
+
if (skipped(file) || !isFile(full)) continue;
|
|
196
|
+
const source = __rspack_external_node_fs_1b05aee1.readFileSync(full, 'utf8');
|
|
197
|
+
try {
|
|
198
|
+
for (const line of disallowedLines(file, source, isPublicApi(file)))errors.push(`${file}:${line}: ${MESSAGE}`);
|
|
199
|
+
} catch (error) {
|
|
200
|
+
errors.push(`${file}: cannot parse: ${error.message}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return errors;
|
|
204
|
+
}
|
|
205
|
+
const HEADING = '## Where checks run';
|
|
206
|
+
const HOOK_ENTRY = /^pnpm (\S+)[^\n]*$/;
|
|
207
|
+
const HOOK_HINT = "call a package.json script as `pnpm <script>` instead";
|
|
208
|
+
const check_runs_text = (file)=>__rspack_external_node_fs_1b05aee1.existsSync(file) ? __rspack_external_node_fs_1b05aee1.readFileSync(file, 'utf8') : '';
|
|
209
|
+
const isObject = (value)=>'object' == typeof value && null !== value;
|
|
210
|
+
const field = (value, key)=>isObject(value) && key in value ? value[key] : void 0;
|
|
211
|
+
const list = (value)=>Array.isArray(value) ? value : [];
|
|
212
|
+
function collect(value, keys, found) {
|
|
213
|
+
if (Array.isArray(value)) {
|
|
214
|
+
for (const item of value)collect(item, keys, found);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (!isObject(value)) return;
|
|
218
|
+
for (const [key, child] of Object.entries(value))if ('run' === key && 'string' == typeof child) found.push({
|
|
219
|
+
path: keys,
|
|
220
|
+
command: child
|
|
221
|
+
});
|
|
222
|
+
else collect(child, [
|
|
223
|
+
...keys,
|
|
224
|
+
key
|
|
225
|
+
], found);
|
|
226
|
+
}
|
|
227
|
+
function check_runs_runs(yaml) {
|
|
228
|
+
const found = [];
|
|
229
|
+
collect(external_yaml_parse(yaml), [], found);
|
|
230
|
+
return found;
|
|
231
|
+
}
|
|
232
|
+
function commands(yaml) {
|
|
233
|
+
const jobs = field(external_yaml_parse(yaml), 'jobs');
|
|
234
|
+
return Object.entries(isObject(jobs) ? jobs : {}).flatMap(([job, body])=>list(field(body, 'steps')).flatMap((step)=>list(field(step, 'commands')).filter((c)=>'string' == typeof c).map((command)=>({
|
|
235
|
+
path: [
|
|
236
|
+
'jobs',
|
|
237
|
+
job
|
|
238
|
+
],
|
|
239
|
+
command
|
|
240
|
+
}))));
|
|
241
|
+
}
|
|
242
|
+
const COMMAND = /^(check|fix|check_diff|check_list_files)\s*=\s*(.*)$/;
|
|
243
|
+
const STRING = /^"((?:[^"\\]|\\.)*)"$/;
|
|
244
|
+
const OPEN = /^(?:\["([^"]+)"\]|(\w+))\s*\{$/;
|
|
245
|
+
const inStep = (stack)=>4 === stack.length && 'hooks' === stack[0] && 'steps' === stack[2];
|
|
246
|
+
function commandOf(line, stack) {
|
|
247
|
+
const [, key, value = ''] = line.match(COMMAND) ?? [];
|
|
248
|
+
if (void 0 === key || /^(true|false)$/.test(value)) return;
|
|
249
|
+
if (!inStep(stack)) throw new Error(`${key} must sit in a step under hooks { ["<hook>"] { steps { ["<step>"] { … } } } }`);
|
|
250
|
+
const body = value.match(STRING)?.[1];
|
|
251
|
+
if (void 0 === body) throw new Error(`write ${key} as a one-line "…" string`);
|
|
252
|
+
return JSON.parse(`"${body}"`);
|
|
253
|
+
}
|
|
254
|
+
function readLine(line, stack) {
|
|
255
|
+
const open = line.match(OPEN);
|
|
256
|
+
if (open) stack.push(open[1] ?? open[2] ?? '');
|
|
257
|
+
else if ('}' === line) stack.pop();
|
|
258
|
+
else if (line.startsWith('[') || line.endsWith('{')) throw new Error('put each block on its own lines: name {, then }');
|
|
259
|
+
else {
|
|
260
|
+
const command = commandOf(line, stack);
|
|
261
|
+
if (void 0 !== command) return {
|
|
262
|
+
path: stack.slice(1),
|
|
263
|
+
command
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
function hookRuns(pkl) {
|
|
268
|
+
const found = [];
|
|
269
|
+
const stack = [];
|
|
270
|
+
for (const [i, raw] of pkl.split('\n').entries()){
|
|
271
|
+
const line = raw.trim();
|
|
272
|
+
if (!('' === line || line.startsWith('//'))) try {
|
|
273
|
+
const run = readLine(line, stack);
|
|
274
|
+
if (run) found.push(run);
|
|
275
|
+
} catch (error) {
|
|
276
|
+
throw new Error(`line ${i + 1}: ${error.message}`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return found;
|
|
280
|
+
}
|
|
281
|
+
function table(markdown) {
|
|
282
|
+
const section = markdown.split(HEADING)[1]?.split(/^## /m)[0] ?? '';
|
|
283
|
+
const names = (cell = '')=>[
|
|
284
|
+
...cell.matchAll(/`([^`]+)`/g)
|
|
285
|
+
].map((m)=>m[1] ?? '').sort();
|
|
286
|
+
const rows = new Map();
|
|
287
|
+
for (const line of section.split('\n')){
|
|
288
|
+
const cells = line.split('|').slice(1, -1);
|
|
289
|
+
const script = cells[0]?.trim().match(/^`([^`]+)`$/)?.[1];
|
|
290
|
+
if (script) rows.set(script, {
|
|
291
|
+
hooks: names(cells[1]),
|
|
292
|
+
jobs: names(cells[2])
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
return rows;
|
|
296
|
+
}
|
|
297
|
+
function parsedOr(errors, file, format, parse, fallback) {
|
|
298
|
+
try {
|
|
299
|
+
return parse();
|
|
300
|
+
} catch (error) {
|
|
301
|
+
const [first = ''] = String(error.message).split('\n');
|
|
302
|
+
errors.push(`${file}: not valid ${format}: ${first.replace(/:$/, '')}`);
|
|
303
|
+
return fallback;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
function pipelineFiles(root, patterns) {
|
|
307
|
+
return [
|
|
308
|
+
...new Set(patterns.flatMap((p)=>__rspack_external_node_fs_1b05aee1.globSync(p, {
|
|
309
|
+
cwd: root
|
|
310
|
+
}).sort()))
|
|
311
|
+
];
|
|
312
|
+
}
|
|
313
|
+
function describeFiles(patterns) {
|
|
314
|
+
if (1 === patterns.length) return patterns[0] ?? '';
|
|
315
|
+
const dirs = new Set(patterns.map((p)=>node_path.posix.dirname(p)));
|
|
316
|
+
const exts = new Set(patterns.map((p)=>node_path.posix.extname(p)));
|
|
317
|
+
const [dir] = dirs;
|
|
318
|
+
const [ext = ''] = exts;
|
|
319
|
+
if (1 !== dirs.size || 1 !== exts.size) return patterns.join(', ');
|
|
320
|
+
const names = patterns.map((p)=>node_path.posix.basename(p, ext));
|
|
321
|
+
return `${dir}/{${names.join(',')}}${ext}`;
|
|
322
|
+
}
|
|
323
|
+
function sites(root, config, errors) {
|
|
324
|
+
const read = (file, format, find)=>parsedOr(errors, file, format, ()=>find(check_runs_text(node_path.join(root, file))), []);
|
|
325
|
+
const hooks = read(config.hooks, 'hk config', hookRuns).map((r)=>({
|
|
326
|
+
file: config.hooks,
|
|
327
|
+
label: r.path.join('.'),
|
|
328
|
+
kind: 'hooks',
|
|
329
|
+
where: r.path[0] ?? '',
|
|
330
|
+
command: r.command,
|
|
331
|
+
script: r.command.trimEnd().match(HOOK_ENTRY)?.[1]
|
|
332
|
+
}));
|
|
333
|
+
const { ci } = config;
|
|
334
|
+
const find = 'github' === ci.platform ? check_runs_runs : commands;
|
|
335
|
+
const jobs = pipelineFiles(root, ci.files).flatMap((file)=>read(file, 'YAML', find).map((r)=>({
|
|
336
|
+
file,
|
|
337
|
+
r
|
|
338
|
+
}))).filter(({ r })=>'jobs' === r.path[0]).map(({ file, r })=>({
|
|
339
|
+
file,
|
|
340
|
+
label: r.path.slice(0, 2).join('.'),
|
|
341
|
+
kind: 'jobs',
|
|
342
|
+
where: r.path[1] ?? '',
|
|
343
|
+
command: r.command,
|
|
344
|
+
script: r.command.trim().match(ci.entry)?.[1]
|
|
345
|
+
})).filter((site)=>!ci.setup.includes(site.script ?? ''));
|
|
346
|
+
return [
|
|
347
|
+
...hooks,
|
|
348
|
+
...jobs
|
|
349
|
+
];
|
|
350
|
+
}
|
|
351
|
+
const check_runs_short = (command)=>{
|
|
352
|
+
const words = command.trim().split(/\s+/);
|
|
353
|
+
return words.length > 3 ? `${words.slice(0, 3).join(' ')} …` : command;
|
|
354
|
+
};
|
|
355
|
+
const check_runs_names = (items)=>items.map((n)=>`\`${n}\``).join(', ') || 'nothing';
|
|
356
|
+
const where = (entry)=>[
|
|
357
|
+
entry.hooks.length && `hk ${check_runs_names(entry.hooks)}`,
|
|
358
|
+
entry.jobs.length && `CI job ${check_runs_names(entry.jobs)}`
|
|
359
|
+
].filter(Boolean).join(' and ');
|
|
360
|
+
const NONE = {
|
|
361
|
+
hooks: [],
|
|
362
|
+
jobs: []
|
|
363
|
+
};
|
|
364
|
+
function invoked(root, config, scripts, errors) {
|
|
365
|
+
const actual = new Map();
|
|
366
|
+
for (const site of sites(root, config, errors)){
|
|
367
|
+
const { script } = site;
|
|
368
|
+
if (void 0 === script || !Object.hasOwn(scripts, script)) {
|
|
369
|
+
const hint = 'hooks' === site.kind ? HOOK_HINT : config.ci.hint;
|
|
370
|
+
errors.push(`${site.file}: ${site.label} runs \`${check_runs_short(site.command)}\`; ${hint}`);
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
const entry = actual.get(script) ?? {
|
|
374
|
+
hooks: [],
|
|
375
|
+
jobs: []
|
|
376
|
+
};
|
|
377
|
+
entry[site.kind].push(site.where);
|
|
378
|
+
actual.set(script, entry);
|
|
379
|
+
}
|
|
380
|
+
return actual;
|
|
381
|
+
}
|
|
382
|
+
function checkRow(errors, config, script, row, entry) {
|
|
383
|
+
for (const [kind, file, column] of [
|
|
384
|
+
[
|
|
385
|
+
'hooks',
|
|
386
|
+
config.hooks,
|
|
387
|
+
'hk'
|
|
388
|
+
],
|
|
389
|
+
[
|
|
390
|
+
'jobs',
|
|
391
|
+
describeFiles(config.ci.files),
|
|
392
|
+
'CI job'
|
|
393
|
+
]
|
|
394
|
+
]){
|
|
395
|
+
const want = [
|
|
396
|
+
...new Set(entry[kind])
|
|
397
|
+
].sort();
|
|
398
|
+
if (want.join() !== row[kind].join()) errors.push(`${config.table}: Where checks run table row \`${script}\` lists ${column} ${check_runs_names(row[kind])}; ${file} runs it in ${check_runs_names(want)}`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
function packageScripts(pkg, errors) {
|
|
402
|
+
const parsed = parsedOr(errors, 'package.json', 'JSON', ()=>pkg ? JSON.parse(pkg) : {}, {});
|
|
403
|
+
const scripts = field(parsed, "scripts");
|
|
404
|
+
return isObject(scripts) ? scripts : {};
|
|
405
|
+
}
|
|
406
|
+
function checkRuns(root, config) {
|
|
407
|
+
const errors = [];
|
|
408
|
+
const scripts = packageScripts(check_runs_text(node_path.join(root, 'package.json')), errors);
|
|
409
|
+
const rows = table(check_runs_text(node_path.join(root, config.table)));
|
|
410
|
+
const actual = invoked(root, config, scripts, errors);
|
|
411
|
+
for (const [script, entry] of actual)if (!rows.has(script)) errors.push(`${config.table}: script \`${script}\` is run by ${where(entry)} but is missing from the Where checks run table`);
|
|
412
|
+
for (const [script, row] of rows)if (Object.hasOwn(scripts, script)) checkRow(errors, config, script, row, actual.get(script) ?? NONE);
|
|
413
|
+
else errors.push(`${config.table}: Where checks run table row \`${script}\` names a script package.json does not define`);
|
|
414
|
+
return errors;
|
|
415
|
+
}
|
|
416
|
+
const TOP = [
|
|
417
|
+
'AGENTS.md',
|
|
418
|
+
'GLOSSARY-MAP.md',
|
|
419
|
+
'GLOSSARY.md',
|
|
420
|
+
'LESSONS.md',
|
|
421
|
+
'README.md'
|
|
422
|
+
];
|
|
423
|
+
const PER_WORKSPACE = [
|
|
424
|
+
'AGENTS.md',
|
|
425
|
+
'GLOSSARY.md',
|
|
426
|
+
'README.md'
|
|
427
|
+
];
|
|
428
|
+
const ENGLISH = new Set([
|
|
429
|
+
'AGENTS.md',
|
|
430
|
+
'GLOSSARY-MAP.md',
|
|
431
|
+
'GLOSSARY.md',
|
|
432
|
+
'LESSONS.md'
|
|
433
|
+
]);
|
|
434
|
+
const CJK = /[\u{3000}-\u{30ff}\u{3400}-\u{9fff}\u{ac00}-\u{d7af}\u{ff00}-\u{ffef}]/u;
|
|
435
|
+
const SKIP = [
|
|
436
|
+
'node_modules',
|
|
437
|
+
'coverage',
|
|
438
|
+
'.metrics',
|
|
439
|
+
'target',
|
|
440
|
+
'dist',
|
|
441
|
+
'log',
|
|
442
|
+
'output',
|
|
443
|
+
'.ci-cache',
|
|
444
|
+
'.eden-mono'
|
|
445
|
+
];
|
|
446
|
+
const AGENT_FILES = new Set([
|
|
447
|
+
'AGENTS.md',
|
|
448
|
+
'CLAUDE.md'
|
|
449
|
+
]);
|
|
450
|
+
const SKILL_TREES = new Set([
|
|
451
|
+
'skills',
|
|
452
|
+
'.agents/skills',
|
|
453
|
+
'.claude/skills'
|
|
454
|
+
]);
|
|
455
|
+
const SHARED_WORDS = 10;
|
|
456
|
+
const WORD = /`[^`\n]*`|[a-z0-9]+(?:['.-][a-z0-9]+)*/g;
|
|
457
|
+
const check_docs_HEADING = /^#{1,6}[ \t]+(.*?)(?:[ \t]+#+)?[ \t]*$/gm;
|
|
458
|
+
const HTML_ANCHOR = /<a\s[^>]*?\b(?:id|name)="([^"]+)"/g;
|
|
459
|
+
const LINK = /\[[^\]\n]*\]\([^)\s]*(?:\s+"[^"]*")?\)/;
|
|
460
|
+
const UNREACHED = 'no link chain from AGENTS.md reaches it; link it from AGENTS.md or a page it links to';
|
|
461
|
+
const check_docs_text = (file)=>__rspack_external_node_fs_1b05aee1.existsSync(file) ? __rspack_external_node_fs_1b05aee1.readFileSync(file, 'utf8') : '';
|
|
462
|
+
const byName = (a, b)=>a.name.localeCompare(b.name);
|
|
463
|
+
function walk(ctx, dir) {
|
|
464
|
+
const full = node_path.join(ctx.root, dir);
|
|
465
|
+
if (!__rspack_external_node_fs_1b05aee1.existsSync(full)) return [];
|
|
466
|
+
return __rspack_external_node_fs_1b05aee1.readdirSync(full, {
|
|
467
|
+
withFileTypes: true
|
|
468
|
+
}).filter((d)=>!ctx.skip.has(d.name)).sort(byName).flatMap((d)=>{
|
|
469
|
+
const rel = node_path.join(dir, d.name);
|
|
470
|
+
if (d.isDirectory()) return walk(ctx, rel);
|
|
471
|
+
return d.name.endsWith('.md') ? [
|
|
472
|
+
rel
|
|
473
|
+
] : [];
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
function workspaceDirs(ctx) {
|
|
477
|
+
return ctx.workspaces.flatMap((parent)=>{
|
|
478
|
+
const full = node_path.join(ctx.root, parent);
|
|
479
|
+
if (!__rspack_external_node_fs_1b05aee1.existsSync(full)) return [];
|
|
480
|
+
return __rspack_external_node_fs_1b05aee1.readdirSync(full, {
|
|
481
|
+
withFileTypes: true
|
|
482
|
+
}).filter((d)=>d.isDirectory() && !ctx.skip.has(d.name)).sort(byName).map((d)=>node_path.join(parent, d.name));
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
const present = (root, files)=>files.filter((f)=>__rspack_external_node_fs_1b05aee1.existsSync(node_path.join(root, f)));
|
|
486
|
+
function agentDocs(ctx) {
|
|
487
|
+
const workspaces = ctx.docs.workspaceDocs ? workspaceDirs(ctx) : [];
|
|
488
|
+
return [
|
|
489
|
+
...present(ctx.root, TOP),
|
|
490
|
+
...walk(ctx, 'docs'),
|
|
491
|
+
...workspaces.flatMap((dir)=>[
|
|
492
|
+
...present(ctx.root, PER_WORKSPACE.map((f)=>node_path.join(dir, f))),
|
|
493
|
+
...walk(ctx, node_path.join(dir, 'docs'))
|
|
494
|
+
])
|
|
495
|
+
];
|
|
496
|
+
}
|
|
497
|
+
function unfenced(markdown) {
|
|
498
|
+
return markdown.replace(/^(```|~~~)[^\n]*\n[\s\S]*?^\1[^\n]*$/gm, (block)=>block.replace(/[^\n]/g, ''));
|
|
499
|
+
}
|
|
500
|
+
function prose(markdown) {
|
|
501
|
+
return unfenced(markdown).replace(/`[^`\n]*`/g, '');
|
|
502
|
+
}
|
|
503
|
+
function links(markdown) {
|
|
504
|
+
return [
|
|
505
|
+
...prose(markdown).matchAll(/\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g)
|
|
506
|
+
].map((m)=>m[1] ?? '');
|
|
507
|
+
}
|
|
508
|
+
const check_docs_external = (target)=>/^([a-z][a-z0-9+.-]*:|#)/i.test(target);
|
|
509
|
+
function slug(heading) {
|
|
510
|
+
return heading.replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1').replace(/<[^>]+>/g, '').toLowerCase().replace(/[^\p{L}\p{M}\p{N}\p{Pc} -]/gu, '').replace(/ /g, '-');
|
|
511
|
+
}
|
|
512
|
+
function check_docs_anchors(markdown) {
|
|
513
|
+
const out = new Set();
|
|
514
|
+
const seen = new Map();
|
|
515
|
+
for (const [, heading = ''] of unfenced(markdown).matchAll(check_docs_HEADING)){
|
|
516
|
+
const base = slug(heading);
|
|
517
|
+
const n = seen.get(base) ?? 0;
|
|
518
|
+
seen.set(base, n + 1);
|
|
519
|
+
out.add(n ? `${base}-${n}` : base);
|
|
520
|
+
}
|
|
521
|
+
for (const [, id = ''] of markdown.matchAll(HTML_ANCHOR))out.add(id);
|
|
522
|
+
return out;
|
|
523
|
+
}
|
|
524
|
+
function decoded(fragment) {
|
|
525
|
+
try {
|
|
526
|
+
return decodeURIComponent(fragment);
|
|
527
|
+
} catch {
|
|
528
|
+
return fragment;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
const withoutQuery = (target)=>target.replace(/[?#].*$/, '');
|
|
532
|
+
function linkProblem(root, file, target) {
|
|
533
|
+
const hash = target.indexOf('#');
|
|
534
|
+
const local = withoutQuery(target);
|
|
535
|
+
const resolved = 0 === hash ? node_path.join(root, file) : node_path.resolve(node_path.dirname(node_path.join(root, file)), local);
|
|
536
|
+
if (!__rspack_external_node_fs_1b05aee1.existsSync(resolved)) return `link to ${target} does not resolve`;
|
|
537
|
+
if (hash < 0 || !resolved.endsWith('.md')) return '';
|
|
538
|
+
const anchor = decoded(target.slice(hash + 1));
|
|
539
|
+
return check_docs_anchors(check_docs_text(resolved)).has(anchor) ? '' : `link to ${target}: no heading ${anchor} in ${local || file}`;
|
|
540
|
+
}
|
|
541
|
+
function unlinkedDir(ctx, file, target) {
|
|
542
|
+
const rel = node_path.relative(ctx.root, node_path.resolve(node_path.dirname(node_path.join(ctx.root, file)), withoutQuery(target)));
|
|
543
|
+
return ctx.docs.unlinked.find((dir)=>rel === dir || rel.startsWith(`${dir}${node_path.sep}`));
|
|
544
|
+
}
|
|
545
|
+
function checkLinks(ctx, file) {
|
|
546
|
+
for (const target of links(check_docs_text(node_path.join(ctx.root, file)))){
|
|
547
|
+
if (check_docs_external(target) && !target.startsWith('#')) continue;
|
|
548
|
+
const dir = unlinkedDir(ctx, file, target);
|
|
549
|
+
const problem = dir ? `link to ${target} points into ${dir}/, which check:docs does not read; write the fact into its owning doc instead (docs/subsystems/README.md#archives-and-scratch)` : linkProblem(ctx.root, file, target);
|
|
550
|
+
if (problem) ctx.fail(file, problem);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
function linked(root, file) {
|
|
554
|
+
return links(check_docs_text(node_path.join(root, file))).filter((target)=>!check_docs_external(target)).map((target)=>node_path.normalize(node_path.join(node_path.dirname(file), withoutQuery(target)))).filter((rel)=>rel.endsWith('.md'));
|
|
555
|
+
}
|
|
556
|
+
function checkReachable(ctx, files) {
|
|
557
|
+
const seen = new Set();
|
|
558
|
+
const queue = __rspack_external_node_fs_1b05aee1.existsSync(node_path.join(ctx.root, 'AGENTS.md')) ? [
|
|
559
|
+
'AGENTS.md'
|
|
560
|
+
] : [];
|
|
561
|
+
for(let file = queue.shift(); void 0 !== file; file = queue.shift())if (!seen.has(file)) {
|
|
562
|
+
seen.add(file);
|
|
563
|
+
queue.push(...linked(ctx.root, file));
|
|
564
|
+
}
|
|
565
|
+
for (const file of files)if ('README.md' !== node_path.basename(file) && !seen.has(file)) ctx.fail(file, UNREACHED);
|
|
566
|
+
}
|
|
567
|
+
function workspaceRoot(ctx, dir) {
|
|
568
|
+
const [parent = '', name, ...rest] = dir.split('/');
|
|
569
|
+
return ctx.workspaces.includes(parent) && void 0 !== name && !rest.length;
|
|
570
|
+
}
|
|
571
|
+
function strayAgentFiles(ctx, dir = '') {
|
|
572
|
+
const allowed = (at)=>!at || 'workspaces' === ctx.docs.agentFiles && workspaceRoot(ctx, at);
|
|
573
|
+
return __rspack_external_node_fs_1b05aee1.readdirSync(node_path.join(ctx.root, dir), {
|
|
574
|
+
withFileTypes: true
|
|
575
|
+
}).filter((d)=>!ctx.skip.has(d.name) && '.git' !== d.name).sort(byName).flatMap((d)=>{
|
|
576
|
+
const rel = dir ? `${dir}/${d.name}` : d.name;
|
|
577
|
+
if (d.isDirectory()) return SKILL_TREES.has(rel) ? [] : strayAgentFiles(ctx, rel);
|
|
578
|
+
return AGENT_FILES.has(d.name) && !allowed(dir) ? [
|
|
579
|
+
rel
|
|
580
|
+
] : [];
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
const STRAY = {
|
|
584
|
+
root: 'agent knowledge lives in the root AGENTS.md, LESSONS.md and docs/ (docs/subsystems/README.md#one-fact-one-place); move its content to the owning page and delete it',
|
|
585
|
+
workspaces: 'agent knowledge lives in the root AGENTS.md, LESSONS.md and docs/, or in an app or package AGENTS.md (docs/subsystems/README.md#one-fact-one-place); move its content to the owning page and delete it'
|
|
586
|
+
};
|
|
587
|
+
function checkStrayAgentFiles(ctx) {
|
|
588
|
+
const { agentFiles } = ctx.docs;
|
|
589
|
+
if ('anywhere' === agentFiles) return;
|
|
590
|
+
for (const file of strayAgentFiles(ctx))ctx.fail(file, STRAY[agentFiles]);
|
|
591
|
+
}
|
|
592
|
+
const english = (file)=>ENGLISH.has(node_path.basename(file)) || file.split(node_path.sep).includes('docs');
|
|
593
|
+
function checkEnglish(ctx, file) {
|
|
594
|
+
const line = prose(check_docs_text(node_path.join(ctx.root, file))).split('\n').findIndex((l)=>CJK.test(l));
|
|
595
|
+
if (line >= 0) ctx.fail(file, `line ${line + 1} has CJK text outside code; write it in English, or quote it in backticks as data`);
|
|
596
|
+
}
|
|
597
|
+
function phrases(markdown) {
|
|
598
|
+
const out = new Set();
|
|
599
|
+
for (const run of unfenced(markdown).toLowerCase().split(LINK)){
|
|
600
|
+
const words = [
|
|
601
|
+
...run.matchAll(WORD)
|
|
602
|
+
].map((m)=>m[0]);
|
|
603
|
+
for(let i = 0; i + SHARED_WORDS <= words.length; i++)out.add(words.slice(i, i + SHARED_WORDS).join(' '));
|
|
604
|
+
}
|
|
605
|
+
return out;
|
|
606
|
+
}
|
|
607
|
+
function checkRepeats(ctx, files) {
|
|
608
|
+
const first = new Map();
|
|
609
|
+
for (const file of files){
|
|
610
|
+
const reported = new Set();
|
|
611
|
+
for (const phrase of phrases(check_docs_text(node_path.join(ctx.root, file)))){
|
|
612
|
+
const owner = first.get(phrase);
|
|
613
|
+
if (void 0 === owner) first.set(phrase, file);
|
|
614
|
+
else if (!reported.has(owner)) {
|
|
615
|
+
reported.add(owner);
|
|
616
|
+
ctx.fail(file, `shares "${phrase}" with ${owner}; keep the copy in the document that owns this fact (owner table: docs/subsystems/README.md#one-fact-one-place) and link to it from the other`);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
function checkDocs(root, settings) {
|
|
622
|
+
const errors = [];
|
|
623
|
+
const ctx = {
|
|
624
|
+
root,
|
|
625
|
+
docs: settings.docs,
|
|
626
|
+
workspaces: settings.workspaces,
|
|
627
|
+
skip: new Set([
|
|
628
|
+
...SKIP,
|
|
629
|
+
...settings.docs.skip
|
|
630
|
+
]),
|
|
631
|
+
fail: (file, message)=>errors.push(`${file}: ${message}`)
|
|
632
|
+
};
|
|
633
|
+
const agent = agentDocs(ctx);
|
|
634
|
+
const files = [
|
|
635
|
+
...agent,
|
|
636
|
+
...settings.docs.trees.flatMap((dir)=>walk(ctx, dir))
|
|
637
|
+
];
|
|
638
|
+
for (const file of files)checkLinks(ctx, file);
|
|
639
|
+
checkReachable(ctx, agent);
|
|
640
|
+
checkStrayAgentFiles(ctx);
|
|
641
|
+
for (const file of files.filter(english))checkEnglish(ctx, file);
|
|
642
|
+
checkRepeats(ctx, files);
|
|
643
|
+
if (settings.runs) errors.push(...checkRuns(root, settings.runs));
|
|
644
|
+
return {
|
|
645
|
+
errors,
|
|
646
|
+
count: files.length
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
const OPTIONS = {
|
|
650
|
+
indent: 2,
|
|
651
|
+
switchCaseIndent: true
|
|
652
|
+
};
|
|
653
|
+
function formatFiles(root, files, write) {
|
|
654
|
+
const result = {
|
|
655
|
+
changed: [],
|
|
656
|
+
errors: []
|
|
657
|
+
};
|
|
658
|
+
for (const file of files){
|
|
659
|
+
const full = node_path.join(root, file);
|
|
660
|
+
if (!__rspack_external_node_fs_1b05aee1.existsSync(full) || !__rspack_external_node_fs_1b05aee1.statSync(full).isFile()) continue;
|
|
661
|
+
const source = __rspack_external_node_fs_1b05aee1.readFileSync(full, 'utf8');
|
|
662
|
+
if (!isShell(file, source.split('\n', 1)[0] ?? '')) continue;
|
|
663
|
+
let formatted;
|
|
664
|
+
try {
|
|
665
|
+
formatted = shfmt_format(source, file, OPTIONS);
|
|
666
|
+
} catch (error) {
|
|
667
|
+
result.errors.push(`${file}: ${error.message}`);
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
if (formatted !== source) {
|
|
671
|
+
result.changed.push(file);
|
|
672
|
+
if (write) __rspack_external_node_fs_1b05aee1.writeFileSync(full, formatted);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
return result;
|
|
676
|
+
}
|
|
677
|
+
function reviewLines(file, source, added) {
|
|
678
|
+
const comments = commentsOf(file, source, false);
|
|
679
|
+
if (!comments) return [];
|
|
680
|
+
const external = externalLines(comments);
|
|
681
|
+
return added.filter((l)=>external.has(l));
|
|
682
|
+
}
|
|
683
|
+
function hunkLines(hunk) {
|
|
684
|
+
const [header = '', ...body] = hunk.split('\n');
|
|
685
|
+
const start = Number.parseInt(header, 10);
|
|
686
|
+
return body.filter((l)=>l.startsWith('+')).map((l, i)=>({
|
|
687
|
+
line: start + i,
|
|
688
|
+
text: l.slice(1)
|
|
689
|
+
}));
|
|
690
|
+
}
|
|
691
|
+
function parseDiff(diff) {
|
|
692
|
+
return diff.split(/^\+\+\+ /m).flatMap((chunk)=>{
|
|
693
|
+
const [target = '', ...rest] = chunk.split('\n');
|
|
694
|
+
const added = rest.join('\n').split(/^@@ -\S+ \+/m).flatMap(hunkLines);
|
|
695
|
+
return added.length ? [
|
|
696
|
+
{
|
|
697
|
+
file: target.slice(2),
|
|
698
|
+
added
|
|
699
|
+
}
|
|
700
|
+
] : [];
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
const git = (root, args)=>execFileSync('git', args, {
|
|
704
|
+
cwd: root,
|
|
705
|
+
encoding: 'utf8',
|
|
706
|
+
maxBuffer: 1073741824
|
|
707
|
+
});
|
|
708
|
+
const isRegular = (root, file)=>__rspack_external_node_fs_1b05aee1.lstatSync(node_path.join(root, file)).isFile();
|
|
709
|
+
const readText = (root, file)=>__rspack_external_node_fs_1b05aee1.readFileSync(node_path.join(root, file), 'utf8');
|
|
710
|
+
function untracked(root) {
|
|
711
|
+
return git(root, [
|
|
712
|
+
'ls-files',
|
|
713
|
+
'--others',
|
|
714
|
+
'--exclude-standard'
|
|
715
|
+
]).split('\n').filter((file)=>file && isRegular(root, file)).map((file)=>({
|
|
716
|
+
file,
|
|
717
|
+
added: readText(root, file).split('\n').map((text, i)=>({
|
|
718
|
+
line: i + 1,
|
|
719
|
+
text
|
|
720
|
+
}))
|
|
721
|
+
}));
|
|
722
|
+
}
|
|
723
|
+
function reviewComments(root, against) {
|
|
724
|
+
const base = git(root, [
|
|
725
|
+
'merge-base',
|
|
726
|
+
against,
|
|
727
|
+
'HEAD'
|
|
728
|
+
]).trim();
|
|
729
|
+
const diff = git(root, [
|
|
730
|
+
'diff',
|
|
731
|
+
'--unified=0',
|
|
732
|
+
'--no-color',
|
|
733
|
+
base
|
|
734
|
+
]);
|
|
735
|
+
const changed = parseDiff(diff).filter(({ file })=>isRegular(root, file));
|
|
736
|
+
return [
|
|
737
|
+
...changed,
|
|
738
|
+
...untracked(root)
|
|
739
|
+
].flatMap(({ file, added })=>reviewLines(file, readText(root, file), added.map((a)=>a.line)).map((line)=>`${file}:${line}: an external(...) comment`));
|
|
740
|
+
}
|
|
741
|
+
const USAGE = `usage: repo-rules <command> [--config <file>] [args...]
|
|
742
|
+
|
|
743
|
+
commands:
|
|
744
|
+
check-comments [files...] comments the policy allows; no files: every file git does not ignore
|
|
745
|
+
format-sh [--check] [files...] shfmt on shell files; --check only lists them
|
|
746
|
+
check-docs [root] links, reachability, agent files, English, shared phrases, and check-runs
|
|
747
|
+
check-runs [root] hooks and CI call package.json scripts the docs table lists
|
|
748
|
+
review-comments [ref] external(...) comments added since the merge base with ref
|
|
749
|
+
|
|
750
|
+
options:
|
|
751
|
+
--config <file> config module (default: repo-rules.config.mjs in the root)`;
|
|
752
|
+
const EXIT_USAGE = 64;
|
|
753
|
+
class UsageError extends Error {
|
|
754
|
+
}
|
|
755
|
+
function report(errors, footer, ok) {
|
|
756
|
+
if (errors.length) {
|
|
757
|
+
console.error(`${errors.join('\n')}\n\n${footer}`);
|
|
758
|
+
return 1;
|
|
759
|
+
}
|
|
760
|
+
console.log(ok);
|
|
761
|
+
return 0;
|
|
762
|
+
}
|
|
763
|
+
const COMMANDS = {
|
|
764
|
+
'check-comments': async ({ cwd, args, load })=>{
|
|
765
|
+
const { comments } = await load(cwd);
|
|
766
|
+
const files = args.length ? ownChanges(cwd, args) : repoFiles(cwd);
|
|
767
|
+
const errors = checkComments(cwd, files, comments);
|
|
768
|
+
return report(errors, `${errors.length} comment(s) not allowed; see docs/code-standards.md#comments`, 'comments ok');
|
|
769
|
+
},
|
|
770
|
+
'format-sh': async ({ cwd, args })=>{
|
|
771
|
+
const check = args.includes('--check');
|
|
772
|
+
const named = args.filter((arg)=>'--check' !== arg);
|
|
773
|
+
const { changed, errors } = formatFiles(cwd, named.length ? ownChanges(cwd, named) : repoFiles(cwd), !check);
|
|
774
|
+
if (errors.length) console.error(errors.join('\n'));
|
|
775
|
+
if (check && changed.length) {
|
|
776
|
+
console.error(`${changed.join('\n')}\n\n${changed.length} shell file(s) need formatting: run pnpm format:sh`);
|
|
777
|
+
return 1;
|
|
778
|
+
}
|
|
779
|
+
for (const file of changed)console.log(`formatted ${file}`);
|
|
780
|
+
return errors.length ? 1 : 0;
|
|
781
|
+
},
|
|
782
|
+
'check-docs': async ({ cwd, args, load })=>{
|
|
783
|
+
const root = node_path.resolve(cwd, args[0] ?? '.');
|
|
784
|
+
const { errors, count } = checkDocs(root, await load(root));
|
|
785
|
+
if (errors.length) {
|
|
786
|
+
console.error(errors.join('\n'));
|
|
787
|
+
console.error(`\n${errors.length} problem(s). Fix the files named above.`);
|
|
788
|
+
return 1;
|
|
789
|
+
}
|
|
790
|
+
console.log(`docs ok: ${count} checked`);
|
|
791
|
+
return 0;
|
|
792
|
+
},
|
|
793
|
+
'check-runs': async ({ cwd, args, load })=>{
|
|
794
|
+
const root = node_path.resolve(cwd, args[0] ?? '.');
|
|
795
|
+
const { runs } = await load(root);
|
|
796
|
+
if (!runs) throw new UsageError('check-runs needs runs in the config');
|
|
797
|
+
return report(checkRuns(root, runs), 'Fix the files named above.', 'runs ok');
|
|
798
|
+
},
|
|
799
|
+
'review-comments': async ({ cwd, args, load })=>{
|
|
800
|
+
const found = reviewComments(cwd, args[0] ?? (await load(cwd)).baseRef);
|
|
801
|
+
if (!found.length) {
|
|
802
|
+
console.log('no comment needs /pstack:no-comments');
|
|
803
|
+
return 0;
|
|
804
|
+
}
|
|
805
|
+
console.log(`${found.join('\n')}\n\nrun /pstack:no-comments on this diff`);
|
|
806
|
+
return 1;
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
async function loadSettings(root, file) {
|
|
810
|
+
const target = node_path.resolve(root, file ?? 'repo-rules.config.mjs');
|
|
811
|
+
if (!__rspack_external_node_fs_1b05aee1.existsSync(target)) {
|
|
812
|
+
if (file) throw new UsageError(`config not found: ${file}`);
|
|
813
|
+
return config_settings();
|
|
814
|
+
}
|
|
815
|
+
const module = await import(pathToFileURL(target).href);
|
|
816
|
+
return config_settings(module.default);
|
|
817
|
+
}
|
|
818
|
+
function splitConfig(args) {
|
|
819
|
+
const at = args.indexOf('--config');
|
|
820
|
+
if (at < 0) return {
|
|
821
|
+
rest: args
|
|
822
|
+
};
|
|
823
|
+
const config = args[at + 1];
|
|
824
|
+
if (void 0 === config) throw new UsageError('--config needs a value');
|
|
825
|
+
return {
|
|
826
|
+
config,
|
|
827
|
+
rest: [
|
|
828
|
+
...args.slice(0, at),
|
|
829
|
+
...args.slice(at + 2)
|
|
830
|
+
]
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
function usageExit(error) {
|
|
834
|
+
if (error instanceof ConfigError) {
|
|
835
|
+
console.error(`repo-rules: config: ${error.message}`);
|
|
836
|
+
return EXIT_USAGE;
|
|
837
|
+
}
|
|
838
|
+
if (!(error instanceof UsageError)) throw error;
|
|
839
|
+
console.error(`repo-rules: ${error.message}\n\n${USAGE}`);
|
|
840
|
+
return EXIT_USAGE;
|
|
841
|
+
}
|
|
842
|
+
function commandNamed(name) {
|
|
843
|
+
const command = name ? COMMANDS[name] : void 0;
|
|
844
|
+
if (!command) throw new UsageError(name ? `unknown command: ${name}` : 'no command');
|
|
845
|
+
return command;
|
|
846
|
+
}
|
|
847
|
+
async function main(argv, cwd = process.cwd()) {
|
|
848
|
+
const [name, ...args] = argv;
|
|
849
|
+
if ('-h' === name || '--help' === name) {
|
|
850
|
+
console.log(USAGE);
|
|
851
|
+
return 0;
|
|
852
|
+
}
|
|
853
|
+
try {
|
|
854
|
+
const command = commandNamed(name);
|
|
855
|
+
const { config, rest } = splitConfig(args);
|
|
856
|
+
return await command({
|
|
857
|
+
cwd,
|
|
858
|
+
args: rest,
|
|
859
|
+
load: (root)=>loadSettings(config ? cwd : root, config)
|
|
860
|
+
});
|
|
861
|
+
} catch (error) {
|
|
862
|
+
return usageExit(error);
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
export { USAGE, main };
|