-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (56 loc) · 1.84 KB
/
index.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
64
65
66
const execa = require('execa');
const _ = require('lodash');
function mapDependencies(dupeList, path, dependencies) {
_.forEach(dependencies, (meta, name) => {
if (!meta._deduped) {
// Don't add it if npm has deduped the package. Relies on npm internals so not exactly safe
_.update(dupeList, [name, meta.version], (paths) => {
if (paths) {
paths.push(path ? path : 'this package');
return paths;
}
return [path ? path : 'this package'];
});
}
if (meta.dependencies && typeof meta.dependencies === 'object') {
const nextPath = path ? `${path} -> ${name}@${meta.version}` : `${name}@${meta.version}`;
mapDependencies(dupeList, nextPath, meta.dependencies);
}
});
}
function removeDupes(dupeList) {
_.forEach(dupeList, (versions, dep) => {
let dupesCount = 0;
_.forEach(versions, (version) => {
dupesCount += version.length;
});
if (dupesCount <= 1) {
delete dupeList[dep];
}
});
}
function formatDupes(dependencyGraph) {
const dupeList = {};
mapDependencies(dupeList, '', dependencyGraph.dependencies);
removeDupes(dupeList);
return dupeList;
}
module.exports = function prestige() {
// NPM no longer supports the programmers api and prefers to be called via exec
// ls --json provides us with output we can work with
// and we run --prod because we don't care about duplicate devDependencies
return execa('npm', ['ls', '--json', '--prod', '--long'], { maxBuffer: 20000000 }).then(({ stdout: npmStdOut }) => {
return formatDupes(JSON.parse(npmStdOut));
})
.catch(err => {
try {
console.log(err.message);
if (err.stderr) {
console.log(err.stderr);
}
return formatDupes(JSON.parse(err.stdout));
} catch(e) {
throw new Error("Couldn't get data from npm -ls", e);
}
});
}