diff --git a/scripts/generate-docs-index.mjs b/scripts/generate-docs-index.mjs index 61756d0..1f9e1ee 100644 --- a/scripts/generate-docs-index.mjs +++ b/scripts/generate-docs-index.mjs @@ -1,6 +1,7 @@ #!/usr/bin/env node // Generate docs/INDEX.md from the front-matter and headings of every -// .md file under docs/. Pure Node 20 — no external dependencies. +// git-tracked .md file under docs/. No external npm dependencies — shells +// out to `git ls-files` so untracked scratch files never pollute the index. // // Usage: `npm run docs:index` (or `node scripts/generate-docs-index.mjs`). // @@ -8,9 +9,10 @@ // output (apart from the generation date in the header), so the script // is safe to run repeatedly and in pre-commit hooks. -import { readdir, readFile, writeFile } from 'node:fs/promises'; +import { readFile, writeFile } from 'node:fs/promises'; import { join, relative, basename, sep } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { execFileSync } from 'node:child_process'; const SCRIPT_DIR = fileURLToPath(new URL('.', import.meta.url)); const REPO_ROOT = join(SCRIPT_DIR, '..'); @@ -28,18 +30,19 @@ const EXCLUDE_PATTERNS = [ /^docs\/old\//, ]; -async function walk(dir) { - const entries = await readdir(dir, { withFileTypes: true }); - const files = []; - for (const e of entries) { - const full = join(dir, e.name); - if (e.isDirectory()) { - files.push(...(await walk(full))); - } else if (e.isFile() && e.name.endsWith('.md')) { - files.push(full); - } - } - return files; +// List git-tracked (and staged) .md files under docs/ via `git ls-files` +// rather than walking the filesystem — this keeps untracked scratch files +// out of the index. Paths return repo-root-relative and forward-slashed, +// and the call works correctly inside git worktrees. +function trackedDocsFiles() { + const out = execFileSync('git', ['ls-files', '-z', 'docs'], { + cwd: REPO_ROOT, + encoding: 'utf8', + }); + return out + .split('\0') + .filter((p) => p.endsWith('.md')) + .map((p) => join(REPO_ROOT, p)); } // Minimal YAML front-matter parser. Front-matter in this repo is restricted @@ -113,7 +116,7 @@ function escapePipe(s) { } async function main() { - const files = await walk(DOCS_DIR); + const files = trackedDocsFiles(); const docs = []; for (const full of files) {