diff options
author | isaacs <i@izs.me> | 2010-08-25 16:13:23 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2010-08-26 05:01:23 +0400 |
commit | ee7eb5063a466543b043d87993f925fb5a60aac0 (patch) | |
tree | 43bf66539b897d2262980d711004ebf11bc3bf2a | |
parent | 0c0447fe3099d1041fd29c84e8281181fa6a0bac (diff) |
Add a rebuild command. Fix #143
-rw-r--r-- | lib/rebuild.js | 69 | ||||
-rwxr-xr-x | npm.js | 1 |
2 files changed, 70 insertions, 0 deletions
diff --git a/lib/rebuild.js b/lib/rebuild.js new file mode 100644 index 000000000..5d74ab5a9 --- /dev/null +++ b/lib/rebuild.js @@ -0,0 +1,69 @@ + +module.exports = rebuild + +var readInstalled = require("./utils/read-installed") + , asyncMap = require("./utils/async-map") + , semver = require("./utils/semver") + , log = require("./utils/log") + , path = require("path") + , npm = require("../npm") + , loadPackageDefaults = require("./utils/load-package-defaults") + , lifecycle = require("./utils/lifecycle") + , chain = require("./utils/chain") + , readJson = require("./utils/read-json") + +function rebuild (args, cb) { + log(args, "rebuild") + lookupArgs(args, function (er, args) { + if (er) return cb(er) + asyncMap(args, function (arg, cb) { + log.verbose(arg, "rebuild") + arg = arg.split("@") + var n = arg.shift() + , v = arg.join("@") + readJson(path.join(npm.dir, n, v, "package", "package.json"), function (er, data) { + if (er) return cb(er) + data.version = v + data._id = data.name + "-" + data.version + loadPackageDefaults(data, cb) + }) + }, function (er, args) { + if (er) return cb(er) + npm.config.set("update-dependents", false) + npm.config.set("auto-activate", false) + npm.commands.build(args, cb) + }) + }) +} +// TODO: abstract this out for uninstall, etc to use. +function lookupArgs (args, cb) { + if (!args.length) return readInstalled([], function (er, inst) { + if (er) return cb(er) + var a = [] + asyncMap(Object.keys(inst), function (p, cb) { + cb(null, Object.keys(inst[p]).map(function (v) { + return p+"@"+v + })) + }, cb) + }) + var req = {} + args.forEach(function (a) { + a = a.split("@") + var n = a.shift() + , v = a.join("@") + ;(req[n] = req[n] || []).push(v) + }) + readInstalled(Object.keys(req), function (er, inst) { + asyncMap(Object.keys(inst), function (p, cb) { + if (!req.hasOwnProperty(p)) return cb() + var matches = [] + Object.keys(inst[p]).forEach(function (v) { + req[p].forEach(function (r) { + if (semver.satisfies(v, r)) matches.push(p+"@"+v), v = null + }) + }) + log.verbose(p, "rebuild") + cb(null, matches) + }, cb) + }) +} @@ -45,6 +45,7 @@ var commandCache = {} , "update-dependents" , "view" , "repl" + , "rebuild" ].forEach(function (c) { Object.defineProperty(npm.commands, c, { get : function () { c = c === "list" ? "ls" |