2014-11-24 19:16:58 -05:00
|
|
|
var fs = require('fs')
|
|
|
|
var Path = require('path')
|
2014-11-24 14:46:37 -05:00
|
|
|
|
|
|
|
module.exports = LocalData
|
|
|
|
|
|
|
|
function LocalData(path) {
|
|
|
|
var self = Object.create(LocalData.prototype)
|
|
|
|
self.path = path
|
|
|
|
try {
|
|
|
|
self.data = JSON.parse(fs.readFileSync(self.path, 'utf8'))
|
|
|
|
} catch(_) {
|
|
|
|
self.data = { list: [] }
|
|
|
|
}
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalData.prototype.add = function(name) {
|
|
|
|
if (this.data.list.indexOf(name) === -1) {
|
|
|
|
this.data.list.push(name)
|
|
|
|
this.sync()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalData.prototype.remove = function(name) {
|
|
|
|
var i = this.data.list.indexOf(name)
|
|
|
|
if (i !== -1) {
|
|
|
|
this.data.list.splice(i, 1)
|
|
|
|
}
|
2015-03-28 13:25:53 -05:00
|
|
|
|
2014-11-24 14:46:37 -05:00
|
|
|
this.sync()
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalData.prototype.get = function() {
|
|
|
|
return this.data.list
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalData.prototype.sync = function() {
|
|
|
|
// Uses sync to prevent ugly race condition
|
2014-11-24 19:16:58 -05:00
|
|
|
try {
|
|
|
|
require('mkdirp').sync(Path.dirname(this.path))
|
|
|
|
} catch(err) {}
|
2014-11-24 14:46:37 -05:00
|
|
|
fs.writeFileSync(this.path, JSON.stringify(this.data))
|
|
|
|
}
|
|
|
|
|