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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/*
This command is plumbing.
npm update-deps <pkg>
For each other version of pkg, for each dependent in other
version's dependents folder, if the new version would satisfy the
dependency as well, update other version's dependent's dependency
links to point at the new version
If no dependents are left, then remove old version
*/
module.exports = updateDependents
var readInstalled = require("./utils/read-installed")
, path = require("path")
, npm = require("../npm")
, chain = require("./utils/chain")
, semver = require("./utils/semver")
, link = require("./utils/link")
, linkIfExists = link.ifExists
, shim = require("./utils/write-shim")
, shimIfExists = shim.ifExists
, readJson = require("./utils/read-json")
, log = require("./utils/log")
, fs = require("fs")
, rm = require("./utils/rm-rf")
function updateDependents (args, cb) {
// replace args with the installed data
if (!args.length) return cb() // nothing to do
readArgs(args, function (er, args) {
if (er) return log.er(cb, "Error reading args")(er)
cb = (function (cb) { return function (er) {
var a = args.map(function (a) { return a._id })
log( a, (er) ? "failed to update dependents" : "updated dependents")
return cb(er)
}})(cb)
// now this is an array of package descriptors
// and each one is installed, and has siblings.
// update any dependents on any other versions to this one,
// if it satisfies them, and then remove them if they have
// no more dependents
if (!args.length) return log(
"Nothing to update", "update dependents", cb)
chain(args.map(function (arg) {
return [updateDepsTo, arg]
}).concat(function (er) {
// now they've all been updated, so remove the others.
var rmList = []
args.forEach(function (arg) {
// if (!arg._others) log(arg); return
arg._others.forEach(function (o) {
rmList.push(arg.name+"@"+o)
})
})
npm.commands.rm(rmList, cb)
}))
})
}
// update the _others to this one.
function updateDepsTo (arg, cb) {
chain(arg._others.map(function (o) {
return [updateOtherVersionDeps, o, arg]
}).concat(cb))
}
function updateOtherVersionDeps (other, pkg, cb) {
var depdir = path.join( npm.dir
, pkg.name
, other
, "dependents"
)
fs.readdir(depdir, function (er, deps) {
if (er) return log.er(cb,
"failed to read dependents on "+pkg.name+"@"+other)(er)
// for each of these, update the dependency on
// other to pkg
if (!deps.length) return cb()
chain(deps.map(function (d) {
// todo: make this a @ instead of a -
d = d.split("-")
var name = d.shift()
, ver = d.join("-")
return [updateDepToNew, name, ver, pkg, other]
}).concat(cb))
})
}
function updateDepToNew (depName, depVer, pkg, other, cb) {
var depdir = path.join(npm.dir, depName, depVer)
, jsonFile = path.join(depdir, "package", "package.json")
readJson(jsonFile, function (er, data) {
if (er) return log.er(cb, "failed to read "+jsonFile)(er)
// check if pkg is ok
var dependencies = data.dependencies
if (!dependencies) return log
( "Weird, "+depName+"@"+depVer+" doesn't have any dependencies"
, "wtf?"
, cb
)
if (Array.isArray(dependencies)) {
var deps = {}
dependencies.forEach(function (d) { deps[d] = "*" })
dependencies = deps
}
var dependency = data.dependencies[pkg.name]
if (!dependency) return log
( "Weird, "+depName+"@"+depVer+" doesn't depend on "+pkg.name
, "wtf?"
, cb
)
if (!semver.satisfies(pkg.version, dependency)) return log
( pkg._id + " doesn't satisfy "+depName+"@"+depVer
, "not updating"
, cb
)
chain
( [ removeDependencyLinks, data, pkg, other ]
, [ createDependencyLinks, data, pkg ]
, cb
)
})
}
function removeDependencyLinks (dep, pkg, other, cb) {
var depdir = path.join(npm.dir, dep.name, dep.version)
, depsOn = path.join(depdir, "dependson", pkg.name+"-"+other)
, deps = path.join(depdir, "dependencies", pkg.name)
, dependentLink = path.join( npm.dir
, pkg.name
, other
, "dependents"
, dep.name + "-" + dep.version
)
chain
( [ rm, deps+".js" ]
, [ rm, deps ]
, [ rm, depsOn ]
, [ rm, dependentLink ]
, cb
)
}
function createDependencyLinks (dep, pkg, cb) {
var depdir = path.join(npm.dir, dep.name, dep.version)
, depsOn = path.join( depdir
, "dependson"
, pkg.name+"-"+pkg.version
)
, deps = path.join(depdir, "dependencies", pkg.name)
, targetRoot = path.join(npm.dir, pkg.name, pkg.version)
, targetMain = path.join(targetRoot, "main.js")
, targetLib = path.join(targetRoot, "lib")
, dependentLink = path.join( npm.dir
, pkg.name
, pkg.version
, "dependents"
, dep.name + "-" + dep.version
)
chain
( [ link, targetRoot, depsOn ]
, [ link, depdir, dependentLink ]
, [ linkIfExists, targetLib, deps ]
, [ shimIfExists, targetMain, deps + ".js" ]
, cb
)
}
function readArgs (args, cb) {
var p = args.length
function r () { if (--p === 0) {
cb(null, args.filter(function (a) { return a }))
}}
function readOthers (arg, i) {
readInstalled([arg.name], function (er, inst) {
log(inst, "installed "+arg.name)
if (er) {
args[i] = null
return log(er, "Error reading installed", r)
}
var have = Object.keys(inst[arg.name])
if (have.length < 2) {
args[i] = null
return log(
"Only one version installed", "update-dependents "+arg.name, r)
}
arg._others = have.filter(function (v) { return v !== arg.version })
r()
})
}
args.forEach(function (arg, i) {
if (typeof arg === "object") return readOthers(arg, i)
arg = arg.split(/@/)
var name = arg.shift()
, ver = arg.join("@")
, jsonFile = path.join( npm.dir
, name
, ver
, "package"
, "package.json"
)
readJson(jsonFile, function (er, arg) {
if (er) {
args[i] = null
return log(er, "Error reading "+jsonFile, r)
}
args[i] = arg
readOthers(arg, i)
})
})
}
|