import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' const __dirname = dirname(fileURLToPath(import.meta.url)) const DATA_DIR = join(__dirname, '..', 'data') const DB_PATH = join(DATA_DIR, 'content.json') const defaultDb = () => ({ site: {}, navigation: [], portfolioCategories: [], projects: [], services: [], serviceProjectCategories: {}, clients: [], stats: [], blogPosts: [], media: [], contactRequests: [] }) function ensureDataDir() { if (!existsSync(DATA_DIR)) mkdirSync(DATA_DIR, { recursive: true }) } export function readDb() { ensureDataDir() if (!existsSync(DB_PATH)) { const db = defaultDb() writeDb(db) return db } return JSON.parse(readFileSync(DB_PATH, 'utf-8')) } export function writeDb(db) { ensureDataDir() writeFileSync(DB_PATH, JSON.stringify(db, null, 2), 'utf-8') } export function updateDb(mutator) { const db = readDb() mutator(db) writeDb(db) return db }