import fs from "node:fs"; import path from "node:path"; import { HISTORY_DIR, MIRROR_DIR, TARBALL_HREF, parseSourceManifest, type SourceManifest, } from "yt-dlp-transcript-common/lib/sourceManifest"; // The published source's manifest, written LAST by `archilyzer source publish` // (common/publish/source.ts) into public/source/, or null when this build has // none. The /source/ page renders an honest empty state for null — the mirror, // the tree and the tarball are gitignored build artefacts, so a fresh clone // (or `archilyzer build homepage --no-source`) has none of them. // // Believed only when the files it describes are here too (the snapshot.ts // rule): a manifest beside a missing mirror or tarball would advertise a // clone that fails and a download that 404s. The same for the history block // (release 15 slice SG): without its log page beside it, the manifest is // returned WITHOUT it, and the page shows no History links. A malformed or // wrong-version manifest is null too (parseSourceManifest checks every number // the page reads), so a bad file is the empty state, never a crash in `next // build`. `pub` is the test's seam. // // The default is a DIRECTORY join on `process.cwd()`, which Turbopack would // trace as every file under `public/` (the source mirror among them, and it // grows with every publish): the opt-out keeps it a plain run-time path // (plans/FACTS.md, "A path joined from `process.cwd()` …"). export function loadSourceManifest( pub: string = sourcePublicDir( process.env, path.join(/* turbopackIgnore: true */ process.cwd(), "public"), ), ): SourceManifest | null { try { const manifest = parseSourceManifest( JSON.parse(fs.readFileSync(path.join(/* turbopackIgnore: true */ pub, "source", "manifest.json"), "utf8")), ); if (!manifest) return null; if (!fs.statSync(path.join(/* turbopackIgnore: true */ pub, "source", MIRROR_DIR, "info", "refs")).isFile()) return null; if (!fs.statSync(path.join(/* turbopackIgnore: true */ pub, TARBALL_HREF.replace(/^\//, ""))).isFile()) return null; if (manifest.history && !isFile(path.join(/* turbopackIgnore: true */ pub, "source", HISTORY_DIR, "log.html"))) { const without: SourceManifest = { ...manifest }; delete without.history; return without; } return manifest; } catch { return null; } } function isFile(p: string): boolean { try { return fs.statSync(p).isFile(); } catch { return false; } } // The directory loadSourceManifest reads: `public/`, or the one // E2E_SOURCE_PUBLIC_DIR names when it holds a manifest — but ONLY outside a // production build. The homepage e2e's `next dev` points it at a fixture // publish its spec writes and removes (e2e/fixture-source.ts), so the page's // states with and without a history are exercised whatever this checkout has // published; with the fixture gone, the page reads `public/` again. `next // build` runs with NODE_ENV=production and ignores it (the summary.ts rule). export function sourcePublicDir( env: Readonly>, publicDir: string, ): string { const fixture = env.NODE_ENV !== "production" ? env.E2E_SOURCE_PUBLIC_DIR : undefined; if (fixture && isFile(path.join(/* turbopackIgnore: true */ fixture, "source", "manifest.json"))) return fixture; return publicDir; }