You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

2 years ago
const Store = require('./Store');
const isEmpty = require('./isEmpty');
const debounce = require('./debounce');
const stringify = require('./stringify');
const isObj = require('./isObj');
const defaults = require('./defaults');
const noop = require('./noop');
const fs = require('fs');
exports = Store.extend({
initialize: function FileStore(path, data) {
this._path = path;
data = data || {};
let fileData;
if (fs.existsSync(path)) {
try {
fileData = JSON.parse(fs.readFileSync(path, 'utf8'));
} catch (e) {
fileData = {};
}
}
if (!isObj(fileData)) {
fileData = {};
}
data = defaults(fileData, data);
this.save = debounce(data => {
if (isEmpty(data)) {
fs.unlink(this._path, noop);
return;
}
fs.writeFile(this._path, stringify(data), 'utf8', noop);
}, 300);
this.callSuper(Store, 'initialize', [data]);
}
});
module.exports = exports;