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
|
var util = require('util')
var path = require('path')
var validate = require('aproba')
var without = require('lodash.without')
var asyncMap = require('slide').asyncMap
var chain = require('slide').chain
var npa = require('npm-package-arg')
var log = require('npmlog')
var npm = require('./npm.js')
var Installer = require('./install.js').Installer
var findRequirement = require('./install/deps.js').findRequirement
var earliestInstallable = require('./install/deps.js').earliestInstallable
var checkPermissions = require('./install/check-permissions.js')
var decomposeActions = require('./install/decompose-actions.js')
var loadExtraneous = require('./install/deps.js').loadExtraneous
var filterInvalidActions = require('./install/filter-invalid-actions.js')
var recalculateMetadata = require('./install/deps.js').recalculateMetadata
var sortActions = require('./install/diff-trees.js').sortActions
module.exports = dedupe
module.exports.Deduper = Deduper
dedupe.usage = 'npm dedupe [package names...]'
function dedupe (args, cb) {
validate('AF', arguments)
// the /path/to/node_modules/..
var where = path.resolve(npm.dir, '..')
var dryrun = false
if (npm.command.match(/^find/)) dryrun = true
if (npm.config.get('dry-run')) dryrun = true
new Deduper(where, dryrun).run(cb)
}
function Deduper (where, dryrun) {
validate('SB', arguments)
Installer.call(this, where, dryrun, [])
this.noPackageJsonOk = true
this.topLevelLifecycles = false
}
util.inherits(Deduper, Installer)
Deduper.prototype.normalizeTree = function (log, cb) {
validate('OF', arguments)
log.silly('dedupe', 'normalizeTree')
// If we're looking globally only look at the one package we're operating on
if (npm.config.get('global')) {
var args = this.args
this.currentTree.children = this.currentTree.children.filter(function (child) {
return args.filter(function (arg) { return arg === child.package.name }).length
})
}
Installer.prototype.normalizeTree.call(this, log, cb)
}
Deduper.prototype.loadIdealTree = function (cb) {
validate('F', arguments)
log.silly('install', 'loadIdealTree')
var self = this
chain([
[this.newTracker(this.progress.loadIdealTree, 'cloneCurrentTree')],
[this, this.cloneCurrentTreeToIdealTree],
[this, this.finishTracker, 'cloneCurrentTree'],
[this.newTracker(this.progress.loadIdealTree, 'loadAllDepsIntoIdealTree', 10)],
[function (next) {
loadExtraneous(self.idealTree, self.progress.loadAllDepsIntoIdealTree, next)
}],
[this, this.finishTracker, 'loadAllDepsIntoIdealTree'],
[this, function (next) { recalculateMetadata(this.idealTree, log, next) } ],
], cb)
}
Deduper.prototype.generateActionsToTake = function (cb) {
validate('F', arguments)
log.silly('dedupe', 'generateActionsToTake')
chain([
[this.newTracker(log, 'hoist', 1)],
[hoistChildren, this.idealTree, this.differences],
[this, this.finishTracker, 'hoist'],
[this.newTracker(log, 'sort-actions', 1)],
[this, function (next) {
this.differences = sortActions(this.differences)
next()
}],
[this, this.finishTracker, 'sort-actions'],
[filterInvalidActions, this.where, this.differences],
[checkPermissions, this.differences],
[decomposeActions, this.differences, this.todo]
], cb)
}
function move (node, hoistTo, diff) {
node.parent.children = without(node.parent.children, node)
hoistTo.children.push(node)
node.fromPath = node.path
node.path = path.resolve(hoistTo.path, 'node_modules', node.package.name)
node.parent = hoistTo
if (!diff.filter(function (action) { return action[0] === 'move' && action[1] === node }).length) {
diff.push(['move', node])
}
}
function moveRemainingChildren (node, diff) {
node.children.forEach(function (child) {
move(child, node, diff)
moveRemainingChildren(child, diff)
})
}
function remove (child, diff, done) {
diff.push(['remove', child])
child.parent.children = without(child.parent.children, child)
asyncMap(child.children, function (child, next) {
remove(child, diff, next)
}, done)
}
function hoistChildren (tree, diff, next) {
validate('OAF', arguments)
asyncMap(tree.children, function (child, done) {
if (!tree.parent) return hoistChildren(child, diff, done)
var better = findRequirement(tree.parent, child.package.name, child.package._requested || npa(child.package.name + '@' + child.package.version))
if (better) {
return chain([
[remove, child, diff],
[recalculateMetadata, tree, log]
], done)
}
var hoistTo = earliestInstallable(tree, tree.parent, child.package)
if (hoistTo) {
move(child, hoistTo, diff)
chain([
[recalculateMetadata, hoistTo, log],
[hoistChildren, child, diff],
[function (next) {
moveRemainingChildren(child, diff)
next()
}]
], done)
} else {
done()
}
}, next)
}
|