-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-doc-governance.js
More file actions
80 lines (66 loc) · 2.61 KB
/
Copy pathcheck-doc-governance.js
File metadata and controls
80 lines (66 loc) · 2.61 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
#!/usr/bin/env node
import { readdir, readFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const ADR_HEADING = /^#\s+ADR-([0-9]{3}[a-z]?)\b/im;
const ADR_LINK = /\((?:\.\/)?(ADR-[0-9]{3}[a-z]?-[-A-Za-z0-9_.]+\.md)(?:#[^)]+)?\)/g;
const ADR_FILE = /^ADR-([0-9]{3}[a-z]?)-.+\.md$/;
function normalizeAdrId(value) {
return value.toLowerCase();
}
export async function collectGovernanceIssues(repositoryRoot = process.cwd()) {
const adrDirectory = path.join(repositoryRoot, 'docs', 'adr');
const directoryEntries = await readdir(adrDirectory, { withFileTypes: true });
const filenames = directoryEntries
.filter((entry) => entry.isFile() && entry.name.endsWith('.md'))
.map((entry) => entry.name)
.sort();
const knownFiles = new Set(filenames);
const adrOwners = new Map();
const issues = [];
for (const filename of filenames) {
const fileMatch = filename.match(ADR_FILE);
if (!fileMatch) {
issues.push(`${filename}: filename must start with ADR-<NNN>[suffix]-`);
continue;
}
const content = await readFile(path.join(adrDirectory, filename), 'utf8');
const headingMatch = content.match(ADR_HEADING);
if (!headingMatch) {
issues.push(`${filename}: first-level ADR heading is missing`);
continue;
}
const filenameId = normalizeAdrId(fileMatch[1]);
const headingId = normalizeAdrId(headingMatch[1]);
if (filenameId !== headingId) {
issues.push(`${filename}: filename ADR-${fileMatch[1]} does not match heading ADR-${headingMatch[1]}`);
}
const existingOwner = adrOwners.get(headingId);
if (existingOwner) {
issues.push(`ADR-${headingMatch[1]} is duplicated by ${existingOwner} and ${filename}`);
} else {
adrOwners.set(headingId, filename);
}
for (const match of content.matchAll(ADR_LINK)) {
const target = match[1];
if (!knownFiles.has(target)) {
issues.push(`${filename}: local ADR link target does not exist: ${target}`);
}
}
}
return issues.sort();
}
export async function main(repositoryRoot = process.cwd()) {
const issues = await collectGovernanceIssues(repositoryRoot);
if (issues.length > 0) {
process.stderr.write('Documentation governance check failed:\n');
for (const issue of issues) process.stderr.write(`- ${issue}\n`);
return 1;
}
process.stdout.write('Documentation governance check passed.\n');
return 0;
}
const invokedPath = process.argv[1] ? path.resolve(process.argv[1]) : '';
if (invokedPath === fileURLToPath(import.meta.url)) {
process.exitCode = await main(process.argv[2] ?? process.cwd());
}