Files
seal-use/scripts/governance/check-readme-naming.ts
T
gitea 666a12c676 Enforce README naming convention for subdirectories
The root keeps README.md; every subdirectory README must be <dir>.readme.md with leading dots stripped. Add a blocking governance checker with 12 tests, wire it into package scripts, CI, and pre-commit, and rename the existing subdirectory READMEs.
2026-11-19 16:26:37 +08:00

150 lines
4.3 KiB
TypeScript

#!/usr/bin/env bun
/**
* Governance check for README naming.
*
* Rule: the repository root keeps the conventional `README.md`; every
* subdirectory README must carry its directory name as a prefix, e.g.
* `src/src.readme.md`, `.agents/agents.readme.md` (leading dots are stripped
* so the file stays visible).
*
* Invoked as `bun run verify:readme-naming` (CI + pre-commit) and with
* `--soft` from the ZCode SessionStart hook.
*/
import { readdirSync } from "node:fs"
import { join, relative, sep } from "node:path"
export const ROOT_README = "README.md"
/** Directories whose contents are generated, vendored, or local-only. */
export const SKIP_DIRS: ReadonlySet<string> = new Set([
"node_modules",
".git",
"dist",
"build",
"coverage",
".cache",
"tmp",
".local",
".drafts",
".workbuddy",
".mimosa",
".apifox"
])
/**
* Only the repository-root copy is local-only; a nested `reports/` directory
* (e.g. `src/domains/seal/reports/`) is tracked and must still be checked.
*/
export const ROOT_ONLY_SKIP_DIRS: ReadonlySet<string> = new Set(["reports"])
export type ReadmeNamingIssue = {
file: string
rule: string
message: string
}
/** `README.md`, `readme.txt`, `src.readme.md` — any file named like a README. */
export function isReadmeLike(name: string): boolean {
const lower = name.toLowerCase()
return lower === "readme" || lower.startsWith("readme.") || lower.endsWith(".readme.md")
}
/** Directory name → required README prefix, stripping leading dots. */
export function expectedReadmeName(dirName: string): string {
const prefix = dirName.replace(/^\.+/, "") || dirName
return `${prefix}.readme.md`
}
/**
* Pure validation over repo-relative paths so tests do not need a temp tree.
*/
export function validateReadmePaths(paths: readonly string[]): ReadmeNamingIssue[] {
const issues: ReadmeNamingIssue[] = []
const normalized = paths.map((path) => path.split(sep).join("/"))
if (!normalized.includes(ROOT_README)) {
issues.push({
file: ROOT_README,
rule: "missing-root-readme",
message: "repository root must keep README.md"
})
}
for (const path of normalized) {
const parts = path.split("/")
const name = parts.pop() ?? ""
if (!isReadmeLike(name)) continue
if (parts.length === 0) {
if (name !== ROOT_README) {
issues.push({
file: path,
rule: "root-readme-name",
message: `root README must be named ${ROOT_README}, found ${name}`
})
}
continue
}
const dirName = parts[parts.length - 1]
const expected = expectedReadmeName(dirName)
if (name !== expected) {
issues.push({
file: path,
rule: "subdir-readme-name",
message: `subdirectory README must be named ${expected} (directory "${dirName}"), found ${name}`
})
}
}
return issues
}
function collectFiles(rootDir: string, dir = rootDir, files: string[] = []): string[] {
for (const entry of readdirSync(dir, { withFileTypes: true })) {
if (entry.isDirectory()) {
if (SKIP_DIRS.has(entry.name)) continue
const relDir = relative(rootDir, join(dir, entry.name)).split(sep).join("/")
if (ROOT_ONLY_SKIP_DIRS.has(entry.name) && relDir === entry.name) continue
collectFiles(rootDir, join(dir, entry.name), files)
} else if (entry.isFile()) {
files.push(relative(rootDir, join(dir, entry.name)).split(sep).join("/"))
}
}
return files
}
export function checkReadmeNaming(rootDir: string): ReadmeNamingIssue[] {
return validateReadmePaths(collectFiles(rootDir))
}
function main(): void {
const root = join(import.meta.dirname, "../..")
const issues = checkReadmeNaming(root)
if (process.argv.includes("--soft")) {
if (issues.length > 0) {
const summary = issues.map((issue) => issue.message).join("; ")
console.log(
JSON.stringify({
additionalContext: `README 命名检查发现 ${issues.length} 项问题:${summary}。本地运行 bun run verify:readme-naming 查看详情。`
})
)
}
return
}
if (issues.length > 0) {
for (const issue of issues) {
console.error(`✗ ${issue.file}: ${issue.message} [${issue.rule}]`)
}
console.error(`README naming checks failed (${issues.length} issue(s)).`)
process.exitCode = 1
return
}
console.log("README naming checks passed.")
}
if (import.meta.main) main()