mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-03-18 02:22:46 -05:00
Migrate Storages to classes
This commit is contained in:
parent
e6561e10d4
commit
d79f12d45a
7 changed files with 51 additions and 47 deletions
|
@ -37,7 +37,7 @@ function Config(config) {
|
|||
assert.equal(typeof(config), 'object', 'CONFIG: it doesn\'t look like a valid config file')
|
||||
|
||||
assert(self.storage, 'CONFIG: storage path not defined')
|
||||
self.localList = LocalData(
|
||||
self.localList = new LocalData(
|
||||
Path.join(
|
||||
Path.resolve(Path.dirname(self.self_path || ''), self.storage),
|
||||
'.sinopia-db.json'
|
||||
|
|
|
@ -1,44 +1,46 @@
|
|||
var fs = require('fs')
|
||||
var Path = require('path')
|
||||
"use strict";
|
||||
|
||||
module.exports = LocalData
|
||||
const fs = require('fs');
|
||||
const Path = require('path');
|
||||
|
||||
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: [] }
|
||||
class LocalData {
|
||||
|
||||
constructor(path) {
|
||||
this.path = path
|
||||
try {
|
||||
this.data = JSON.parse(fs.readFileSync(this.path, 'utf8'))
|
||||
} catch(_) {
|
||||
this.data = { list: [] }
|
||||
}
|
||||
}
|
||||
return self
|
||||
}
|
||||
|
||||
LocalData.prototype.add = function(name) {
|
||||
if (this.data.list.indexOf(name) === -1) {
|
||||
this.data.list.push(name)
|
||||
add(name) {
|
||||
if (this.data.list.indexOf(name) === -1) {
|
||||
this.data.list.push(name)
|
||||
this.sync()
|
||||
}
|
||||
}
|
||||
|
||||
remove(name) {
|
||||
const i = this.data.list.indexOf(name)
|
||||
if (i !== -1) {
|
||||
this.data.list.splice(i, 1)
|
||||
}
|
||||
this.sync()
|
||||
}
|
||||
}
|
||||
|
||||
LocalData.prototype.remove = function(name) {
|
||||
var i = this.data.list.indexOf(name)
|
||||
if (i !== -1) {
|
||||
this.data.list.splice(i, 1)
|
||||
get() {
|
||||
return this.data.list
|
||||
}
|
||||
|
||||
sync() {
|
||||
// Uses sync to prevent ugly race condition
|
||||
try {
|
||||
require('mkdirp').sync(Path.dirname(this.path))
|
||||
} catch(err) {}
|
||||
fs.writeFileSync(this.path, JSON.stringify(this.data))
|
||||
}
|
||||
|
||||
this.sync()
|
||||
}
|
||||
|
||||
LocalData.prototype.get = function() {
|
||||
return this.data.list
|
||||
}
|
||||
|
||||
LocalData.prototype.sync = function() {
|
||||
// Uses sync to prevent ugly race condition
|
||||
try {
|
||||
require('mkdirp').sync(Path.dirname(this.path))
|
||||
} catch(err) {}
|
||||
fs.writeFileSync(this.path, JSON.stringify(this.data))
|
||||
}
|
||||
|
||||
module.exports = LocalData;
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
var fs = require('fs')
|
||||
var Error = require('http-errors')
|
||||
var mkdirp = require('mkdirp')
|
||||
var Path = require('path')
|
||||
var MyStreams = require('./streams')
|
||||
"use strict";
|
||||
|
||||
const fs = require('fs')
|
||||
const Error = require('http-errors')
|
||||
const mkdirp = require('mkdirp')
|
||||
const Path = require('path')
|
||||
const MyStreams = require('./streams')
|
||||
|
||||
function FSError(code) {
|
||||
var err = Error(code)
|
||||
|
|
|
@ -19,11 +19,11 @@ var info_file = 'package.json'
|
|||
// Implements Storage interface
|
||||
// (same for storage.js, local-storage.js, up-storage.js)
|
||||
//
|
||||
function Storage(config) {
|
||||
var self = Object.create(Storage.prototype)
|
||||
self.config = config
|
||||
self.logger = Logger.logger.child({ sub: 'fs' })
|
||||
return self
|
||||
class Storage {
|
||||
constructor(config) {
|
||||
this.config = config
|
||||
this.logger = Logger.logger.child({ sub: 'fs' })
|
||||
}
|
||||
}
|
||||
|
||||
// returns the minimal package file
|
||||
|
|
|
@ -15,7 +15,7 @@ class Search {
|
|||
query(q) {
|
||||
return q === '*'
|
||||
? this.storage.config.localList.get().map( function( pkg ) {
|
||||
return { ref: pkg, score: 1 };
|
||||
return { ref: pkg, score: 1 };
|
||||
}) : this.index.search(q);
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ function Storage(config) {
|
|||
self.uplinks[p] = Proxy(config.uplinks[p], config)
|
||||
self.uplinks[p].upname = p
|
||||
}
|
||||
self.local = Local(config)
|
||||
self.local = new Local(config)
|
||||
self.logger = Logger.logger.child()
|
||||
|
||||
return self
|
||||
|
|
|
@ -27,7 +27,7 @@ var packages = [
|
|||
_npmUser: {
|
||||
name: 'test_user',
|
||||
}
|
||||
}
|
||||
},
|
||||
]
|
||||
|
||||
describe('search', function() {
|
||||
|
|
Loading…
Add table
Reference in a new issue