import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { type Config, ConfigSchema, DEFAULT_CONFIG } from "./types";
const CONFIG_FILE = ".doclea/config.json";
export function loadConfig(projectPath: string = process.cwd()): Config {
const configPath = join(projectPath, CONFIG_FILE);
if (!existsSync(configPath)) {
return DEFAULT_CONFIG;
}
try {
const rawConfig = JSON.parse(readFileSync(configPath, "utf-8"));
return ConfigSchema.parse(rawConfig);
} catch (error) {
if (error instanceof Error) {
throw new Error(
`Failed to load config from ${configPath}: ${error.message}`,
);
}
throw error;
}
}
export function getConfigPath(projectPath: string = process.cwd()): string {
return join(projectPath, CONFIG_FILE);
}
export function getDbPath(
config: Config,
projectPath: string = process.cwd(),
): string {
return join(projectPath, config.storage.dbPath);
}