import fs from "fs/promises"; export async function readJsonFile(filePath, fallback = []) { try { const content = await fs.readFile(filePath, "utf-8"); const parsed = JSON.parse(content); return parsed ?? fallback; } catch (error) { if (error.code === "ENOENT") { await fs.writeFile(filePath, JSON.stringify(fallback, null, 2)); return fallback; } throw error; } } export async function writeJsonFile(filePath, data) { await fs.writeFile(filePath, JSON.stringify(data, null, 2)); }