import fs from "node:fs"; import path from "node:path"; import { createReportReader, reportMetadata } from "yt-dlp-transcript-common/lib/report/reader-server"; import { ARTICLES_REPORTS_BASE } from "yt-dlp-transcript-common/lib/report/routes"; // THE ARTICLES (release 26 A3): the reports the project homepage publishes, // read by the shared reader (common/lib/report/reader-server.ts) under // /articles/ from what `compose homepage` wrote into public/ — articles/, // m/ and media/, through the same reports stage a site's compose runs // (publish/composeReports.ts over publish/reportHost.ts articlesReportHost). // // A build:nodata tree has none: every reader answers "none", /articles/ // renders its empty index and the nav stays unconditional (./nav.ts). // // E2E_ARTICLES_PUBLIC_DIR names a fixture public dir to read instead — but // ONLY outside a production build, and only while it holds an article index // (the source.ts rule): `next build` runs with NODE_ENV=production. export function articlesPublicDir( env: Readonly> = process.env, publicDir: string = path.join(/* turbopackIgnore: true */ process.cwd(), "public"), ): string { const fixture = env.NODE_ENV !== "production" ? env.E2E_ARTICLES_PUBLIC_DIR : undefined; if (fixture && fs.existsSync(path.join(/* turbopackIgnore: true */ fixture, "articles", "index.json"))) return fixture; return publicDir; } const reader = createReportReader({ publicDir: () => articlesPublicDir(), base: ARTICLES_REPORTS_BASE }); export const ARTICLES_ROUTE_BASE = reader.base; export const readArticleIndex = () => reader.readReportIndex(); export const articleIds = () => reader.reportIds(); export const readArticleView = (id: string) => reader.readReportView(id); export const readArticleHistory = (id: string) => reader.readReportHistory(id); export const momentKeys = () => reader.momentKeys(); export const readMomentView = (key: string) => reader.readMomentView(key); // The newest few, for the home page: the index's order is the operator's // (reports/index.json), so "latest" is by date, newest first. export function latestArticles(n: number) { const reports = readArticleIndex()?.reports ?? []; const at = (r: { updated?: string; published?: string }) => Date.parse(r.updated ?? r.published ?? "") || 0; return [...reports].sort((a, b) => at(b) - at(a)).slice(0, n); } export { reportMetadata };