From 1fdb5373c9cdeb34d24d3eb64a0c024f49cd0582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Cruz?= Date: Wed, 5 Jun 2013 18:35:38 +0100 Subject: [PATCH] Fix adjustment made to targets and improve walk tree. --- lib/core/Manager.js | 32 +++++++++++++++----------------- lib/core/Project.js | 39 +++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/lib/core/Manager.js b/lib/core/Manager.js index 45f6ef0a..048ddf46 100644 --- a/lib/core/Manager.js +++ b/lib/core/Manager.js @@ -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; diff --git a/lib/core/Project.js b/lib/core/Project.js index 4e9b9569..a71c5bb8 100644 --- a/lib/core/Project.js +++ b/lib/core/Project.js @@ -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); } };