0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00
verdaccio/lib/local-data.js

47 lines
821 B
JavaScript
Raw Normal View History

'use strict';
2017-04-17 09:10:21 -05:00
const fs = require('fs');
2017-04-17 09:10:21 -05:00
const Path = require('path');
class LocalData {
constructor(path) {
this.path = path;
2017-04-17 09:10:21 -05:00
try {
this.data = JSON.parse(fs.readFileSync(this.path, 'utf8'));
2017-04-17 09:10:21 -05:00
} catch(_) {
this.data = {list: []};
2017-04-17 09:10:21 -05:00
}
}
2017-04-17 09:10:21 -05:00
add(name) {
if (this.data.list.indexOf(name) === -1) {
this.data.list.push(name);
this.sync();
2017-04-17 09:10:21 -05:00
}
}
2017-04-17 09:10:21 -05:00
remove(name) {
const i = this.data.list.indexOf(name);
2017-04-17 09:10:21 -05:00
if (i !== -1) {
this.data.list.splice(i, 1);
2017-04-17 09:10:21 -05:00
}
this.sync();
}
2017-04-17 09:10:21 -05:00
get() {
return this.data.list;
2017-04-17 09:10:21 -05:00
}
2017-04-17 09:10:21 -05:00
sync() {
// Uses sync to prevent ugly race condition
try {
require('mkdirp').sync(Path.dirname(this.path));
2017-04-17 09:10:21 -05:00
} catch(err) {}
fs.writeFileSync(this.path, JSON.stringify(this.data));
2017-04-17 09:10:21 -05:00
}
}
2017-04-17 09:10:21 -05:00
module.exports = LocalData;