import { readFile, access } from "node:fs/promises"; import { createJiti } from "jiti"; import { join } from "node:path"; import type { Config } from "./config"; import { isBun } from "std-env"; export const jiti = createJiti(import.meta.url); export async function exists(path: string | URL) { try { await access(path); return true; } catch { return false; } } export async function readJson(path: string | URL) { if (isBun) return await Bun.file(path).json(); return JSON.parse((await readFile(path)).toString()); } export async function loadConfig() { const extensions = [".js", ".ts", ".mjs", ".mts", ".json"]; let selectedExt: string | undefined = undefined; for (const ext of extensions) { if (await exists(join(process.cwd(), `/leaflet-md.config${ext}`))) { selectedExt = ext; break; } } if (!selectedExt) return undefined; const configPath = join(process.cwd(), `/leaflet-md.config${selectedExt}`); const config = (await jiti.import(configPath, { default: true })) as Config; return config; }