Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/lukebennett88/luke-ui. ui.lukebennett.dev
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import { existsSync, readdirSync, readFileSync, rmdirSync, rmSync, statSync } from 'node:fs';import { resolve } from 'node:path';
const GENERATOR_OWNED_FILES: ReadonlyArray<string> = ['props.mdx', 'meta.json'];
/** * The retired `generate-props-pages` script wrote this exact line into every `props.mdx` it * generated, after the frontmatter block. It is the only reliable signal that a given * `props.mdx` is stale generator output rather than an authored Fumadocs page — depth alone * (`<group>/<component>/props.mdx`) is not, since `props.mdx` and `meta.json` are both legitimate * things to author at that location. */const GENERATOR_MARKER = '{/* Generated by scripts/generate-props-pages.ts. Do not edit. */}';
/** * Removes stale output from the retired `generate-props-pages` script. Those files were * gitignored, so an existing checkout can still have `content/docs/components/<group>/<name>/ * props.mdx` and `meta.json` on disk after switching to a branch that no longer generates them — * Fumadocs would keep discovering them and the old `/components/<group>/<name>/props` routes * would survive locally. * * A component directory (exactly two levels under `componentsDir`, i.e. `<group>/<component>/`) * is only treated as stale generator output when its `props.mdx` exists and its contents contain * `GENERATOR_MARKER`. In that case both `props.mdx` and its sibling `meta.json` (if present) are * removed, and the component directory itself is removed only once it is empty. An authored * `props.mdx` without the marker is left untouched, and so is any `meta.json` next to it — a * standalone `meta.json` with no marked `props.mdx` is never removed. Never touches the authored * `<group>/<component>.mdx` guides or the group-level `<group>/meta.json` files one level up. * No-ops silently when `componentsDir` or a group subdirectory does not exist. */export function cleanGeneratedPropsPages(componentsDir: string): { removedCount: number;} { let removedCount = 0; if (!existsSync(componentsDir)) return { removedCount };
for (const group of readdirSync(componentsDir, { withFileTypes: true })) { if (!group.isDirectory()) continue; const groupDir = resolve(componentsDir, group.name);
for (const component of readdirSync(groupDir, { withFileTypes: true })) { if (!component.isDirectory()) continue; const componentDir = resolve(groupDir, component.name); removedCount += removeGeneratorOwnedFiles(componentDir); removeDirIfEmpty(componentDir); } }
return { removedCount };}
/** * Removes `props.mdx` and its sibling `meta.json` in `componentDir`, but only when `props.mdx` * exists and contains `GENERATOR_MARKER`. Leaves everything else untouched. */function removeGeneratorOwnedFiles(componentDir: string): number { const propsPath = resolve(componentDir, 'props.mdx'); if (!existsSync(propsPath)) return 0; if (!statSync(propsPath).isFile()) return 0; if (!readFileSync(propsPath, 'utf8').includes(GENERATOR_MARKER)) return 0;
let removedCount = 0; for (const filename of GENERATOR_OWNED_FILES) { const filepath = resolve(componentDir, filename); if (!existsSync(filepath)) continue; if (!statSync(filepath).isFile()) continue; rmSync(filepath); removedCount++; } return removedCount;}
/** Removes the component output directory only when it is now empty. */function removeDirIfEmpty(dirPath: string): void { if (!existsSync(dirPath)) return; if (readdirSync(dirPath).length > 0) return; rmdirSync(dirPath);}