diff --git a/lib/config.js b/lib/config.js index 7176e456b..626b43b0c 100644 --- a/lib/config.js +++ b/lib/config.js @@ -3,6 +3,7 @@ var assert = require('assert') , Path = require('path') , minimatch = require('minimatch') , UError = require('./error').UserError + , LocalList = require('./local-list') , utils = require('./utils') // [[a, [b, c]], d] -> [a, b, c, d] @@ -28,6 +29,7 @@ function Config(config) { assert.equal(typeof(config), 'object', 'CONFIG: this doesn\'t look like a valid config file') assert(this.storage, 'CONFIG: storage path not defined') + this.localList = LocalList(Path.join(this.storage, '.sinopia-db.json')) var users = {all:true, anonymous:true, 'undefined':true, owner:true, none:true} diff --git a/lib/index-web.js b/lib/index-web.js index cfecafa39..eced0f856 100644 --- a/lib/index-web.js +++ b/lib/index-web.js @@ -2,7 +2,6 @@ var fs = require('fs') var marked = require('marked') var search = require('./search') var Handlebars = require('handlebars') -var localList = require('./local-list') module.exports = function(app, config, storage) { search.configureStorage(storage) diff --git a/lib/local-list.js b/lib/local-list.js index 64d5bbd04..c4092644f 100644 --- a/lib/local-list.js +++ b/lib/local-list.js @@ -1,36 +1,38 @@ var fs = require('fs') - , listFilePath = './local-list.json'; -var LocalList = function() { - if(fs.existsSync(listFilePath)) { - this.list = JSON.parse(fs.readFileSync(listFilePath, 'utf8')); +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 = [] } - else { - this.list = []; - } -}; + return self +} LocalList.prototype = { add: function(name) { - if(this.list.indexOf(name) == -1) { - this.list.push(name); - this.sync(); + if (this.list.indexOf(name) === -1) { + this.list.push(name) + this.sync() } }, remove: function(name) { - var i = this.list.indexOf(name); - if(i != -1) { - this.list.splice(i, 1); + var i = this.list.indexOf(name) + if (i !== -1) { + this.list.splice(i, 1) } - this.sync(); + this.sync() }, get: function() { - return this.list; + return this.list }, sync: function() { - fs.writeFileSync(listFilePath, JSON.stringify(this.list)); //Uses sync to prevent ugly race condition - } -}; + fs.writeFileSync(this.path, JSON.stringify({list: this.list})); //Uses sync to prevent ugly race condition + }, +} + +module.exports = LocalList -module.exports = new LocalList(); diff --git a/lib/local-storage.js b/lib/local-storage.js index 8447b14c3..4878b2e6d 100644 --- a/lib/local-storage.js +++ b/lib/local-storage.js @@ -8,7 +8,6 @@ var fs = require('fs') , mystreams = require('./streams') , Logger = require('./logger') , info_file = 'package.json' - , localList = require('./local-list') , targz = require('tar.gz') , search = require('./search'); @@ -49,11 +48,13 @@ Storage.prototype._internal_error = function(err, file, message) { } Storage.prototype.add_package = function(name, info, callback) { + var self = this var storage = this.storage(name) if (!storage) return callback(new UError({ status: 404, message: 'this package cannot be added' })) + storage.create_json(info_file, get_boilerplate(name), function(err) { if (err && err.code === 'EEXISTS') { return callback(new UError({ @@ -66,8 +67,6 @@ Storage.prototype.add_package = function(name, info, callback) { if (latest && info.versions[latest]) { search.add(info.versions[latest]) } - localList.add(name) - callback() }) } @@ -118,7 +117,7 @@ Storage.prototype.remove_package = function(name, callback) { }); search.remove(name); - localList.remove(name); + this.config.localList.remove(name); } Storage.prototype._read_create_package = function(name, callback) { @@ -248,6 +247,7 @@ Storage.prototype.add_version = function(name, version, metadata, tag, callback) data.versions[version] = metadata utils.tag_version(data, version, tag, self.config) + self.config.localList.add(name) cb() }, callback) } diff --git a/lib/search.js b/lib/search.js index 82bd27122..3363affca 100644 --- a/lib/search.js +++ b/lib/search.js @@ -1,5 +1,4 @@ var lunr = require('lunr') - , localList = require('./local-list'); var Search = function() { this.index = lunr(function () { diff --git a/lib/storage.js b/lib/storage.js index 5a019b582..e5f47944e 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -6,7 +6,6 @@ var async = require('async') , mystreams = require('./streams') , utils = require('./utils') , Logger = require('./logger') - , localList = require('./local-list'); // // Implements Storage interface @@ -419,14 +418,20 @@ Storage.prototype.search = function(startkey, options, callback) { Storage.prototype.get_local = function(callback) { var self = this - , locals = localList.get() + , locals = this.config.localList.get() , packages = []; var getPackage = function(i) { self.local.get_package(locals[i], function(err, info) { if (!err) { - var latest = info['dist-tags'].latest; - if (info.versions[latest]) packages.push(info.versions[latest]); + var latest = Array.isArray(info['dist-tags'].latest) + ? utils.semver_sort(info['dist-tags'].latest)[0] + : info['dist-tags'].latest + if (info.versions[latest]) { + packages.push(info.versions[latest]) + } else { + self.logger.warn({package: locals[i]}, 'package @{package} does not have a "latest" tag?') + } } if (i >= locals.length - 1) {