API for the ledger command-line interface (ledger-cli.org).
API for the Ledger command-line interface (ledger-cli.org).
Ledger is a powerful, double-entry accounting system that is accessed from the UNIX command-line.
The simplest way to install Ledger 3 is through Homebrew.
brew install ledger --HEAD
--HEAD option is required to install version 3.x.
ledger-cli and its dependencies with npm.
npm install ledger-cli
Then require the library and use the exported Ledger class to execute commands.
var Ledger = require'ledger-cli'Ledger;
You must provide the path to the Ledger journal file via the
var ledger = file: 'path/to/ledger/journal/file.dat' ;
There are five available Ledger commands.
accounts- Lists all accounts for postings.
balance- Reports the current balance of all accounts.
register- Displays all the postings occurring in a single account.
stats- Retrieves statistics, like number of unique accounts.
version- Gets the currently installed Ledger version number.
Lists all accounts for postings. It returns a readable object
ledgeraccountson'data'// account is the name of an account (e.g. 'Assets:Current Account');
The balance command reports the current balance of all accounts. It returns a readable object
ledgerbalanceon'data'// JSON object for each entryentry =total:currency: '£'amount: 1000formatted: '£1,000.00'account:fullname: 'Assets:Checking'shortname: 'Assets:Checking'depth: 2;once'end'// completedonce'error'// error;
The print command formats the full list of transactions, ordered by date, using the same format as they would appear in a Ledger data file. It returns a readable stream.
var fs = require'fs'out = fscreateWriteStream'output.dat';ledgerprintpipeout;
The register command displays all the postings occurring in a single account. It returns a readable object
ledgerregisteron'data'// JSON object for each entryentry =date: 2014 1 1cleared: truepending: truepayee: 'Salary'postings:commodity:currency: '£'amount: 1000formatted: '£1,000.00'account: 'Assets:Checking';once'end'// completedonce'error'// error;
The stats command is used to retrieve statistics about the Ledger data file. It requires a Node style callback function that is called with either an error or the stats object.
ledgerstatsif err return console.errorerr;// stats is a map (e.g. stats['Unique accounts'] = 13);
The version command is used to get the Ledger binary version. It requires a Node style callback function that is called with either an error or the version number as a string.
ledgerversionif err return console.errorerr;// version is a string (e.g. '3.0.0-20130529');