UNPKG

10.6 kBJavaScriptView Raw
1// traverse the node_modules/package.json tree
2// looking for duplicates. If any duplicates are found,
3// then move them up to the highest level necessary
4// in order to make them no longer duplicated.
5//
6// This is kind of ugly, and really highlights the need for
7// much better "put pkg X at folder Y" abstraction. Oh well,
8// whatever. Perfect enemy of the good, and all that.
9
10var fs = require("fs")
11var asyncMap = require("slide").asyncMap
12var path = require("path")
13var readJson = require("read-package-json")
14var archy = require("archy")
15var util = require("util")
16var RegClient = require("npm-registry-client")
17var npmconf = require("npmconf")
18var semver = require("semver")
19var rm = require("./utils/gently-rm.js")
20var log = require("npmlog")
21var npm = require("./npm.js")
22
23module.exports = dedupe
24
25dedupe.usage = "npm dedupe [pkg pkg...]"
26
27function dedupe (args, silent, cb) {
28 if (typeof silent === "function") cb = silent, silent = false
29 var dryrun = false
30 if (npm.command.match(/^find/)) dryrun = true
31 return dedupe_(npm.prefix, args, {}, dryrun, silent, cb)
32}
33
34function dedupe_ (dir, filter, unavoidable, dryrun, silent, cb) {
35 readInstalled(path.resolve(dir), {}, null, function (er, data, counter) {
36 if (er) {
37 return cb(er)
38 }
39
40 if (!data) {
41 return cb()
42 }
43
44 // find out which things are dupes
45 var dupes = Object.keys(counter || {}).filter(function (k) {
46 if (filter.length && -1 === filter.indexOf(k)) return false
47 return counter[k] > 1 && !unavoidable[k]
48 }).reduce(function (s, k) {
49 s[k] = []
50 return s
51 }, {})
52
53 // any that are unavoidable need to remain as they are. don't even
54 // try to touch them or figure it out. Maybe some day, we can do
55 // something a bit more clever here, but for now, just skip over it,
56 // and all its children.
57 ;(function U (obj) {
58 if (unavoidable[obj.name]) {
59 obj.unavoidable = true
60 }
61 if (obj.parent && obj.parent.unavoidable) {
62 obj.unavoidable = true
63 }
64 Object.keys(obj.children).forEach(function (k) {
65 U(obj.children[k])
66 })
67 })
68
69 // then collect them up and figure out who needs them
70 ;(function C (obj) {
71 if (dupes[obj.name] && !obj.unavoidable) {
72 dupes[obj.name].push(obj)
73 obj.duplicate = true
74 }
75 obj.dependents = whoDepends(obj)
76 Object.keys(obj.children).forEach(function (k) {
77 C(obj.children[k])
78 })
79 })(data)
80
81 if (dryrun) {
82 var k = Object.keys(dupes)
83 if (!k.length) return cb()
84 return npm.commands.ls(k, silent, cb)
85 }
86
87 var summary = Object.keys(dupes).map(function (n) {
88 return [n, dupes[n].filter(function (d) {
89 return d && d.parent && !d.parent.duplicate && !d.unavoidable
90 }).map(function M (d) {
91 return [d.path, d.version, d.dependents.map(function (k) {
92 return [k.path, k.version, k.dependencies[d.name] || ""]
93 })]
94 })]
95 }).map(function (item) {
96 var name = item[0]
97 var set = item[1]
98
99 var ranges = set.map(function (i) {
100 return i[2].map(function (d) {
101 return d[2]
102 })
103 }).reduce(function (l, r) {
104 return l.concat(r)
105 }, []).map(function (v, i, set) {
106 if (set.indexOf(v) !== i) return false
107 return v
108 }).filter(function (v) {
109 return v !== false
110 })
111
112 var locs = set.map(function (i) {
113 return i[0]
114 })
115
116 var versions = set.map(function (i) {
117 return i[1]
118 }).filter(function (v, i, set) {
119 return set.indexOf(v) === i
120 })
121
122 var has = set.map(function (i) {
123 return [i[0], i[1]]
124 }).reduce(function (set, kv) {
125 set[kv[0]] = kv[1]
126 return set
127 }, {})
128
129 var loc = locs.length ? locs.reduce(function (a, b) {
130 // a=/path/to/node_modules/foo/node_modules/bar
131 // b=/path/to/node_modules/elk/node_modules/bar
132 // ==/path/to/node_modules/bar
133 var nmReg = new RegExp("\\" + path.sep + "node_modules\\" + path.sep)
134 a = a.split(nmReg)
135 b = b.split(nmReg)
136 var name = a.pop()
137 b.pop()
138 // find the longest chain that both A and B share.
139 // then push the name back on it, and join by /node_modules/
140 var res = []
141 for (var i = 0, al = a.length, bl = b.length; i < al && i < bl && a[i] === b[i]; i++);
142 return a.slice(0, i).concat(name).join(path.sep + "node_modules" + path.sep)
143 }) : undefined
144
145 return [item[0], { item: item
146 , ranges: ranges
147 , locs: locs
148 , loc: loc
149 , has: has
150 , versions: versions
151 }]
152 }).filter(function (i) {
153 return i[1].loc
154 })
155
156 findVersions(npm, summary, function (er, set) {
157 if (er) return cb(er)
158 if (!set.length) return cb()
159 installAndRetest(set, filter, dir, unavoidable, silent, cb)
160 })
161 })
162}
163
164function installAndRetest (set, filter, dir, unavoidable, silent, cb) {
165 //return cb(null, set)
166 var remove = []
167
168 asyncMap(set, function (item, cb) {
169 // [name, has, loc, locMatch, regMatch, others]
170 var name = item[0]
171 var has = item[1]
172 var where = item[2]
173 var locMatch = item[3]
174 var regMatch = item[4]
175 var others = item[5]
176
177 // nothing to be done here. oh well. just a conflict.
178 if (!locMatch && !regMatch) {
179 log.warn("unavoidable conflict", item[0], item[1])
180 log.warn("unavoidable conflict", "Not de-duplicating")
181 unavoidable[item[0]] = true
182 return cb()
183 }
184
185 // nothing to do except to clean up the extraneous deps
186 if (locMatch && has[where] === locMatch) {
187 remove.push.apply(remove, others)
188 return cb()
189 }
190
191 if (regMatch) {
192 var what = name + "@" + regMatch
193 // where is /path/to/node_modules/foo/node_modules/bar
194 // for package "bar", but we need it to be just
195 // /path/to/node_modules/foo
196 var nmReg = new RegExp("\\" + path.sep + "node_modules\\" + path.sep)
197 where = where.split(nmReg)
198 where.pop()
199 where = where.join(path.sep + "node_modules" + path.sep)
200 remove.push.apply(remove, others)
201
202 return npm.commands.install(where, what, cb)
203 }
204
205 // hrm?
206 return cb(new Error("danger zone\n" + name + " " +
207 + regMatch + " " + locMatch))
208
209 }, function (er, installed) {
210 if (er) return cb(er)
211 asyncMap(remove, rm, function (er) {
212 if (er) return cb(er)
213 remove.forEach(function (r) {
214 log.info("rm", r)
215 })
216 dedupe_(dir, filter, unavoidable, false, silent, cb)
217 })
218 })
219}
220
221function findVersions (npm, summary, cb) {
222 // now, for each item in the summary, try to find the maximum version
223 // that will satisfy all the ranges. next step is to install it at
224 // the specified location.
225 asyncMap(summary, function (item, cb) {
226 var name = item[0]
227 var data = item[1]
228 var loc = data.loc
229 var locs = data.locs.filter(function (l) {
230 return l !== loc
231 })
232
233 // not actually a dupe, or perhaps all the other copies were
234 // children of a dupe, so this'll maybe be picked up later.
235 if (locs.length === 0) {
236 return cb(null, [])
237 }
238
239 // { <folder>: <version> }
240 var has = data.has
241
242 // the versions that we already have.
243 // if one of these is ok, then prefer to use that.
244 // otherwise, try fetching from the registry.
245 var versions = data.versions
246
247 var ranges = data.ranges
248 npm.registry.get(name, function (er, data) {
249 var regVersions = er ? [] : Object.keys(data.versions)
250 var locMatch = bestMatch(versions, ranges)
251 var regMatch;
252 var tag = npm.config.get("tag")
253 var distTag = data["dist-tags"] && data["dist-tags"][tag]
254 if (distTag && data.versions[distTag] && matches(distTag, ranges)) {
255 regMatch = distTag
256 } else {
257 regMatch = bestMatch(regVersions, ranges)
258 }
259
260 cb(null, [[name, has, loc, locMatch, regMatch, locs]])
261 })
262 }, cb)
263}
264
265function matches (version, ranges) {
266 return !ranges.some(function (r) {
267 return !semver.satisfies(version, r, true)
268 })
269}
270
271function bestMatch (versions, ranges) {
272 return versions.filter(function (v) {
273 return matches(v, ranges)
274 }).sort(semver.compareLoose).pop()
275}
276
277
278function readInstalled (dir, counter, parent, cb) {
279 var pkg, children, realpath
280
281 fs.realpath(dir, function (er, rp) {
282 realpath = rp
283 next()
284 })
285
286 readJson(path.resolve(dir, "package.json"), function (er, data) {
287 if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
288 if (er) return cb() // not a package, probably.
289 counter[data.name] = counter[data.name] || 0
290 counter[data.name]++
291 pkg =
292 { _id: data._id
293 , name: data.name
294 , version: data.version
295 , dependencies: data.dependencies || {}
296 , optionalDependencies: data.optionalDependencies || {}
297 , devDependencies: data.devDependencies || {}
298 , bundledDependencies: data.bundledDependencies || []
299 , path: dir
300 , realPath: dir
301 , children: {}
302 , parent: parent
303 , family: Object.create(parent ? parent.family : null)
304 , unavoidable: false
305 , duplicate: false
306 }
307 if (parent) {
308 parent.children[data.name] = pkg
309 parent.family[data.name] = pkg
310 }
311 next()
312 })
313
314 fs.readdir(path.resolve(dir, "node_modules"), function (er, c) {
315 children = c || [] // error is ok, just means no children.
316 children = children.filter(function (p) {
317 return !p.match(/^[\._-]/)
318 })
319 next()
320 })
321
322 function next () {
323 if (!children || !pkg || !realpath) return
324
325 // ignore devDependencies. Just leave them where they are.
326 children = children.filter(function (c) {
327 return !pkg.devDependencies.hasOwnProperty(c)
328 })
329
330 pkg.realPath = realpath
331 if (pkg.realPath !== pkg.path) children = []
332 var d = path.resolve(dir, "node_modules")
333 asyncMap(children, function (child, cb) {
334 readInstalled(path.resolve(d, child), counter, pkg, cb)
335 }, function (er) {
336 cb(er, pkg, counter)
337 })
338 }
339}
340
341function whoDepends (pkg) {
342 var start = pkg.parent || pkg
343 return whoDepends_(pkg, [], start)
344}
345
346function whoDepends_ (pkg, who, test) {
347 if (test !== pkg &&
348 test.dependencies[pkg.name] &&
349 test.family[pkg.name] === pkg) {
350 who.push(test)
351 }
352 Object.keys(test.children).forEach(function (n) {
353 whoDepends_(pkg, who, test.children[n])
354 })
355 return who
356}
357