Build a Persistent Expense Tracker CLI with Reports
A command-line expense tracker that records dated expenses with categories, persists them to a CSV file, and prints monthly and per-category summaries with totals.
Project goal
Build a CLI that records expenses with an amount, category, and date, stores them in a CSV file, and reports monthly and category breakdowns with totals and averages.
Final features
- Add an expense with amount, category, and date, validated before it is stored.
- List expenses filtered by month or category.
- Summarise spending per category and per month with totals.
- Persist to CSV so the data is editable and diffable in git.
- Reject malformed amounts and dates with a clear message.
Architecture
A parser converts argv into a command object; a store reads and writes a CSV file with a header row; a report module groups records and computes totals; and a formatter prints aligned tables. Amounts are stored in minor units as integers to avoid floating-point drift.
Prerequisites
- Node.js 20 LTS or later
- Comfort with string parsing and date handling
- A basic understanding of monetary rounding
Dependencies
- node >=20 — File system access and the CLI entry point
- vitest ^2.1.0 — Parsing, storage, and reporting tests
Directory structure
expense-cli/
├── src/
│ ├── parse.js
│ ├── money.js
│ ├── store.js
│ ├── report.js
│ └── cli.js
├── test/
│ ├── money.test.js
│ └── report.test.js
└── package.json
Phases
1. Argument parsing
Translate argv into a command object with validated subcommands and options.
Files: src/parse.js
export function parseArgs(argv) {
const [command, ...rest] = argv;
const options = {};
const positionals = [];
for (let i = 0; i < rest.length; i++) {
const token = rest[i];
if (token.startsWith('--')) {
const [key, inline] = token.slice(2).split('=');
options[key] = inline ?? rest[++i];
} else positionals.push(token);
}
return { command, options, positionals };
}
export const COMMANDS = new Set(['add', 'list', 'report']);Expected behavior: Both --month=2026-01 and --month 2026-01 parse to the same option, repeated positionals are preserved, and an unknown command is rejected by the caller.
2. Money module
Represent amounts as integer minor units and provide exact parse and format functions.
Files: src/money.js
export function parseAmount(input) {
const match = /^(-?)(\d+)(?:\.(\d{1,2}))?$/.exec(String(input).trim());
if (!match) throw new Error('invalid amount: ' + input);
const [, sign, whole, frac = ''] = match;
const cents = Number(whole) * 100 + Number((frac + '00').slice(0, 2));
return sign === '-' ? -cents : cents;
}
export function sum(amounts) {
return amounts.reduce((total, cents) => total + cents, 0);
}Expected behavior: Parsing 19.99 gives 1999 cents, parsing 19.9 gives 1990, a negative sign is preserved, and summing many values never produces a fractional cent.
3. CSV store
Read and write a header-row CSV ledger with atomic writes and tolerant parsing of quoted fields.
Files: src/store.js
import { readFile, writeFile, rename } from 'node:fs/promises';
const HEADER = ['date', 'category', 'amountCents', 'note'];
export function toCsv(records) {
const rows = [HEADER.join(',')];
for (const r of records) rows.push([r.date, r.category, r.amountCents, escapeCsv(r.note ?? '')].join(','));
return rows.join('\n') + '\n';
}
export function fromCsv(text) {
return text.trim().split('\n').slice(1).filter(Boolean).map((line) => {
const [date, category, amountCents, ...note] = parseCsvLine(line);
return { date, category, amountCents: Number(amountCents), note: note.join(',') };
});
}
function escapeCsv(value) {
return /[",\n]/.test(value) ? '"' + value.replace(/"/g, '""') + '"' : value;
}
function parseCsvLine(line) {
return line.match(/("([^"]|"")*"|[^,]*)(,|$)/g).slice(0, -1).map((f) =>
f.replace(/,$/, '').replace(/^"|"$/g, '').replace(/""/g, '"'));
}Expected behavior: A record with a comma in the note survives a CSV round-trip, and the header line is written exactly once.
4. Report grouping
Group records by category and by month and compute totals and averages as pure functions.
Files: src/report.js
export function byCategory(records) {
const groups = new Map();
for (const r of records) {
const bucket = groups.get(r.category) ?? { category: r.category, total: 0, count: 0 };
bucket.total += r.amountCents;
bucket.count += 1;
groups.set(r.category, bucket);
}
return [...groups.values()].sort((a, b) => b.total - a.total);
}
export function byMonth(records) {
const groups = new Map();
for (const r of records) {
const month = r.date.slice(0, 7);
groups.set(month, (groups.get(month) ?? 0) + r.amountCents);
}
return [...groups.entries()].map(([month, total]) => ({ month, total }))
.sort((a, b) => a.month.localeCompare(b.month));
}Expected behavior: Grouping a ledger with two categories and two months returns exact totals for each, and ties in category totals sort by a deterministic key.
5. CLI surface
Dispatch subcommands, validate inputs, and print aligned tables or summaries.
Files: src/cli.js
import { parseArgs, COMMANDS } from './parse.js';
import { parseAmount, formatAmount } from './money.js';
import { fromCsv, toCsv } from './store.js';
import { byCategory } from './report.js';
const { command, options } = parseArgs(process.argv.slice(2));
if (!COMMANDS.has(command)) {
console.error('usage: expense <add|list|report> [options]');
process.exit(1);
}
if (command === 'add') {
const cents = parseAmount(options.amount ?? '');
console.log('recorded ' + formatAmount(cents) + ' in ' + options.category);
} else if (command === 'report') {
for (const g of byCategory([])) console.log(g.category.padEnd(12) + formatAmount(g.total));
}Expected behavior: Adding with a malformed amount exits non-zero with a message, and reporting prints one aligned line per category with totals.
6. Tests and hardening
Cover parsing, money, storage, and reporting, and fix any failure the tests reveal.
Files: test/report.test.js
import { byCategory, byMonth } from '../src/report.js';
const ledger = [
{ date: '2026-01-02', category: 'food', amountCents: 1250 },
{ date: '2026-01-15', category: 'travel', amountCents: 8000 },
{ date: '2026-02-03', category: 'food', amountCents: 900 },
];
test('category totals add up', () => {
expect(byCategory(ledger)).toEqual([
{ category: 'travel', total: 8000, count: 1 },
{ category: 'food', total: 2150, count: 2 },
]);
});
test('month totals split by month', () => {
expect(byMonth(ledger)).toEqual([
{ month: '2026-01', total: 9250 },
{ month: '2026-02', total: 900 },
]);
});Expected behavior: The report suite passes, a malformed amount throws before writing anything, and the ledger round-trips through CSV unchanged.
Testing
Run npx vitest. Money tests cover parsing, rounding, negatives, and formatting round-trips. Store tests round-trip records through a temporary CSV and assert the header is written once. Report tests group a fixture ledger by month and category and assert exact totals, including a month with no expenses.
Troubleshooting
Totals drift by a cent after many entries
Store amounts as integer minor units and sum them as integers; converting to floats at any intermediate step reintroduces the rounding the integer representation exists to avoid.
A note containing a comma breaks the CSV rows
Quote any field containing a comma, quote, or newline and double embedded quotes; the escape must happen on write and the unescape on read, symmetrically.
Monthly grouping is off by one day near midnight
Store the date as a plain ISO day string and group on its first seven characters, avoiding time-zone conversion entirely.
Deployment
Install as a global CLI or run from a checkout. The CSV ledger is portable and reviewable, so a team can keep it in a shared repository and read the diffs in a pull request.
Known limitations
- A single CSV file is fine for personal use but is not designed for concurrent writers.
- There is no currency conversion; amounts are assumed to share one currency.
Project overview
Expense tracking is a small problem that hides two classic pitfalls: floating-point money and date grouping across time zones. This project fixes both up front so the reports can be trusted.
Amounts are stored as integer minor units, dates as ISO days, and the data lives in a CSV file you can open in a spreadsheet or diff in git. Reports regroup the same records without re-reading anything.
Money handling is the decision that affects every number the tool prints, so it gets its own tiny module:
export function parseAmount(input) {
const match = /^(-?)(\d+)(?:\.(\d{1,2}))?$/.exec(String(input).trim());
if (!match) throw new Error('invalid amount: ' + input);
const [, sign, whole, frac = ''] = match;
const cents = Number(whole) * 100 + Number((frac + '00').slice(0, 2));
return sign === '-' ? -cents : cents;
}
export function formatAmount(cents) {
const sign = cents < 0 ? '-' : '';
const abs = Math.abs(cents);
return sign + Math.floor(abs / 100) + '.' + String(abs % 100).padStart(2, '0');
}
Parsing to integer cents once at the boundary means every later sum is exact, and formatting back is the only place that knows about a decimal point.
Setup
The standard library covers everything this tool needs, so the install is a single test dependency.
| Dependency | Version | Why it is here |
|---|---|---|
| node | >=20 | File system access and the CLI entry point |
| vitest | ^2.1.0 | Parsing, storage, and reporting tests |
Create the project and install:
mkdir expense-cli && cd expense-cli
npm init -y
npm install -D vitest@^2.1.0
Configure package.json with the CLI and test scripts:
{
"name": "expense-cli",
"type": "module",
"scripts": {
"expense": "node src/cli.js",
"test": "vitest run"
}
}
Point the store at a path from an environment variable so tests write to a temporary file instead of your real ledger.
Phase 1: Argument parsing
Translate argv into a command object with validated subcommands and options. Because the phase is complete on its own, the tutorial can be paused and resumed at its boundaries without losing the thread.
export function parseArgs(argv) {
const [command, ...rest] = argv;
const options = {};
const positionals = [];
for (let i = 0; i < rest.length; i++) {
const token = rest[i];
if (token.startsWith('--')) {
const [key, inline] = token.slice(2).split('=');
options[key] = inline ?? rest[++i];
} else positionals.push(token);
}
return { command, options, positionals };
}
export const COMMANDS = new Set(['add', 'list', 'report']);
Acceptance for this phase: Both –month=2026-01 and –month 2026-01 parse to the same option, repeated positionals are preserved, and an unknown command is rejected by the caller.
Phase 2: Money module
Represent amounts as integer minor units and provide exact parse and format functions. Because the phase is complete on its own, the tutorial can be paused and resumed at its boundaries without losing the thread.
export function parseAmount(input) {
const match = /^(-?)(\d+)(?:\.(\d{1,2}))?$/.exec(String(input).trim());
if (!match) throw new Error('invalid amount: ' + input);
const [, sign, whole, frac = ''] = match;
const cents = Number(whole) * 100 + Number((frac + '00').slice(0, 2));
return sign === '-' ? -cents : cents;
}
export function sum(amounts) {
return amounts.reduce((total, cents) => total + cents, 0);
}
Acceptance for this phase: Parsing 19.99 gives 1999 cents, parsing 19.9 gives 1990, a negative sign is preserved, and summing many values never produces a fractional cent.
Phase 3: CSV store
Read and write a header-row CSV ledger with atomic writes and tolerant parsing of quoted fields. Because the phase is complete on its own, the tutorial can be paused and resumed at its boundaries without losing the thread.
import { readFile, writeFile, rename } from 'node:fs/promises';
const HEADER = ['date', 'category', 'amountCents', 'note'];
export function toCsv(records) {
const rows = [HEADER.join(',')];
for (const r of records) rows.push([r.date, r.category, r.amountCents, escapeCsv(r.note ?? '')].join(','));
return rows.join('\n') + '\n';
}
export function fromCsv(text) {
return text.trim().split('\n').slice(1).filter(Boolean).map((line) => {
const [date, category, amountCents, ...note] = parseCsvLine(line);
return { date, category, amountCents: Number(amountCents), note: note.join(',') };
});
}
function escapeCsv(value) {
return /[",\n]/.test(value) ? '"' + value.replace(/"/g, '""') + '"' : value;
}
function parseCsvLine(line) {
return line.match(/("([^"]|"")*"|[^,]*)(,|$)/g).slice(0, -1).map((f) =>
f.replace(/,$/, '').replace(/^"|"$/g, '').replace(/""/g, '"'));
}
Acceptance for this phase: A record with a comma in the note survives a CSV round-trip, and the header line is written exactly once.
Phase 4: Report grouping
Group records by category and by month and compute totals and averages as pure functions. Because the phase is complete on its own, the tutorial can be paused and resumed at its boundaries without losing the thread.
export function byCategory(records) {
const groups = new Map();
for (const r of records) {
const bucket = groups.get(r.category) ?? { category: r.category, total: 0, count: 0 };
bucket.total += r.amountCents;
bucket.count += 1;
groups.set(r.category, bucket);
}
return [...groups.values()].sort((a, b) => b.total - a.total);
}
export function byMonth(records) {
const groups = new Map();
for (const r of records) {
const month = r.date.slice(0, 7);
groups.set(month, (groups.get(month) ?? 0) + r.amountCents);
}
return [...groups.entries()].map(([month, total]) => ({ month, total }))
.sort((a, b) => a.month.localeCompare(b.month));
}
Acceptance for this phase: Grouping a ledger with two categories and two months returns exact totals for each, and ties in category totals sort by a deterministic key.
Phase 5: CLI surface
Dispatch subcommands, validate inputs, and print aligned tables or summaries. Because the phase is complete on its own, the tutorial can be paused and resumed at its boundaries without losing the thread.
import { parseArgs, COMMANDS } from './parse.js';
import { parseAmount, formatAmount } from './money.js';
import { fromCsv, toCsv } from './store.js';
import { byCategory } from './report.js';
const { command, options } = parseArgs(process.argv.slice(2));
if (!COMMANDS.has(command)) {
console.error('usage: expense <add|list|report> [options]');
process.exit(1);
}
if (command === 'add') {
const cents = parseAmount(options.amount ?? '');
console.log('recorded ' + formatAmount(cents) + ' in ' + options.category);
} else if (command === 'report') {
for (const g of byCategory([])) console.log(g.category.padEnd(12) + formatAmount(g.total));
}
Acceptance for this phase: Adding with a malformed amount exits non-zero with a message, and reporting prints one aligned line per category with totals.
Phase 6: Tests and hardening
Cover parsing, money, storage, and reporting, and fix any failure the tests reveal. Because the phase is complete on its own, the tutorial can be paused and resumed at its boundaries without losing the thread.
import { byCategory, byMonth } from '../src/report.js';
const ledger = [
{ date: '2026-01-02', category: 'food', amountCents: 1250 },
{ date: '2026-01-15', category: 'travel', amountCents: 8000 },
{ date: '2026-02-03', category: 'food', amountCents: 900 },
];
test('category totals add up', () => {
expect(byCategory(ledger)).toEqual([
{ category: 'travel', total: 8000, count: 1 },
{ category: 'food', total: 2150, count: 2 },
]);
});
test('month totals split by month', () => {
expect(byMonth(ledger)).toEqual([
{ month: '2026-01', total: 9250 },
{ month: '2026-02', total: 900 },
]);
});
Acceptance for this phase: The report suite passes, a malformed amount throws before writing anything, and the ledger round-trips through CSV unchanged.
Testing strategy
Run npx vitest. Money tests cover parsing, rounding, negatives, and formatting round-trips. Store tests round-trip records through a temporary CSV and assert the header is written once. Report tests group a fixture ledger by month and category and assert exact totals, including a month with no expenses.
The round-trip money test is the important one: parse then format must return the original string, which catches rounding bugs before they reach a total.
Extending the project
Budgets, recurring entries, and import from bank CSV each consume the same record shape. Because grouping is a pure function over records, a new report is a new function rather than a change to storage.