mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-16 21:56:25 -05:00
6a778e8c17
close #155, see reasons there This is a huge commit, so let me know if it will cause any trouble, I might consider reverting it if it's the case.
40 lines
776 B
JavaScript
40 lines
776 B
JavaScript
var fs = require('fs')
|
|
|
|
module.exports = LocalList
|
|
|
|
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 = []
|
|
}
|
|
return self
|
|
}
|
|
|
|
LocalList.prototype.add = function(name) {
|
|
if (this.list.indexOf(name) === -1) {
|
|
this.list.push(name)
|
|
this.sync()
|
|
}
|
|
}
|
|
|
|
LocalList.prototype.remove = function(name) {
|
|
var i = this.list.indexOf(name)
|
|
if (i !== -1) {
|
|
this.list.splice(i, 1)
|
|
}
|
|
|
|
this.sync()
|
|
}
|
|
|
|
LocalList.prototype.get = function() {
|
|
return this.list
|
|
}
|
|
|
|
LocalList.prototype.sync = function() {
|
|
// Uses sync to prevent ugly race condition
|
|
fs.writeFileSync(this.path, JSON.stringify({ list: this.list }))
|
|
}
|
|
|