-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit.ts
More file actions
114 lines (104 loc) · 3.19 KB
/
Copy pathaudit.ts
File metadata and controls
114 lines (104 loc) · 3.19 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
/**
* Run the complete repository maintenance audit.
*
* Usage:
* nextdns-skills-scripts audit
* nextdns-skills-scripts audit --json
*/
import path from 'node:path';
import { getRepositoryRoot } from '../core/paths.js';
import { parseAuditReport, type AuditCheck, type AuditReport } from '../core/schemas.js';
import { checkDuplicateTags, checkDuplicateTitles, loadAllRules } from './check-duplicates.js';
import { validateTags } from './check-tags.js';
import { buildReport } from './generate-stats.js';
import { validateFrontmatter, validateReferentialIntegrity } from './validate-rules.js';
const REPO_ROOT = getRepositoryRoot(import.meta.url);
const SKILLS_DIR = path.join(REPO_ROOT, 'skills');
export type { AuditCheck, AuditReport } from '../core/schemas.js';
function suppressOutput<T>(fn: () => T): T {
const log = console.log;
const error = console.error;
console.log = () => undefined;
console.error = () => undefined;
try {
return fn();
} finally {
console.log = log;
console.error = error;
}
}
export function runAudit(
skillsDir: string = SKILLS_DIR,
repoRoot: string = REPO_ROOT
): AuditReport {
const rules = suppressOutput(() => loadAllRules(skillsDir));
const duplicateTitles = suppressOutput(() => checkDuplicateTitles(rules));
const duplicateTags = suppressOutput(() => checkDuplicateTags(rules));
const referentialIntegrity = suppressOutput(() => validateReferentialIntegrity(skillsDir));
const frontmatter = suppressOutput(() => validateFrontmatter(skillsDir));
const tags = suppressOutput(() => validateTags(skillsDir));
const checks: AuditCheck[] = [
{
name: 'referential-integrity',
passed: referentialIntegrity,
errors: referentialIntegrity ? 0 : 1,
warnings: 0,
},
{
name: 'frontmatter',
passed: frontmatter,
errors: frontmatter ? 0 : 1,
warnings: 0,
},
{
name: 'tags',
passed: tags,
errors: tags ? 0 : 1,
warnings: 0,
},
{
name: 'duplicate-titles',
passed: duplicateTitles.errors === 0,
errors: duplicateTitles.errors,
warnings: duplicateTitles.warnings,
},
{
name: 'duplicate-tags',
passed: duplicateTags === 0,
errors: 0,
warnings: duplicateTags,
},
];
return parseAuditReport({
generatedAt: new Date().toISOString(),
passed: checks.every((check) => check.passed),
ruleCount: rules.length,
checks,
statistics: buildReport(skillsDir, repoRoot),
});
}
export function formatAuditText(report: AuditReport): string {
const lines = [
`NextDNS Skills audit — ${report.generatedAt}`,
`Rules checked: ${report.ruleCount}`,
'',
...report.checks.map(
(check) =>
`${check.passed ? 'PASS' : 'FAIL'} ${check.name} ` +
`(errors: ${check.errors}, warnings: ${check.warnings})`
),
'',
report.passed ? 'Audit passed.' : 'Audit failed.',
];
return lines.join('\n');
}
export function run(): void {
const args = process.argv.slice(2);
const report = runAudit();
if (args.includes('--json')) {
console.log(JSON.stringify(report, null, 2));
} else {
console.log(formatAuditText(report));
}
process.exitCode = report.passed ? 0 : 1;
}