Skip to content

Commit

Permalink
locate hook and registry separation completion
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Oct 14, 2014
1 parent 88003ea commit 0b8739f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
13 changes: 7 additions & 6 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,15 @@ var install = exports.install = function(name, target, options) {
// to be sure we're not overriding something important
var oldDep, oldMap, oldVersions;

return Promise.resolve(target)

return pkg.locate(target, options.override)

// if not a primary install, and we have something that satisfies this already, then use that
.then(function() {
// otherwise do the full lookup
.then(function(located) {
target = located.target;
options.override = located.override;

if (options.primary)
return;

Expand Down Expand Up @@ -299,10 +304,6 @@ var install = exports.install = function(name, target, options) {
.then(function(_lookup) {
lookup = _lookup;

// allow the lookup to provide an override
if (lookup.override && !options.override)
options.override = lookup.override;

if (!options.primary)
return;

Expand Down
4 changes: 3 additions & 1 deletion lib/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ process.on('exit', function() {
METHODS
parse (name)
-> { package, path }
locate (packageName, versionRange, override)
-> { found: true/false, [override] } / { redirect: 'new:package', [override] }
lookup (packageName)
-> { notfound: true } / { redirect: 'new:package' } / { versions: {...} }
-> { notfound: true } / { versions: {...} }
download (packageName, version, hash, dir)
-> Promise pjson
getPackageConfig (packageName, version, hash), optional
Expand Down
47 changes: 43 additions & 4 deletions lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ Package.prototype.setVersion = function(version) {
this.exactPackage = this.package + v;
this.exactName = this.name + v;
}
Package.prototype.setEndpoint = function(endpoint) {
if (this.endpoint)
throw 'Endpoint already set.';
this.endpoint = endpoint;
this.exactName = endpoint + ':' + this.exactName;
this.name = endpoint + ':' + this.name;
}

var link = require('./link');

Expand All @@ -87,6 +94,39 @@ function extend(a, b) {

var _pkg = module.exports;


// given a name like 'jquery', 'github:repo/thatwasmoved'
// add the default registry endpoint to the name
// so we now have 'jspm:jquery', 'github:repo/thatwasmoved'
// then run the locate hook (if provided) of the endpoint
// following redirects until the locate hook converges
// getting 'github:components/jquery' and 'github:repo/redirected'
// at this point, we have the final name for the target
// return value is { target, override }
exports.locate = function(target, override) {
if (!target.endpoint)
target.setEndpoint(config.globalConfig.registry);

var endpoint = ep.load(target.endpoint);

if (!endpoint.locate)
return Promise.resolve({ target: target, override: override });

return Promise.resolve(endpoint.locate(target.package, target.version, override))
.then(function(located) {
if (located.redirect) {
var newTarget = new Package(located.redirect);
newTarget.setVersion(target.version);
return _pkg.locate(newTarget, located.override || override);
}
if (!located.found)
throw 'Repo `' + target.name + '` not found!';
if (located.found)
return { target: target, override: located.override || override };
throw 'Invalid endpoint locate response for %' + target.endpoint + '%';
});
}

// lookup the latest suitable version for this package range
// if the range is a tag or prerelease, then it is just that
// if the range is compatible with a prerelease, then it is just that
Expand All @@ -110,11 +150,12 @@ exports.lookup = function(pkg) {
return lookupCache[pkg.package] = Promise.resolve(ep.load(pkg.endpoint).lookup(pkg.package));
})
.then(function(lookup) {
if (lookup.redirect)
return exports.lookup(new Package(lookup.redirect));
if (lookup.notfound)
throw 'Repo `' + pkg.package + '` not found!';

if (!lookup.versions)
throw 'Invalid endpoint lookup response for %' + pkg.endpoint + '%';

return _pkg.getVersionMatch(pkg, lookup.versions);
})
// return the lookup
Expand All @@ -124,8 +165,6 @@ exports.lookup = function(pkg) {

var lookup = new Package(pkg.name + '@' + lookupObj.version);
lookup.hash = lookupObj.hash;
if (lookupObj.override)
lookup.override = lookupObj.override;
return lookup;
});
}
Expand Down

0 comments on commit 0b8739f

Please sign in to comment.