this repo has no description
at main 61 lines 1.6 kB view raw
1import { access } from "node:fs/promises"; 2import { join } from "node:path"; 3import { pathToFileURL } from "node:url"; 4import type { ExportConfig } from "./types.ts"; 5 6const CONFIG_FILENAMES = ["sitebase.config.ts", "sitebase.config.js"]; 7 8/** 9 * Find config file in directory (auto-discovery) 10 */ 11export async function findConfigFile(dir: string): Promise<string | null> { 12 for (const filename of CONFIG_FILENAMES) { 13 const configPath = join(dir, filename); 14 try { 15 await access(configPath); 16 return configPath; 17 } catch { 18 // File doesn't exist, try next 19 } 20 } 21 return null; 22} 23 24/** 25 * Load export config from a JS/TS file 26 */ 27export async function loadExportConfig( 28 configPath: string, 29): Promise<ExportConfig> { 30 const configUrl = pathToFileURL(configPath).href; 31 const configModule = await import(configUrl); 32 const config = configModule.default as ExportConfig; 33 34 // Validate required fields 35 if (!config.publicationUri) { 36 throw new Error("Config missing required field: publicationUri"); 37 } 38 if ( 39 !config.exports || 40 !Array.isArray(config.exports) || 41 config.exports.length === 0 42 ) { 43 throw new Error( 44 "Config missing required field: exports (must be non-empty array)", 45 ); 46 } 47 48 // Validate each export target 49 for (const [i, target] of config.exports.entries()) { 50 if (!target.outputDir) { 51 throw new Error(`Export target ${i} missing required field: outputDir`); 52 } 53 if (!target.filename || typeof target.filename !== "function") { 54 throw new Error( 55 `Export target ${i} missing required field: filename (must be a function)`, 56 ); 57 } 58 } 59 60 return config; 61}