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.
This commit is contained in:
1 parent
7fe5cae62c
commit
666a12c676
13 files changed
+288
-21
No files matched your search
@@ -0,0 +1,106 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
|
||||
import { tmpdir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
import {
|
||||
checkReadmeNaming,
|
||||
expectedReadmeName,
|
||||
isReadmeLike,
|
||||
validateReadmePaths
|
||||
} from "./check-readme-naming.js"
|
||||
|
||||
describe("isReadmeLike", () => {
|
||||
test("matches README variants", () => {
|
||||
for (const name of ["README.md", "readme.md", "README.txt", "src.readme.md"]) {
|
||||
expect(isReadmeLike(name)).toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
test("ignores unrelated files", () => {
|
||||
for (const name of ["index.md", "readme-assets.ts", "README_GUIDE.md"]) {
|
||||
expect(isReadmeLike(name)).toBe(false)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe("expectedReadmeName", () => {
|
||||
test("prefixes with the directory name", () => {
|
||||
expect(expectedReadmeName("src")).toBe("src.readme.md")
|
||||
expect(expectedReadmeName("host-infra")).toBe("host-infra.readme.md")
|
||||
})
|
||||
|
||||
test("strips leading dots from dot-directories", () => {
|
||||
expect(expectedReadmeName(".agents")).toBe("agents.readme.md")
|
||||
})
|
||||
})
|
||||
|
||||
describe("validateReadmePaths", () => {
|
||||
test("accepts the conventional layout", () => {
|
||||
const issues = validateReadmePaths([
|
||||
"README.md",
|
||||
"src/src.readme.md",
|
||||
".agents/agents.readme.md",
|
||||
"host-infra/host-infra.readme.md",
|
||||
"src/domains/seal/reports/reports.readme.md"
|
||||
])
|
||||
expect(issues).toEqual([])
|
||||
})
|
||||
|
||||
test("rejects a plain README.md in a subdirectory", () => {
|
||||
const issues = validateReadmePaths(["README.md", "src/README.md"])
|
||||
expect(issues).toHaveLength(1)
|
||||
expect(issues[0].file).toBe("src/README.md")
|
||||
expect(issues[0].rule).toBe("subdir-readme-name")
|
||||
})
|
||||
|
||||
test("rejects a subdirectory README with the wrong prefix", () => {
|
||||
const issues = validateReadmePaths(["README.md", "src/docs.readme.md"])
|
||||
expect(issues).toHaveLength(1)
|
||||
expect(issues[0].rule).toBe("subdir-readme-name")
|
||||
expect(issues[0].message).toContain("src.readme.md")
|
||||
})
|
||||
|
||||
test("rejects a lowercase root readme", () => {
|
||||
const issues = validateReadmePaths(["readme.md"])
|
||||
expect(issues.some((issue) => issue.rule === "root-readme-name")).toBe(true)
|
||||
expect(issues.some((issue) => issue.rule === "missing-root-readme")).toBe(true)
|
||||
})
|
||||
|
||||
test("requires a root README", () => {
|
||||
const issues = validateReadmePaths(["src/src.readme.md"])
|
||||
expect(issues.some((issue) => issue.rule === "missing-root-readme")).toBe(true)
|
||||
})
|
||||
|
||||
test("accepts nested directories with matching prefixes", () => {
|
||||
const issues = validateReadmePaths([
|
||||
"README.md",
|
||||
"src/domains/seal/reports/reports.readme.md",
|
||||
"src/domains/seal/reports/templates/templates.readme.md"
|
||||
])
|
||||
expect(issues).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe("checkReadmeNaming", () => {
|
||||
test("skips the local root reports directory but checks nested ones", () => {
|
||||
const root = mkdtempSync(join(tmpdir(), "readme-naming-"))
|
||||
try {
|
||||
writeFileSync(join(root, "README.md"), "# Root")
|
||||
mkdirSync(join(root, "reports"))
|
||||
writeFileSync(join(root, "reports", "README.md"), "# Local-only")
|
||||
mkdirSync(join(root, "src"))
|
||||
writeFileSync(join(root, "src", "README.md"), "# Wrong")
|
||||
|
||||
const issues = checkReadmeNaming(root)
|
||||
expect(issues).toHaveLength(1)
|
||||
expect(issues[0].file).toBe("src/README.md")
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test("the committed repository passes the naming rule", () => {
|
||||
const root = join(import.meta.dirname, "../..")
|
||||
expect(checkReadmeNaming(root)).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,149 @@
|
||||
#!/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()
|
||||
@@ -13,6 +13,7 @@ scripts/
|
||||
│ ├── check-agents-md.ts # AGENTS.md 开发者契约校验器
|
||||
│ ├── check-skill-md.ts # SKILL.md 路由中枢与 references 双向路由校验器
|
||||
│ ├── check-readme.ts # README.md 结构与卫生巡检器(警告级别)
|
||||
│ ├── check-readme-naming.ts # 子目录 README 命名规范校验器(目录名前缀)
|
||||
│ ├── doc-md-common.ts # 共享 fence 感知 Markdown 解析纯函数库
|
||||
│ └── *.test.ts # 逻辑 fixture 与真实文档现状双覆盖测试
|
||||
│
|
||||
Reference in new issue
Block a user