Permalink
Browse files

change repositories setting to an array and add ability to resolve de…

…pendencies using multiple repositories
  • Loading branch information...
1 parent afa3604 commit 761b637abe732e1f8828536b9c3dc79b75cd394a @caolan committed Sep 30, 2010
View
@@ -22,6 +22,7 @@ var logger = require('../logger'),
module.exports = function (args, options) {
var name = (args.length < 1) ? '.': args[0];
+ var loc = args[1] || '';
settings.loadSettings(name, function(err, settings){
if (err) return logger.error(err);
@@ -31,7 +32,7 @@ module.exports = function (args, options) {
if (err) return logger.error(err);
logger.debug('package:', pkg);
- repository.putPackage(settings, '', pkg, function (err) {
+ repository.putPackage(settings, args[1], pkg, function (err) {
if (err) return logger.error(err);
logger.end(pkg.package.name + '@' + pkg.package.version);
});
@@ -25,32 +25,7 @@ var couchdb = require('../couchdb'),
*/
exports.resolveInstance = function (settings, str) {
- if (str) {
- if (str in settings.repositories) {
- // a named repository from settings
- var ins = settings.repositories[str];
- //ins.protocol = ins.protocol || 'http:';
- //ins.slashes = true;
- return ins;
- }
- else {
- return str;
- // a repository URL
- /*var parsed = url.parse(str);
- return {
- protocol: parsed.protocol || 'http:',
- slashes: true,
- hostname: parsed.hostname,
- port: parsed.port,
- db: (parsed.pathname || '').substr(1)
- };*/
- }
- }
- // use default repository
- var ins = settings.repositories[settings.default_repository];
- ins.protocol = ins.protocol || 'http:';
- ins.slashes = true;
- return ins;
+ return str || settings.repositories[0];
};
/**
@@ -121,9 +96,11 @@ exports.getPackageID = function (instance, pkg, /*optional*/ver, callback) {
if (err) return callback(err);
var rows = data.rows;
if (!rows.length) {
- return callback(new Error(
+ // package id not found
+ return callback(null, null);
+ /*return callback(new Error(
'Could not find package: ' + pkg + (ver ? ' (' + ver + ')': '')
- ));
+ ));*/
}
// sort rows by version number, highest to lowest
rows = rows.sort(function (a, b) {
@@ -139,9 +116,11 @@ exports.getPackageID = function (instance, pkg, /*optional*/ver, callback) {
return callback(null, rows[i].value);
}
}
- return callback(new Error(
+ // package id not found
+ return callback(null, null);
+ /*return callback(new Error(
'Could not find package: ' + pkg + ' (' + ver.join(', ') + ')'
- ));
+ ));*/
}
else callback(null, rows[0].value);
});
@@ -222,7 +201,7 @@ exports.getMetadata = function (settings, loc, callback) {
*/
exports.putPackage = function (settings, loc, pkg, callback) {
- var ins = exports.resolveInstance(settings, '');
+ var ins = exports.resolveInstance(settings, loc);
var id = pkg.package.name + '-' + pkg.package.version;
var attachments = pkg._attachments;
@@ -231,13 +210,15 @@ exports.putPackage = function (settings, loc, pkg, callback) {
var db = couchdb(ins);
db.save(id, pkg, {}, function (err, d) {
if (err) return callback(err);
- var keys = Object.keys(attachments);
+
+ var keys = Object.keys(attachments || {});
async.reduce(keys, d.rev, function (rev, k, cb) {
- var path = d.id + '/' + k;
+ var path = id + '/' + k;
db.upload(path, attachments[k], rev, function (err, r) {
- if(!err) logger.info('uploaded', attachments[k]);
- cb(err, r);
- });
+ if(!err) logger.info('uploaded', attachments[k]);
+ cb(err, r);
+ }
+ );
}, callback);
});
};
@@ -252,13 +233,11 @@ exports.putPackage = function (settings, loc, pkg, callback) {
*/
exports.deletePackage = function (settings, loc, callback) {
- var ins = exports.resolveInstance(settings, '');
-
var split = exports.splitLocation(loc);
var name = split.name;
var ver = split.version;
- exports.getPackageID(ins, name, ver, function (err, id) {
+ exports.getRepository(settings, name, [ver], function (err, ins, id) {
if (err) return callback(err);
couchdb(ins).delete(id, null, {force: true}, callback);
});
@@ -275,7 +254,7 @@ exports.deletePackage = function (settings, loc, callback) {
*/
exports.list = function (settings, loc, callback) {
- var ins = exports.resolveInstance(settings, '');
+ var ins = exports.resolveInstance(settings, loc);
couchdb(ins).get('_design/repository/_view/packages',
function(err, data) {
@@ -293,6 +272,41 @@ exports.list = function (settings, loc, callback) {
};
/**
+ * Checks all repositories in settings for package with a given version.
+ * Passes to the callback the repository url and id of the package document,
+ * returns an exception if the package cannot be found.
+ *
+ * @param {Object} settings
+ * @param {String} name
+ * @param {Array} versions
+ * @param {Function} callback
+ * @api public
+ */
+
+exports.getRepository = function (settings, name, versions, callback) {
+ var ins, pkg_id;
+ async.detectSeries(settings.repositories, function (r, cb) {
+ exports.getPackageID(r, name, versions, function (err, id) {
+ if (id) {
+ ins = r;
+ pkg_id = id;
+ }
+ cb(err, id);
+ });
+ },
+ function (err) {
+ if (err) return callback(err);
+ if (!pkg_id) {
+ return callback(new Error(
+ 'Could not find package: ' +
+ name + (ver ? ' (' + ver + ')': '')
+ ));
+ }
+ callback(null, ins, pkg_id);
+ });
+};
+
+/**
* Given a version or range of versions, attempt to get the most recent
* matching package from the repository.
*
@@ -304,9 +318,7 @@ exports.list = function (settings, loc, callback) {
*/
exports.getByVersion = function (settings, name, versions, callback) {
- var ins = exports.resolveInstance(settings, '');
-
- exports.getPackageID(ins, name, versions, function (err, id) {
+ exports.getRepository(settings, name, versions, function (err, ins, id) {
if (err) return callback(err);
couchdb(ins).get(id, function (err, doc) {
View
@@ -44,10 +44,10 @@ exports.lists = {
};
exports.validate_doc_update = function (newDoc, savedDoc, userCtx) {
- if (!newDoc._deleted) {
- if (!newDoc.package) {
+ if (!newDoc._deleted && newDoc.package) {
+ /*if (!newDoc.package) {
throw({forbidden: 'Packages must contain package metadata'});
- }
+ }*/
if(!newDoc.package.version) {
throw({forbidden: 'Packages must have a version number'});
}
@@ -0,0 +1,5 @@
+var dep_test_lib_rewrites = require('../../dep_test_lib/lib/rewrites');
+
+exports.rewrites = [
+ {'from': '/', 'to': dep_test_lib_rewrites}
+];
@@ -0,0 +1,12 @@
+{
+ "name": "dep_test2",
+ "description": "Dependency test package 2",
+ "version": "0.1.0",
+ "app": "lib/app",
+ "paths": {
+ "modules": ["lib"]
+ },
+ "dependencies": {
+ "dep_test_lib": ["0.0.4"]
+ }
+}
Oops, something went wrong.

0 comments on commit 761b637

Please sign in to comment.