From f9c9fcdc3b8ef845feddbcb9b41bb8ca8b025843 Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 2 Aug 2010 23:50:15 -0700 Subject: Add the update and update-dependents commands --- lib/update.js | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 lib/update.js (limited to 'lib/update.js') diff --git a/lib/update.js b/lib/update.js new file mode 100644 index 000000000..27d642afc --- /dev/null +++ b/lib/update.js @@ -0,0 +1,99 @@ +/* +http://github.com/isaacs/npm/issues/issue/7 + +npm update [pkg] + +Does the following: + +1. check for a new version of pkg +2. if not found, then quit +3. install new version of pkg +4. For each other version of pkg, for each dependent in + other version's dependents folder, if the new version + would satisfy the dependency as well, update other + version's dependent's dependency links to point at the + new version +5. If no dependents are left, then remove old version + +If no packages are specified, then run for all installed +packages. + +Depending on config value, auto-update, run steps 4-5 +after installation + +* always - Run an update after every install, so as to + minimize the different number of versions of things. +* true - Default, run if newly installed version is + the highest version number (that is, don't downgrade + by default) +* false - Don't run "update" automatically after + installation. + +*/ + +module.exports = update + +var readInstalled = require("./utils/read-installed") + , chain = require("./utils/chain") + , log = require("./utils/log") + , registry = require("./utils/registry") + , npm = require("../npm") + , semver = require("./utils/semver") + +function update (args, cb) { + findUpdates(args, function (er, updates) { + if (er) return log.er(cb, "failed to find updates")(er) + if (!updates || Object.keys(updates).length === 0) return log( + "Nothing to update", "update", cb) + installUpdates(updates, cb) + }) +} +function installUpdates (updates, cb) { + log(updates, "install updates") + npm.config.set("auto-update", true) + var installList = [] + , updateList = [] + Object.keys(updates).forEach(function (i) { + var u = updates[i] + if (u.have.indexOf(u.latest) === -1) { + installList.push(i+"@"+u.latest) + } else { + updateList.push(i+"@"+u.latest) + } + }) + log(installList, "update installList") + log(updateList, "update updateList") + npm.commands.install(installList, function (er) { + if (er) return log.er(cb, "install failed "+installList)(er) + npm.commands["update-dependents"](updateList, function (er) { + if (er) return log.er(cb, "update failed "+updateList)(er) + cb() + }) + }) +} + +function findUpdates (args, cb) { + log(args, "findUpdates") + readInstalled(args, function (er, inst) { + if (er) return log.er(cb, "Couldn't read installed packages")(er) + log(inst, "installed") + var pkgs = Object.keys(inst) + , p = pkgs.length + , updates = {} + , tag = npm.config.get("tag") + function found () { if (--p === 0) cb(null, updates) } + pkgs.forEach(function (pkg) { + registry.get(pkg, function (er, data) { + if (er) return log(pkg, "not in registry", found) + var latest = data["dist-tags"] && data["dist-tags"][tag] + , have = Object.keys(inst[pkg]).sort(semver.sort) + , minHave = have[0] + if (!latest || !semver.gt(latest, minHave)) return found() + // we have something that's out of date. + updates[pkg] = {latest:latest,have:have} + found() + }) + }) + }) +} + -- cgit v1.2.3