Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
timaschew committed Jan 21, 2015
1 parent 72f4bff commit e983777
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
51 changes: 46 additions & 5 deletions lib/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,46 @@ function Remote(options) {
*/

Remote.prototype.resolve = function* (remotes, repo, ref) {
// TODO: this is ugly
if (typeof remotes === 'string') {
ref = repo;
repo = remotes;
} else if (Array.isArray(remotes) && !~remotes.indexOf(this.name)) {
// if the current remote is not in this list,
// then it's obviously not valid.
return;
}
var versions = yield* this.getAvailableVersions(remotes, repo, ref);

// TODO: this looks not good, should return null or something like that
if (versions == null) versions = ['master'];

// add master, if there are no versions published
if (versions.length === 0) versions.push('master');

// TODO: are there use cases where ref is not defined?
var reference = ref || versions[0];

var json = yield* this.json(repo, reference);
if (json) return this;
};

Remote.prototype.isValid = function* (remotes, repo, ref) {
var versions = yield* this.getAvailableVersions(remotes, repo, ref);
if (versions == null) return false;
// return true if there are some version, or there are no version published (empty array)
return true;
};

/**
* There are two use cases for this function:
* 1. Just to check if a repo exist via isValid()
* 2. Get available versions
*
* @return {this}
* @api public
*/
Remote.prototype.getAvailableVersions = function* (remotes, repo, ref) {
if (typeof remotes === 'string') {
ref = repo;
repo = remotes;
Expand All @@ -87,11 +127,7 @@ Remote.prototype.resolve = function* (remotes, repo, ref) {
}
// use latest tag, and if it's not available, then master
var availableVersions = yield* this.versions(repo);
if (availableVersions == null || availableVersions.length == 0) {
availableVersions = ['master'];
}
var json = yield* this.json(repo, ref || availableVersions[0]);
if (json) return this;
return availableVersions;
};

/**
Expand All @@ -104,6 +140,11 @@ Remote.prototype.resolve = function* (remotes, repo, ref) {
*/

Remote.prototype.versions = function* (repo) {
if (!repo) {
console.log("repo: "+repo);
console.log(new Error().stack);
}

repo = repo.toLowerCase();
var event = 'version:' + repo;
var cache = this.c_versions;
Expand Down
32 changes: 30 additions & 2 deletions lib/remotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,33 @@ Remotes.prototype.use = function (remote) {
* @api public
*/

Remotes.prototype.check = function* (remotes, name, reference) {
var length = this.remotes.length;
if (!length) throw new Error('no remotes');

// resolve(name, [reference])
if (typeof remotes === 'string') {
reference = name;
name = remotes;
remotes = null;
}

remotes = remotes || this.remotes.map(toName);

// map to remote instances
var instances = remotes.map(function (name) {
return this.remote[name];
}, this).filter(Boolean);
remotes = instances.map(toName);

// loop through
for (var i = 0; i < instances.length; i++) {
if (yield* instances[i].isValid(remotes, name, reference)) {
return instances[i];
}
}
};

Remotes.prototype.resolve = function* (remotes, name, reference) {
var length = this.remotes.length;
if (!length) throw new Error('no remotes');
Expand All @@ -96,10 +123,11 @@ Remotes.prototype.resolve = function* (remotes, name, reference) {
remotes = instances.map(toName);

// loop through
for (var i = 0; i < instances.length; i++)
for (var i = 0; i < instances.length; i++) {
if (yield* instances[i].resolve(remotes, name, reference))
return instances[i];
}
}
};

function toName(x) {
return x.name;
Expand Down

0 comments on commit e983777

Please sign in to comment.