@vpdn/moneymoney-cli
Version:
Command-line interface for MoneyMoney app - list accounts and fetch transactions
99 lines • 3.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const applescript_1 = require("./applescript");
const formatters_1 = require("./formatters");
const fs_1 = require("fs");
const path_1 = require("path");
const program = new commander_1.Command();
const moneyMoney = new applescript_1.MoneyMoneyAppleScript();
// Read version from package.json
const packageJson = JSON.parse((0, fs_1.readFileSync)((0, path_1.join)(__dirname, '..', 'package.json'), 'utf-8'));
program
.name('moneymoney-cli')
.description('CLI tool for MoneyMoney - list accounts and fetch transactions')
.version(packageJson.version)
.option('--json', 'Output raw data as JSON (overrides format option)');
// Accounts command
program
.command('accounts')
.description('List all available accounts')
.option('-f, --format <format>', 'Output format (json|text)', 'text')
.action(async (options, command) => {
try {
const accounts = await moneyMoney.getAccounts();
// Check for global --json option
const globalOpts = command.parent?.opts() || {};
if (globalOpts.json || options.format === 'json') {
console.log(JSON.stringify(accounts, null, 2));
}
else {
console.log((0, formatters_1.formatAccounts)(accounts));
}
}
catch (error) {
console.error('Error fetching accounts:', error);
process.exit(1);
}
});
// Transactions command
program
.command('transactions')
.description('Fetch transactions for selected accounts')
.option('-a, --accounts <accounts>', 'Comma-separated list of account numbers')
.option('-f, --from <date>', 'Start date (YYYY-MM-DD)')
.option('-t, --to <date>', 'End date (YYYY-MM-DD)')
.option('-o, --format <format>', 'Output format (json|text)', 'text')
.action(async (options, command) => {
try {
// Parse options
const accountNumbers = options.accounts ? options.accounts.split(',').map((a) => a.trim()) : undefined;
const fromDate = options.from ? new Date(options.from) : undefined;
const toDate = options.to ? new Date(options.to) : undefined;
// Validate dates
if (fromDate && isNaN(fromDate.getTime())) {
throw new Error('Invalid from date format. Use YYYY-MM-DD');
}
if (toDate && isNaN(toDate.getTime())) {
throw new Error('Invalid to date format. Use YYYY-MM-DD');
}
let transactions;
if (accountNumbers && accountNumbers.length > 1) {
// Multiple accounts
transactions = await moneyMoney.getTransactionsMultipleAccounts({
accountNumbers,
fromDate,
toDate
});
}
else {
// Single account or all accounts
transactions = await moneyMoney.getTransactions({
accountNumbers,
fromDate,
toDate,
format: 'plist'
});
}
// Check for global --json option
const globalOpts = command.parent?.opts() || {};
if (globalOpts.json || options.format === 'json') {
console.log(JSON.stringify(transactions, null, 2));
}
else {
console.log((0, formatters_1.formatTransactions)(transactions));
}
}
catch (error) {
console.error('Error fetching transactions:', error);
process.exit(1);
}
});
// Parse command line arguments
program.parse(process.argv);
// Show help if no command provided
if (!process.argv.slice(2).length) {
program.outputHelp();
}
//# sourceMappingURL=index.js.map