invoice-fs
Version:
Nostalgic command-line invoicing application producing plain text invoices and JSON data structures. Uses the file system as a database
35 lines (26 loc) • 782 B
JavaScript
var obj = require('javascript-object-paraphernalia'),
fs = require('fs'),
appStateFile = './data/application.json',
writeDataSync = require('./writeDataSync');
function State() {
this.createFileIfNeeded(appStateFile);
obj.merge(this, JSON.parse(fs.readFileSync(appStateFile, 'utf8')));
}
State.prototype.createFileIfNeeded = function(filename) {
try {
fs.lstatSync(filename);
} catch (e) {
fs.writeFileSync(filename, "");
}
};
State.prototype.set = function(name, value, callback) {
this[name] = value;
this.save(callback);
};
State.prototype.get = function(name) {
return this[name];
};
State.prototype.save = function(callback) {
writeDataSync(appStateFile, this, callback);
};
module.exports = new State();