diff options
author | isaacs <i@izs.me> | 2010-05-03 01:51:43 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2010-05-03 05:07:49 +0400 |
commit | 5199ca30e51e9f182d586dc168f1bb2091157a88 (patch) | |
tree | ea19df24e9c49d713941b325f3428ce822168ad2 /lib | |
parent | 84ee391f2817a6d56a9293abbabd76a000338ec9 (diff) |
Abstract out the tree-walking bits from ls so that it can be reused elsewhere.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ls.js | 44 | ||||
-rw-r--r-- | lib/utils/read-installed.js | 47 |
2 files changed, 57 insertions, 34 deletions
@@ -2,44 +2,20 @@ // show the installed versions of a package module.exports = exports = ls + var npm = require("../npm") - , path = require("path") - , fs = require("fs") , log = require("./utils/log") + , readInstalled = require("./utils/read-installed") function ls (args, cb) { - var pkg = args.shift() - fs.readdir(npm.dir, function (er, packages) { - if (er) return cb(er) - packages = packages.filter(function (dir) { - return (pkg ? dir === pkg : true) && dir.charAt(0) !== "." - }) - if (!packages.length) { - if (pkg) log(pkg, "not found") - else log("nothing installed") + var showActive = (npm.config.get("show") === "active") + || (npm.config.get("active") === true) + readInstalled(args, function (er, data) { + for (var pkg in data) for (var ver in data[pkg]) { + if (showActive && !data[pkg][ver].active) continue + log(pkg+" "+ver + + (data[pkg][ver].active ? " \033[33mactive\033[0m" : "") + , "installed") } - var p = packages.length - function listed () { if (--p === 0) cb() } - packages.forEach(function (package) { - var packageDir = path.join(npm.dir, package) - , activeVersion = null - fs.readdir(packageDir, function (er, versions) { - if (er) return cb(er) - versions - .filter(function (version) { - if (version !== "active") return true - var active = path.join(packageDir, "active") - if (fs.lstatSync(active).isSymbolicLink()) { - activeVersion = path.basename(fs.readlinkSync(active)) - } - }) - .forEach(function (version) { - log(package+" "+version + - (activeVersion === version - ? " \033[33mactive\033[0m" : ""), "installed") - }) - listed() - }) - }) }) } diff --git a/lib/utils/read-installed.js b/lib/utils/read-installed.js new file mode 100644 index 000000000..f72ef033e --- /dev/null +++ b/lib/utils/read-installed.js @@ -0,0 +1,47 @@ + +// Walk through the file-system "database" of installed +// packages, and create a data object related to the +// installed/active versions of each package. + +var npm = require("../../npm") + , fs = require("fs") + , path = require("path") + +module.exports = readInstalled + +function readInstalled (args, cb) { + var showAll = args.length === 0 + fs.readdir(npm.dir, function (er, packages) { + if (er) return cb(er) + packages = packages.filter(function (dir) { + return (showAll || args.indexOf(dir) !== -1) && dir.charAt(0) !== "." + }) + // maybe nothing found + if (!packages.length) cb(null, null) + + var p = packages.length + , data = {} + function listed () { if (--p === 0) cb(null, data) } + packages.forEach(function (package) { + var packageDir = path.join(npm.dir, package) + , activeVersion = null + fs.readdir(packageDir, function (er, versions) { + if (er) return cb(er) + versions + .filter(function (version) { + if (version !== "active") return true + var active = path.join(packageDir, "active") + if (fs.lstatSync(active).isSymbolicLink()) { + activeVersion = path.basename(fs.readlinkSync(active)) + } + }) + .forEach(function (version) { + data[package] = data[package] || {} + data[package][version] = data[package][version] || {} + if (activeVersion === version) data[package][version].active = true + }) + listed() + }) + }) + }) +} |