UNPKG

invoice-fs

Version:

Nostalgic command-line invoicing application producing plain text invoices and JSON data structures. Uses the file system as a database

31 lines (25 loc) 838 B
var state = require('./state'), out = require('./out'), paths = state.get('paths'), Api = require('invoice-api'), fs = require('fs'), writeDataSync = require('./writeDataSync'); function Customer(code) { this.type = 'customer'; this.open(code); } Customer.prototype.formatCode = function(code) { return code.toLowerCase(); }; Customer.prototype.open = function(code) { try { this.data = new Api.Customer(JSON.parse(fs.readFileSync(paths.customers + this.formatCode(code) + '.json', 'utf8'))); state.set('customer', this.formatCode(code)); } catch (e) { this.data = new Api.Customer(); } }; Customer.prototype.save = function(callback) { writeDataSync(paths.customers + this.data.code.toLowerCase() + ".json", this.data, callback); }; module.exports = Customer;