Files
npm/lib/outdated.js
isaacs af9302894e refactor output to go through central module
This is the dramatic refactor which was alluded to in ed004e0.

1. All output goes through a central module.
2. Callbacks are called when data is flushed, but only ever called once
3. Set "outfd" and "logfd" to direct output and error to different places,
   or set to a writable stream when using npm programmatically.
4. Clean up the many varied ways to write data to the console.
5. Set colors smarter, and allow overriding by setting the "color" config.
2010-10-28 18:34:34 -07:00

72 lines
2.2 KiB
JavaScript

/*
npm outdated [pkg]
Does the following:
1. check for a new version of pkg
If no packages are specified, then run for all installed
packages.
*/
module.exports = outdated
outdated.usage = "npm outdated [<pkg> [<pkg> ...]]"
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")
, lifecycle = require("./utils/lifecycle")
, asyncMap = require("./utils/async-map")
, output = require("./utils/output")
function outdated (args, silent, cb) {
if (typeof silent === "function") cb = silent, silent = false
findUpdates(args, function (er, updates) {
if (er) return log.er(cb, "failed to find outdated packages")(er)
if (!updates || Object.keys(updates).length === 0) return log(
"Everything up-to-date.", "outdated", cb)
if (!silent) {
var fullList = []
updates.forEach(function (u) {
fullList.push(u.name+"@"+u.installed)
})
output.write(npm.config.get("outfd"), fullList.join("\n"), function (e) {
cb(e, updates)
})
} else cb(null, updates)
})
}
function findUpdates (args, cb) {
readInstalled(args, function (er, inst) {
if (er) return log.er(cb, "Couldn't read installed packages")(er)
var tag = npm.config.get("tag")
asyncMap(Object.keys(inst), function (pkg, cb) {
log.verbose(pkg, "find updates")
registry.get(pkg, function (er, data) {
if (er) return log.verbose(pkg, "not in registry", cb)
var latest = data["dist-tags"] && data["dist-tags"][tag]
, have = Object.keys(inst[pkg]).sort(semver.sort)
, minHave = have[0]
log.verbose(latest, pkg+"@latest")
log.verbose(minHave, pkg+" min installed")
log.verbose(semver.gt(latest, minHave), pkg+" latest > minHave")
if (!latest || !semver.gt(latest, minHave)) return cb()
// we have something that's out of date.
cb(null, { latest : latest
, installed : minHave
, have : have
, pkg : data.versions[latest]
, name : data.name
})
})
}, cb)
})
}