2017-04-23 20:02:26 +02:00
|
|
|
'use strict';
|
2017-04-17 16:10:21 +02:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
const fs = require('fs');
|
2017-04-17 16:10:21 +02:00
|
|
|
const Path = require('path');
|
|
|
|
|
|
|
|
class LocalData {
|
|
|
|
|
|
|
|
constructor(path) {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.path = path;
|
2017-04-17 16:10:21 +02:00
|
|
|
try {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.data = JSON.parse(fs.readFileSync(this.path, 'utf8'));
|
2017-04-17 16:10:21 +02:00
|
|
|
} catch(_) {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.data = {list: []};
|
2017-04-17 16:10:21 +02:00
|
|
|
}
|
2014-11-24 20:46:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-17 16:10:21 +02:00
|
|
|
add(name) {
|
|
|
|
if (this.data.list.indexOf(name) === -1) {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.data.list.push(name);
|
|
|
|
this.sync();
|
2017-04-17 16:10:21 +02:00
|
|
|
}
|
2014-11-24 20:46:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-17 16:10:21 +02:00
|
|
|
remove(name) {
|
2017-04-23 20:02:26 +02:00
|
|
|
const i = this.data.list.indexOf(name);
|
2017-04-17 16:10:21 +02:00
|
|
|
if (i !== -1) {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.data.list.splice(i, 1);
|
2017-04-17 16:10:21 +02:00
|
|
|
}
|
2017-04-23 20:02:26 +02:00
|
|
|
this.sync();
|
2014-11-24 20:46:37 +01:00
|
|
|
}
|
2015-03-28 19:25:53 +01:00
|
|
|
|
2017-04-17 16:10:21 +02:00
|
|
|
get() {
|
2017-04-23 20:02:26 +02:00
|
|
|
return this.data.list;
|
2017-04-17 16:10:21 +02:00
|
|
|
}
|
2014-11-24 20:46:37 +01:00
|
|
|
|
2017-04-17 16:10:21 +02:00
|
|
|
sync() {
|
|
|
|
// Uses sync to prevent ugly race condition
|
|
|
|
try {
|
2017-04-23 20:02:26 +02:00
|
|
|
require('mkdirp').sync(Path.dirname(this.path));
|
2017-04-17 16:10:21 +02:00
|
|
|
} catch(err) {}
|
2017-04-23 20:02:26 +02:00
|
|
|
fs.writeFileSync(this.path, JSON.stringify(this.data));
|
2017-04-17 16:10:21 +02:00
|
|
|
}
|
2014-11-24 20:46:37 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-17 16:10:21 +02:00
|
|
|
module.exports = LocalData;
|