Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2010-08-03 10:50:15 +0400
committerisaacs <i@izs.me>2010-08-03 10:50:15 +0400
commitf9c9fcdc3b8ef845feddbcb9b41bb8ca8b025843 (patch)
tree89d019c36e967d57cc2e9aec41325dba2cc74f6e /lib/update.js
parentced7eb2bdcf4cb1d20bfdeccef9a2f92c6d1d371 (diff)
Add the update and update-dependents commands
Diffstat (limited to 'lib/update.js')
-rw-r--r--lib/update.js99
1 files changed, 99 insertions, 0 deletions
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()
+ })
+ })
+ })
+}
+