2014-05-07 10:10:59 -05:00
|
|
|
var fs = require('fs')
|
2014-05-06 17:40:21 -05:00
|
|
|
|
2014-09-06 15:50:34 -05:00
|
|
|
function LocalList(path) {
|
|
|
|
var self = Object.create(LocalList.prototype)
|
|
|
|
self.path = path
|
|
|
|
try {
|
|
|
|
self.list = JSON.parse(fs.readFileSync(self.path, 'utf8')).list
|
|
|
|
} catch(_) {
|
|
|
|
self.list = []
|
2014-05-06 17:40:21 -05:00
|
|
|
}
|
2014-09-06 15:50:34 -05:00
|
|
|
return self
|
|
|
|
}
|
2014-05-06 17:40:21 -05:00
|
|
|
|
|
|
|
LocalList.prototype = {
|
|
|
|
add: function(name) {
|
2014-09-06 15:50:34 -05:00
|
|
|
if (this.list.indexOf(name) === -1) {
|
|
|
|
this.list.push(name)
|
|
|
|
this.sync()
|
2014-05-06 17:40:21 -05:00
|
|
|
}
|
|
|
|
},
|
2014-05-08 16:48:15 -05:00
|
|
|
remove: function(name) {
|
2014-09-06 15:50:34 -05:00
|
|
|
var i = this.list.indexOf(name)
|
|
|
|
if (i !== -1) {
|
|
|
|
this.list.splice(i, 1)
|
2014-05-06 17:40:21 -05:00
|
|
|
}
|
|
|
|
|
2014-09-06 15:50:34 -05:00
|
|
|
this.sync()
|
2014-05-06 17:40:21 -05:00
|
|
|
},
|
|
|
|
get: function() {
|
2014-09-06 15:50:34 -05:00
|
|
|
return this.list
|
2014-05-06 17:40:21 -05:00
|
|
|
},
|
|
|
|
sync: function() {
|
2014-09-06 15:50:34 -05:00
|
|
|
fs.writeFileSync(this.path, JSON.stringify({list: this.list})); //Uses sync to prevent ugly race condition
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = LocalList
|
2014-05-06 17:40:21 -05:00
|
|
|
|