forked from uber-archive/npm-shrinkwrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trim-nested.js
63 lines (47 loc) · 1.47 KB
/
trim-nested.js
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
var jsonDiff = require('json-diff');
module.exports = trimNested;
/* var patches = diff(current, previous)
for each NESTED (depth >=1) patch, apply it to current.
Write new current into disk at dirname/npm-shrinkwrap.json
*/
function trimNested(previous, current, opts) {
// bail early if we want to keep nested dependencies
if (opts.keepNested) {
return current;
}
// purposes find patches from to
// apply TO current FROM previous
var patches = jsonDiff.diff(current, previous);
if (!patches) {
return current;
}
patches = removeTopLevelPatches(patches);
if (patches.dependencies) {
Object.keys(patches.dependencies)
.forEach(function (key) {
current.dependencies[key] =
previous.dependencies[key];
});
}
return current;
}
function removeTopLevelPatches(patches) {
if (!patches.dependencies) {
return patches;
}
patches.dependencies = Object.keys(patches.dependencies)
.reduce(function (acc, key) {
var patch = patches.dependencies[key];
if (typeof patch !== 'object' || patch === null) {
return acc;
}
var patchKeys = Object.keys(patch);
if (patchKeys.length === 1 &&
patchKeys[0] === 'dependencies'
) {
acc[key] = patch;
}
return acc;
}, {});
return patches;
}