import path from 'node:path'; import { readJson, writeJson, ensureDir } from '../utils/fs.js'; export interface PageAssetManifest { js?: string; css?: string; } export interface AssetManifest { pages: Record; shared?: SharedAssets; } export interface SharedAssets { css?: string; js?: string; } const MANIFEST_FILENAME = 'manifest.json'; export async function updatePageManifest( directory: string, pageName: string, updater: (value: PageAssetManifest) => void, ): Promise { const manifestPath = path.join(directory, MANIFEST_FILENAME); await ensureDir(directory); const manifest = (await readJson(manifestPath)) ?? { pages: {} }; const pageManifest: PageAssetManifest = manifest.pages[pageName] ?? {}; updater(pageManifest); manifest.pages[pageName] = pageManifest; await writeJson(manifestPath, manifest); } export async function readPageManifest( directory: string, pageName: string, ): Promise { const manifestPath = path.join(directory, MANIFEST_FILENAME); const manifest = (await readJson(manifestPath)) ?? { pages: {} }; return manifest.pages[pageName] ?? {}; } export async function updateSharedAssets( directory: string, updater: (value: SharedAssets) => void, ): Promise { const manifestPath = path.join(directory, MANIFEST_FILENAME); await ensureDir(directory); const manifest = (await readJson(manifestPath)) ?? { pages: {} }; const shared: SharedAssets = manifest.shared ?? {}; updater(shared); manifest.shared = shared; await writeJson(manifestPath, manifest); } export async function readSharedAssets(directory: string): Promise { const manifestPath = path.join(directory, MANIFEST_FILENAME); const manifest = await readJson(manifestPath); return manifest?.shared ?? null; }