1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
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")
rebuild.usage = "npm rebuild [<name>[@<version>] [name[@<version>] ...]]"
function rebuild (args, cb) {
log.verbose(args, "rebuild")
lookupArgs(args, function (er, args) {
if (er) return cb(er)
if (!args.length) return log("Nothing to do", "rebuild", cb)
var folders = []
, actives = []
args = args.reduce(function (r, l) {
Object.keys(l).forEach(function (p) {
r[p] = r[p] || {}
Object.keys(l[p]).forEach(function (v) {
if (v === "active") {
r[p].active = l[p].active
actives.push([p, l[p].active])
} else {
r[p][v] = l[p][v]
folders.push([p, v])
}
})
})
return r
}, {})
log.silly(folders,"rebuild folders")
log.silly(actives, "rebuild actives")
asyncMap(folders, function (arg, cb) {
log.verbose(arg, "rebuild")
var n = arg[0]
, v = arg[1]
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.commands.build(args, function (er) {
// now activate what was activated before, in case building
// activated a different thing (very likely)
asyncMap(actives, function (active, cb) {
npm.commands.activate([active.join("@")], cb)
}, 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) {
var o = {}
o[p] = {}
o[p][v] = inst[p][v]
if (o[p][v].active) o[p].active = v
return o
}))
}, cb)
})
var req = {}
args.forEach(function (a) {
a = a.split("@")
var n = a.shift()
, v = a.join("@")
;(req[n] = req[n] || []).push(v)
})
log.silly(req, "rebuild request")
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) {
var o = {}
o[p] = {}
if (inst[p][v].active) o[p].active = v
if (semver.satisfies(v, r)) o[p][v] = inst[p][v]
matches.push(o)
})
})
log.verbose(p, "rebuild")
cb(null, matches)
}, cb)
})
}
|