import { access } from "node:fs/promises"; import { join } from "node:path"; import { pathToFileURL } from "node:url"; import type { ExportConfig } from "./types.ts"; const CONFIG_FILENAMES = ["sitebase.config.ts", "sitebase.config.js"]; /** * Find config file in directory (auto-discovery) */ export async function findConfigFile(dir: string): Promise { for (const filename of CONFIG_FILENAMES) { const configPath = join(dir, filename); try { await access(configPath); return configPath; } catch { // File doesn't exist, try next } } return null; } /** * Load export config from a JS/TS file */ export async function loadExportConfig( configPath: string, ): Promise { const configUrl = pathToFileURL(configPath).href; const configModule = await import(configUrl); const config = configModule.default as ExportConfig; // Validate required fields if (!config.publicationUri) { throw new Error("Config missing required field: publicationUri"); } if ( !config.exports || !Array.isArray(config.exports) || config.exports.length === 0 ) { throw new Error( "Config missing required field: exports (must be non-empty array)", ); } // Validate each export target for (const [i, target] of config.exports.entries()) { if (!target.outputDir) { throw new Error(`Export target ${i} missing required field: outputDir`); } if (!target.filename || typeof target.filename !== "function") { throw new Error( `Export target ${i} missing required field: filename (must be a function)`, ); } } return config; }