|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Run the complete repository maintenance audit. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * nextdns-skills-scripts audit |
| 7 | + * nextdns-skills-scripts audit --json |
| 8 | + */ |
| 9 | + |
| 10 | +import path from 'node:path'; |
| 11 | +import { fileURLToPath } from 'node:url'; |
| 12 | + |
| 13 | +import { checkDuplicateTags, checkDuplicateTitles, loadAllRules } from './check-duplicates.js'; |
| 14 | +import { buildReport, type StatsReport } from './generate-stats.js'; |
| 15 | +import { validateFrontmatter, validateReferentialIntegrity } from './validate-rules.js'; |
| 16 | +import { validateTags } from './check-tags.js'; |
| 17 | + |
| 18 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 19 | +const REPO_ROOT = path.join(__dirname, '../../..'); |
| 20 | +const SKILLS_DIR = path.join(REPO_ROOT, 'skills'); |
| 21 | + |
| 22 | +export interface AuditCheck { |
| 23 | + name: 'referential-integrity' | 'frontmatter' | 'tags' | 'duplicate-titles' | 'duplicate-tags'; |
| 24 | + passed: boolean; |
| 25 | + errors: number; |
| 26 | + warnings: number; |
| 27 | +} |
| 28 | + |
| 29 | +export interface AuditReport { |
| 30 | + generatedAt: string; |
| 31 | + passed: boolean; |
| 32 | + ruleCount: number; |
| 33 | + checks: AuditCheck[]; |
| 34 | + statistics: StatsReport; |
| 35 | +} |
| 36 | + |
| 37 | +function suppressOutput<T>(fn: () => T): T { |
| 38 | + const log = console.log; |
| 39 | + const error = console.error; |
| 40 | + console.log = () => undefined; |
| 41 | + console.error = () => undefined; |
| 42 | + try { |
| 43 | + return fn(); |
| 44 | + } finally { |
| 45 | + console.log = log; |
| 46 | + console.error = error; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export function runAudit(skillsDir: string = SKILLS_DIR, repoRoot: string = REPO_ROOT): AuditReport { |
| 51 | + const rules = suppressOutput(() => loadAllRules(skillsDir)); |
| 52 | + const duplicateTitles = suppressOutput(() => checkDuplicateTitles(rules)); |
| 53 | + const duplicateTags = suppressOutput(() => checkDuplicateTags(rules)); |
| 54 | + const referentialIntegrity = suppressOutput(() => validateReferentialIntegrity(skillsDir)); |
| 55 | + const frontmatter = suppressOutput(() => validateFrontmatter(skillsDir)); |
| 56 | + const tags = suppressOutput(() => validateTags(skillsDir)); |
| 57 | + |
| 58 | + const checks: AuditCheck[] = [ |
| 59 | + { |
| 60 | + name: 'referential-integrity', |
| 61 | + passed: referentialIntegrity, |
| 62 | + errors: referentialIntegrity ? 0 : 1, |
| 63 | + warnings: 0, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: 'frontmatter', |
| 67 | + passed: frontmatter, |
| 68 | + errors: frontmatter ? 0 : 1, |
| 69 | + warnings: 0, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: 'tags', |
| 73 | + passed: tags, |
| 74 | + errors: tags ? 0 : 1, |
| 75 | + warnings: 0, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: 'duplicate-titles', |
| 79 | + passed: duplicateTitles.errors === 0, |
| 80 | + errors: duplicateTitles.errors, |
| 81 | + warnings: duplicateTitles.warnings, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: 'duplicate-tags', |
| 85 | + passed: duplicateTags === 0, |
| 86 | + errors: 0, |
| 87 | + warnings: duplicateTags, |
| 88 | + }, |
| 89 | + ]; |
| 90 | + |
| 91 | + return { |
| 92 | + generatedAt: new Date().toISOString(), |
| 93 | + passed: checks.every((check) => check.passed), |
| 94 | + ruleCount: rules.length, |
| 95 | + checks, |
| 96 | + statistics: buildReport(skillsDir, repoRoot), |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +export function formatAuditText(report: AuditReport): string { |
| 101 | + const lines = [ |
| 102 | + `NextDNS Skills audit — ${report.generatedAt}`, |
| 103 | + `Rules checked: ${report.ruleCount}`, |
| 104 | + '', |
| 105 | + ...report.checks.map( |
| 106 | + (check) => |
| 107 | + `${check.passed ? 'PASS' : 'FAIL'} ${check.name} ` + |
| 108 | + `(errors: ${check.errors}, warnings: ${check.warnings})` |
| 109 | + ), |
| 110 | + '', |
| 111 | + report.passed ? 'Audit passed.' : 'Audit failed.', |
| 112 | + ]; |
| 113 | + return lines.join('\n'); |
| 114 | +} |
| 115 | + |
| 116 | +export function run(): void { |
| 117 | + const args = process.argv.slice(2); |
| 118 | + const report = runAudit(); |
| 119 | + if (args.includes('--json')) { |
| 120 | + console.log(JSON.stringify(report, null, 2)); |
| 121 | + } else { |
| 122 | + console.log(formatAuditText(report)); |
| 123 | + } |
| 124 | + process.exitCode = report.passed ? 0 : 1; |
| 125 | +} |
| 126 | + |
| 127 | +if (fileURLToPath(import.meta.url) === process.argv[1]) run(); |
0 commit comments