|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +const root = process.cwd(); |
| 5 | +const reportPath = path.join(root, "docs/维护与报告/仓库健康巡检报告.md"); |
| 6 | + |
| 7 | +const skipDirs = new Set([ |
| 8 | + ".git", |
| 9 | + "node_modules", |
| 10 | + "dist", |
| 11 | + ".vite", |
| 12 | + ".cache", |
| 13 | + ".astro", |
| 14 | +]); |
| 15 | + |
| 16 | +const guardFiles = new Set([ |
| 17 | + "scripts/check-site-domain.mjs", |
| 18 | + "scripts/check-doc-links-light.mjs", |
| 19 | + "scripts/audit-repo-health.mjs", |
| 20 | + "docs/维护与报告/仓库健康巡检报告.md", |
| 21 | +]); |
| 22 | + |
| 23 | +const textExts = new Set([ |
| 24 | + ".md", |
| 25 | + ".mdx", |
| 26 | + ".ts", |
| 27 | + ".tsx", |
| 28 | + ".js", |
| 29 | + ".jsx", |
| 30 | + ".mjs", |
| 31 | + ".mts", |
| 32 | + ".vue", |
| 33 | + ".css", |
| 34 | + ".json", |
| 35 | + ".yml", |
| 36 | + ".yaml", |
| 37 | + ".txt", |
| 38 | + ".html", |
| 39 | +]); |
| 40 | + |
| 41 | +const suspiciousPatterns = [ |
| 42 | + { |
| 43 | + name: "绝对本地路径", |
| 44 | + pattern: /[A-Z]:\\|\/storage\/emulated\/0|\/home\/|\/mnt\//i, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "旧域名", |
| 48 | + pattern: /cysjdocs\.dpdns\.org|cysjdocs\.pages\.dev/i, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "疑似临时标记", |
| 52 | + pattern: /TODO|FIXME|待处理|临时|占位|随便写|测试内容/i, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "冲突文件痕迹", |
| 56 | + pattern: /sync-conflict|<<<<<<<|=======|>>>>>>>/i, |
| 57 | + }, |
| 58 | +]; |
| 59 | + |
| 60 | +function rel(file) { |
| 61 | + return path.relative(root, file).replaceAll("\\", "/"); |
| 62 | +} |
| 63 | + |
| 64 | +function walk(dir, output = []) { |
| 65 | + if (!fs.existsSync(dir)) return output; |
| 66 | + |
| 67 | + for (const item of fs.readdirSync(dir, { withFileTypes: true })) { |
| 68 | + const full = path.join(dir, item.name); |
| 69 | + const relative = rel(full); |
| 70 | + |
| 71 | + if (item.isDirectory()) { |
| 72 | + if (skipDirs.has(item.name)) continue; |
| 73 | + if (relative.startsWith("docs/.vitepress/dist")) continue; |
| 74 | + |
| 75 | + walk(full, output); |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + if (guardFiles.has(relative)) continue; |
| 80 | + |
| 81 | + if (textExts.has(path.extname(item.name).toLowerCase())) { |
| 82 | + output.push(full); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return output; |
| 87 | +} |
| 88 | + |
| 89 | +function read(file) { |
| 90 | + try { |
| 91 | + return fs.readFileSync(file, "utf8"); |
| 92 | + } catch { |
| 93 | + return ""; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function getTitle(text) { |
| 98 | + const frontmatterTitle = text.match(/^---[\s\S]*?\ntitle:\s*["']?(.+?)["']?\s*\n[\s\S]*?---/); |
| 99 | + if (frontmatterTitle) return frontmatterTitle[1].trim(); |
| 100 | + |
| 101 | + const h1 = text.match(/^#\s+(.+)$/m); |
| 102 | + if (h1) return h1[1].trim(); |
| 103 | + |
| 104 | + return ""; |
| 105 | +} |
| 106 | + |
| 107 | +const files = walk(root); |
| 108 | +const mdFiles = files.filter((file) => path.extname(file).toLowerCase() === ".md"); |
| 109 | + |
| 110 | +const stats = { |
| 111 | + totalTextFiles: files.length, |
| 112 | + totalMdFiles: mdFiles.length, |
| 113 | + totalDocsFiles: mdFiles.filter((file) => rel(file).startsWith("docs/")).length, |
| 114 | +}; |
| 115 | + |
| 116 | +const findings = []; |
| 117 | +const titleMap = new Map(); |
| 118 | +const bigFiles = []; |
| 119 | + |
| 120 | +for (const file of files) { |
| 121 | + const relative = rel(file); |
| 122 | + const text = read(file); |
| 123 | + const size = Buffer.byteLength(text, "utf8"); |
| 124 | + |
| 125 | + if (size > 80 * 1024) { |
| 126 | + bigFiles.push({ file: relative, size }); |
| 127 | + } |
| 128 | + |
| 129 | + for (const item of suspiciousPatterns) { |
| 130 | + if (item.pattern.test(text)) { |
| 131 | + findings.push({ |
| 132 | + level: item.name === "旧域名" || item.name === "冲突文件痕迹" ? "高" : "中", |
| 133 | + file: relative, |
| 134 | + issue: item.name, |
| 135 | + }); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (relative.endsWith(".md") && relative.startsWith("docs/")) { |
| 140 | + const title = getTitle(text); |
| 141 | + |
| 142 | + if (!title) { |
| 143 | + findings.push({ |
| 144 | + level: "中", |
| 145 | + file: relative, |
| 146 | + issue: "缺少可识别标题", |
| 147 | + }); |
| 148 | + } else { |
| 149 | + if (!titleMap.has(title)) titleMap.set(title, []); |
| 150 | + titleMap.get(title).push(relative); |
| 151 | + } |
| 152 | + |
| 153 | + const h1Count = (text.match(/^#\s+/gm) || []).length; |
| 154 | + if (h1Count > 1) { |
| 155 | + findings.push({ |
| 156 | + level: "低", |
| 157 | + file: relative, |
| 158 | + issue: `存在 ${h1Count} 个一级标题`, |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + if (!/^---[\s\S]*?---/.test(text)) { |
| 163 | + findings.push({ |
| 164 | + level: "中", |
| 165 | + file: relative, |
| 166 | + issue: "缺少 frontmatter", |
| 167 | + }); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +for (const [title, list] of titleMap.entries()) { |
| 173 | + if (list.length > 1) { |
| 174 | + for (const file of list) { |
| 175 | + findings.push({ |
| 176 | + level: "低", |
| 177 | + file, |
| 178 | + issue: `标题重复:${title}`, |
| 179 | + }); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +bigFiles |
| 185 | + .sort((a, b) => b.size - a.size) |
| 186 | + .slice(0, 20) |
| 187 | + .forEach((item) => { |
| 188 | + findings.push({ |
| 189 | + level: item.size > 180 * 1024 ? "中" : "低", |
| 190 | + file: item.file, |
| 191 | + issue: `文件偏大:${Math.round(item.size / 1024)}KB`, |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | +const grouped = { |
| 196 | + 高: findings.filter((item) => item.level === "高"), |
| 197 | + 中: findings.filter((item) => item.level === "中"), |
| 198 | + 低: findings.filter((item) => item.level === "低"), |
| 199 | +}; |
| 200 | + |
| 201 | +const now = new Date().toISOString(); |
| 202 | + |
| 203 | +const lines = [ |
| 204 | + "---", |
| 205 | + "title: 仓库健康巡检报告", |
| 206 | + "description: 记录创游世界资料库的轻量巡检结果,用于发现旧域名、冲突文件、缺失标题、重复标题、过大文件和潜在维护问题。", |
| 207 | + "created: 2026-06-08", |
| 208 | + `updated: ${now.slice(0, 10)}`, |
| 209 | + "search: false", |
| 210 | + "---", |
| 211 | + "", |
| 212 | + "# 仓库健康巡检报告", |
| 213 | + "", |
| 214 | + `> 生成时间:${now}`, |
| 215 | + "", |
| 216 | + "## 巡检摘要", |
| 217 | + "", |
| 218 | + `- 文本文件数量:${stats.totalTextFiles}`, |
| 219 | + `- Markdown 文件数量:${stats.totalMdFiles}`, |
| 220 | + `- docs 文档数量:${stats.totalDocsFiles}`, |
| 221 | + `- 高风险问题:${grouped["高"].length}`, |
| 222 | + `- 中风险问题:${grouped["中"].length}`, |
| 223 | + `- 低风险问题:${grouped["低"].length}`, |
| 224 | + "", |
| 225 | + "## 高风险问题", |
| 226 | + "", |
| 227 | + grouped["高"].length |
| 228 | + ? grouped["高"].map((item) => `- \`${item.file}\`:${item.issue}`).join("\n") |
| 229 | + : "- 暂无高风险问题。", |
| 230 | + "", |
| 231 | + "## 中风险问题", |
| 232 | + "", |
| 233 | + grouped["中"].length |
| 234 | + ? grouped["中"].slice(0, 80).map((item) => `- \`${item.file}\`:${item.issue}`).join("\n") |
| 235 | + : "- 暂无中风险问题。", |
| 236 | + "", |
| 237 | + "## 低风险问题", |
| 238 | + "", |
| 239 | + grouped["低"].length |
| 240 | + ? grouped["低"].slice(0, 120).map((item) => `- \`${item.file}\`:${item.issue}`).join("\n") |
| 241 | + : "- 暂无低风险问题。", |
| 242 | + "", |
| 243 | + "## 后续建议", |
| 244 | + "", |
| 245 | + "- 优先处理旧域名、冲突文件痕迹、缺少 frontmatter、缺少标题的问题。", |
| 246 | + "- 重复标题不一定是错误,但建议检查是否存在内容重复或导航命名不清晰。", |
| 247 | + "- 文件偏大不一定需要立刻拆分,但如果阅读体验下降,可以拆为专题页、索引页和细分页。", |
| 248 | + "- 本报告只做轻量巡检,不会替代完整构建检查。", |
| 249 | + "", |
| 250 | +]; |
| 251 | + |
| 252 | +fs.mkdirSync(path.dirname(reportPath), { recursive: true }); |
| 253 | +fs.writeFileSync(reportPath, lines.join("\n") + "\n", "utf8"); |
| 254 | + |
| 255 | +console.log(`Repository health report generated: ${path.relative(root, reportPath)}`); |
| 256 | + |
| 257 | +if (grouped["高"].length > 0) { |
| 258 | + console.error(`Found ${grouped["高"].length} high-risk issue(s).`); |
| 259 | + process.exit(1); |
| 260 | +} |
0 commit comments