-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfind-duplicate-dependencies.js
50 lines (39 loc) · 1.4 KB
/
find-duplicate-dependencies.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
import npm from 'npm';
export default function findDuplicateDependencies(options = {}) {
const exclude = new Set(options.exclude ?? [])
return new Promise((resolve, reject) => {
npm.load({ production: !options.checkDevDependencies, json: true }, (err) => {
if (err) return reject(err);
npm.commands.ls([], true, (err, packageInfo, packageObj) => {
if (err) return reject(err);
const catalog = catalogDependencies(packageObj.dependencies, packageObj.name);
const duplicatePairs = Object.entries(catalog).filter(([name, occurrences]) => occurrences.length > 1 && !exclude.has(name));
resolve(Object.fromEntries(duplicatePairs));
});
});
});
}
function catalogDependencies(dependencies, path) {
return _catalogDependencies({}, dependencies, path);
}
function _catalogDependencies(result, dependencies, path) {
return Object.entries(dependencies || {}).reduce((acc, entry) => {
const [name, moduleObj] = entry;
if (!acc[name]) {
acc[name] = [];
}
const isAdded = acc[name].some((pack) => pack.version === moduleObj.version);
if (!isAdded) {
acc[name].push({
name,
version: moduleObj.version,
from: moduleObj.from,
path
});
}
if (moduleObj.dependencies) {
return _catalogDependencies(acc, moduleObj.dependencies, `${path}/${name}`);
}
return acc;
}, result);
}