UNPKG

ledgerline

Version:

A modern interactive CLI to track expenses, stored locally in JSON.

30 lines (29 loc) 907 B
import path from "node:path"; import os from "node:os"; import fs from "fs-extra"; const dataDirectoryPath = path.join(os.homedir(), ".ledgerline"); const dataFilePath = path.join(dataDirectoryPath, "expenses.json"); export async function loadExpenses() { const exists = await fs.pathExists(dataFilePath); if (!exists) return []; const raw = await fs.readFile(dataFilePath, "utf-8"); try { const parsed = JSON.parse(raw); if (Array.isArray(parsed)) return parsed; if (parsed && Array.isArray(parsed.expenses)) return parsed.expenses; return []; } catch { return []; } } export async function saveExpenses(expenses) { await fs.ensureDir(dataDirectoryPath); await fs.writeFile(dataFilePath, JSON.stringify(expenses, null, 2), "utf-8"); } export function getDataFilePath() { return dataFilePath; }