mattermost-desktop/src/common/JsonFileManager.js

37 lines
624 B
JavaScript
Raw Normal View History

2018-01-17 07:14:33 -08:00
const fs = require('fs');
class JsonFileManager {
constructor(file) {
this.jsonFile = file;
try {
this.json = JSON.parse(fs.readFileSync(file, 'utf-8'));
} catch (err) {
this.json = {};
}
}
writeToFile() {
fs.writeFile(this.jsonFile, JSON.stringify(this.json, null, 2), (err) => {
if (err) {
console.error(err);
}
});
}
setJson(json) {
this.json = json;
this.writeToFile();
}
setValue(key, value) {
this.json[key] = value;
this.writeToFile();
}
getValue(key) {
return this.json[key];
}
}
module.exports = JsonFileManager;