import fs from 'node:fs/promises'; import path from 'node:path'; export async function readJson(file, fallback = null) { try { return JSON.parse(await fs.readFile(file, 'utf8')); } catch { return fallback; } } export async function writeJson(file, data) { await fs.mkdir(path.dirname(file), { recursive: true }); const tmp = `${file}.tmp`; await fs.writeFile(tmp, JSON.stringify(data, null, 2)); await fs.rename(tmp, file); // atomic replace }