From ef610ab7dea4f2c58f80e49dfe68f6a05e9c317d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Cruz?= Date: Wed, 5 Jun 2013 01:13:15 +0100 Subject: [PATCH] Sort of picks must be done by the Manager, otherwise indexes are messed up. Little code simplification on the Project. --- lib/core/Manager.js | 92 ++++++++++++++++++++----------- lib/core/Project.js | 34 ++++-------- lib/renderers/StandardRenderer.js | 28 ---------- 3 files changed, 70 insertions(+), 84 deletions(-) diff --git a/lib/core/Manager.js b/lib/core/Manager.js index f00e575e..52842dbe 100644 --- a/lib/core/Manager.js +++ b/lib/core/Manager.js @@ -472,47 +472,73 @@ Manager.prototype._electSuitable = function (name, semvers, nonSemvers) { picks.push.apply(picks, nonSemvers); } - // If there are picks, the user needs to choose between them - if (picks.length) { - // If interactive is disabled, error out - if (!this._config.interactive) { - throw createError('Unable to find suitable version for ' + name, 'ECONFLICT', { - picks: picks - }); + // If there are no picks, resolve to the suitable one + if (!picks.length) { + return Q.resolve(suitable); + + } + + // Sort picks by version/release + picks.sort(function (pick1, pick2) { + var version1 = pick1.pkgMeta.version; + var version2 = pick2.pkgMeta.version; + + // If both have versions, compare their versions using semver + if (version1 && version2) { + if (semver.gt(version1, version2)) { + return 1; + } + if (semver.lt(version1, version2)) { + return -1; + } + return 0; } - dataPicks = picks.map(function (pick) { - return { - endpoint: mout.object.pick(pick, ['name', 'source', 'target']), - pkgMeta: pick.pkgMeta, - canonicalPkg: pick.canonicalPkg, - dependants: pick.dependants.map(function (dependant) { - return { - endpoint: mout.object.pick(dependant, ['name', 'source', 'target']), - pkgMeta: dependant.pkgMeta, - canonicalPkg: dependant.canonicalPkg - }; - }) - }; - }); + // Give priority to the one that is a version + if (version1) { + return 1; + } + if (version2) { + return -1; + } - this._logger.conflict('incompatible', 'Unable to find suitable version for ' + name, { - picks: dataPicks, - name: name - }); + return 0; + }); - // Question the user - choices = picks.map(function (pick, index) { - return index + 1; - }); + dataPicks = picks.map(function (pick) { + return { + endpoint: mout.object.pick(pick, ['name', 'source', 'target']), + pkgMeta: pick.pkgMeta, + canonicalPkg: pick.canonicalPkg, + dependants: pick.dependants.map(function (dependant) { + return { + endpoint: mout.object.pick(dependant, ['name', 'source', 'target']), + pkgMeta: dependant.pkgMeta, + canonicalPkg: dependant.canonicalPkg + }; + }) + }; + }); - return Q.nfcall(promptly.choose, 'Choice:', choices) - .then(function (choice) { - return picks[choice - 1]; + // If interactive is disabled, error out + if (!this._config.interactive) { + throw createError('Unable to find suitable version for ' + name, 'ECONFLICT', { + name: name, + picks: dataPicks }); } - return Q.resolve(suitable); + // Otherwise, question the user + this._logger.conflict('incompatible', 'Unable to find suitable version for ' + name, { + name: name, + picks: dataPicks + }); + + choices = picks.map(function (pick, index) { return index + 1; }); + return Q.nfcall(promptly.choose, 'Choice:', choices) + .then(function (choice) { + return picks[choice - 1]; + }); }; Manager.prototype._getCap = function (comparators, side) { diff --git a/lib/core/Project.js b/lib/core/Project.js index 7e23dddd..40c37a9f 100644 --- a/lib/core/Project.js +++ b/lib/core/Project.js @@ -28,7 +28,6 @@ Project.prototype.install = function (endpoints, options) { var that = this; var targets = []; var resolved = {}; - var installed; // If already working, error out if (this._working) { @@ -60,19 +59,12 @@ Project.prototype.install = function (endpoints, options) { // Add endpoints as targets if (endpoints) { endpoints.forEach(function (endpoint) { - var decEndpoint = endpointParser.decompose(endpoint); - targets[decEndpoint.name] = decEndpoint; + targets.push(endpointParser.decompose(endpoint)); }); } - // Mark installed - installed = mout.object.map(flattened, function (decEndpoint) { - return decEndpoint.pkgMeta; - }); - }) - // Bootstrap the process - .then(function () { - return that._bootstrap(mout.object.values(targets), resolved, installed); + // Bootstrap the process + return that._bootstrap(targets, resolved, flattened); }) // Handle save and saveDev options .then(function (installed) { @@ -112,7 +104,6 @@ Project.prototype.update = function (names, options) { var that = this; var targets = []; var resolved = {}; - var installed; // If already working, error out if (this._working) { @@ -162,8 +153,7 @@ Project.prototype.update = function (names, options) { node.walked = true; }); - // Mark extraneous as targets only if - // it's not already a target + // Mark extraneous as targets only if it's not already a target mout.object.forOwn(flattened, function (decEndpoint) { var foundTarget; var name = decEndpoint.name; @@ -180,14 +170,8 @@ Project.prototype.update = function (names, options) { }); } - // Mark installed - installed = mout.object.map(flattened, function (decEndpoint) { - return decEndpoint.pkgMeta; - }); - }) - // Bootstrap the process - .then(function () { - return that._bootstrap(targets, resolved, installed); + // Bootstrap the process + return that._bootstrap(targets, resolved, flattened); }) .fin(function () { that._working = false; @@ -338,7 +322,11 @@ Project.prototype.analyse = function () { // ----------------- -Project.prototype._bootstrap = function (targets, resolved, installed) { +Project.prototype._bootstrap = function (targets, resolved, flattened) { + var installed = mout.object.map(flattened, function (decEndpoint) { + return decEndpoint.pkgMeta; + }); + // Configure the manager and kick in the resolve process return this._manager .setProduction(this._production) diff --git a/lib/renderers/StandardRenderer.js b/lib/renderers/StandardRenderer.js index c3182e7b..6471413f 100644 --- a/lib/renderers/StandardRenderer.js +++ b/lib/renderers/StandardRenderer.js @@ -126,40 +126,12 @@ StandardRenderer.prototype._incompatibleLog = function (log) { // Generate dependants string for each pick log.data.picks.forEach(function (pick) { - pick.dependants = pick.dependants.map(function (dependant) { var release = dependant.pkgMeta._release; return dependant.endpoint.name + (release ? '#' + release : ''); }).join(', '); }); - // Sort picks by version/release - log.data.picks.sort(function (pick1, pick2) { - var version1 = pick1.pkgMeta.version; - var version2 = pick2.pkgMeta.version; - - // If both have versions, compare their versions using semver - if (version1 && version2) { - if (semver.gt(version1, version2)) { - return 1; - } - 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; - } - - return 0; - }); - str = template.render('std/incompatible.std', log.data); this._write(process.stdout, '\n');