node/deps/npm/lib/rebuild.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

module.exports = rebuild
2012-06-16 01:00:30 +08:00
var readInstalled = require("read-installed")
, semver = require("semver")
2012-06-11 12:29:47 +08:00
, log = require("npmlog")
, path = require("path")
, npm = require("./npm.js")
2011-12-14 10:53:02 +08:00
, asyncMap = require("slide").asyncMap
, fs = require("graceful-fs")
rebuild.usage = "npm rebuild [<name>[@<version>] [name[@<version>] ...]]"
rebuild.completion = require("./utils/completion/installed-deep.js")
function rebuild (args, cb) {
2012-06-16 01:00:30 +08:00
readInstalled(npm.prefix, npm.config.get("depth"), function (er, data) {
2012-06-11 12:29:47 +08:00
log.info("readInstalled", typeof data)
if (er) return cb(er)
var set = filter(data, args)
, folders = Object.keys(set).filter(function (f) {
return f !== npm.prefix
})
if (!folders.length) return cb()
2012-06-11 12:29:47 +08:00
log.silly("rebuild set", folders)
2011-12-14 10:53:02 +08:00
cleanBuild(folders, set, cb)
})
}
function cleanBuild (folders, set, cb) {
2013-03-21 08:49:57 +08:00
npm.commands.build(folders, function (er) {
2011-12-14 10:53:02 +08:00
if (er) return cb(er)
2013-03-21 08:49:57 +08:00
console.log(folders.map(function (f) {
return set[f] + " " + f
}).join("\n"))
cb()
})
}
function filter (data, args, set, seen) {
if (!set) set = {}
if (!seen) seen = {}
if (set.hasOwnProperty(data.path)) return set
if (seen.hasOwnProperty(data.path)) return set
seen[data.path] = true
var pass
if (!args.length) pass = true // rebuild everything
else if (data.name && data._id) {
for (var i = 0, l = args.length; i < l; i ++) {
var arg = args[i]
, nv = arg.split("@")
, n = nv.shift()
, v = nv.join("@")
if (n !== data.name) continue
2013-07-10 04:09:02 +08:00
if (!semver.satisfies(data.version, v, true)) continue
pass = true
break
}
}
if (pass && data._id) {
2012-06-11 12:29:47 +08:00
log.verbose("rebuild", "path, id", [data.path, data._id])
set[data.path] = data._id
}
// need to also dive through kids, always.
// since this isn't an install these won't get auto-built unless
// they're not dependencies.
Object.keys(data.dependencies || {}).forEach(function (d) {
// return
var dep = data.dependencies[d]
if (typeof dep === "string") return
filter(dep, args, set, seen)
})
return set
}