Fix adjustment made to targets and improve walk tree.

This commit is contained in:
André Cruz
2013-06-05 18:35:38 +01:00
parent 0ebe5ff55a
commit 1fdb5373c9
2 changed files with 38 additions and 33 deletions

View File

@@ -240,9 +240,14 @@ Manager.prototype._onFetchSuccess = function (decEndpoint, canonicalPkg, pkgMeta
decEndpoint.pkgMeta = pkgMeta;
delete decEndpoint.promise;
// If the fetched endpoint target is '*' and it resolved to a version,
// adjust the target
if (decEndpoint.target === '*' && decEndpoint.pkgMeta.version) {
decEndpoint.target = '~' + decEndpoint.pkgMeta.version;
}
// Add to the resolved list, marking it as resolved
// If after resolving there's an exact equal endpoint,
// replace instead of adding
// If there's an exact equal endpoint, replace instead of adding
resolved = this._resolved[name] = this._resolved[name] || [];
index = mout.array.findIndex(resolved, function (resolved) {
return resolved.target === decEndpoint.target;
@@ -265,12 +270,6 @@ Manager.prototype._onFetchSuccess = function (decEndpoint, canonicalPkg, pkgMeta
}
}
// If the fetched endpoint is an initial target and the target is '*',
// make the target equal to ~version
if (decEndpoint.pkgMeta.version) {
decEndpoint.target = '~' + decEndpoint.pkgMeta.version;
}
// Parse dependencies
this._parseDependencies(decEndpoint, pkgMeta, 'dependencies');
// Do the same for the dev dependencies
@@ -502,15 +501,14 @@ Manager.prototype._electSuitable = function (name, semvers, nonSemvers) {
if (semver.lt(version1, version2)) {
return -1;
}
return 0;
}
// Give priority to the one that is a version
if (version1) {
return 1;
}
if (version2) {
return -1;
} else {
// Give priority to the one that is a version
if (version1) {
return 1;
}
if (version2) {
return -1;
}
}
return 0;

View File

@@ -43,18 +43,12 @@ Project.prototype.install = function (endpoints, options) {
// Walk down the tree adding missing and incompatible
// as targets
that._walkTree(tree, function (node, name) {
if (node.walked) {
return;
}
if (node.missing || node.incompatible) {
targets.push(node);
} else {
resolved[name] = node;
}
node.walked = true;
});
}, true);
// Add endpoints as targets
if (endpoints) {
@@ -138,10 +132,6 @@ Project.prototype.update = function (names, options) {
// Walk down the tree adding missing, incompatible
// and names as targets
that._walkTree(tree, function (node, name) {
if (node.walked) {
return;
}
if (node.missing || node.incompatible) {
targets.push(node);
} else if (names.indexOf(name) !== -1) {
@@ -149,9 +139,7 @@ Project.prototype.update = function (names, options) {
} else {
resolved[name] = node;
}
node.walked = true;
});
}, true);
// Mark extraneous as targets only if it's not already a target
mout.object.forOwn(flattened, function (decEndpoint) {
@@ -489,19 +477,38 @@ Project.prototype._removePackages = function (packages, options) {
});
};
Project.prototype._walkTree = function (node, fn) {
Project.prototype._walkTree = function (node, fn, onlyOnce) {
var queue = mout.object.values(node.dependencies);
var result;
var deps;
if (onlyOnce === true) {
onlyOnce = [];
}
while (queue.length) {
node = queue.shift();
result = fn(node, node.name);
if (onlyOnce) {
onlyOnce.push(node);
}
// Abort traversal if result is false
if (result === false) {
continue;
}
queue.unshift.apply(queue, mout.object.values(node.dependencies));
// Add dependencies to the queue
deps = mout.object.values(node.dependencies);
// If onlyOnce was true, do not add if already traversed
if (onlyOnce) {
deps = deps.filter(function (dep) {
return onlyOnce.indexOf(dep) === -1;
});
}
queue.unshift.apply(queue, deps);
}
};