-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·124 lines (114 loc) · 3.26 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
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
#!/usr/bin/env node
'use strict';
const {
existsSync: exists,
readdirSync: readdir,
statSync: stat,
readFile: readFileCb,
} = require('fs');
const path = require('path');
const { promisify } = require('util');
const readFile = promisify(readFileCb);
function getDependencies(dir) {
if (exists(dir)) {
const dependencies = readdir(dir)
.filter((curr) => !/^\./.test(curr))
.reduce((acc, curr) => {
const currDir = path.join(dir, curr);
if (/^@/.test(curr)) {
acc.push(getDependencies(currDir));
} else {
acc.push(currDir);
if (stat(currDir).isDirectory()) {
var nextDir = path.join(currDir, 'node_modules');
acc.push(getDependencies(nextDir));
}
}
return acc;
}, []);
return [].concat(...dependencies);
} else {
return [];
}
}
function getAllDuplicates(cwd, getFullPaths = false) {
const dependencies = getDependencies(cwd);
const duplicates = dependencies
.map((curr) =>
curr
.replace(/\\/g, '/')
.replace(new RegExp('^.*?/((?:@[^/]+/)?[^/]*)$'), '$1')
)
.filter((curr, index, self) => self.indexOf(curr) !== index)
.filter((curr, index, self) => self.indexOf(curr) === index)
.sort();
if (!getFullPaths) {
return duplicates;
}
return dependencies.filter((dep) =>
duplicates.some((dup) => dep.endsWith(dup))
);
}
function getDuplicates(cwd, ...patterns) {
const dir = path.join(cwd, 'node_modules');
const regExps = patterns.map(
(pattern) => new RegExp(`^${pattern.replace(/\*+/g, '.*')}$`)
);
const duplicates = getAllDuplicates(dir);
if (regExps.length) {
return duplicates.filter((curr) => {
for (let i = 0; i < regExps.length; i++) {
if (regExps[i].test(curr)) return true;
}
});
}
return duplicates;
}
async function getPackageVersion({ pathName, ...rest }) {
const pkg = await readFile(path.join(pathName, 'package.json'), 'utf8');
const { version } = JSON.parse(pkg);
return { pathName, version, ...rest };
}
async function getDuplicatesDetails(cwd, ...patterns) {
const dir = path.join(cwd, 'node_modules');
const regExps = patterns.map(
(pattern) => new RegExp(`node_modules\\/${pattern.replace(/\*+/g, '.*')}$`)
);
let duplicates = getAllDuplicates(dir, true);
if (regExps.length) {
duplicates = duplicates.filter((curr) => {
for (let i = 0; i < regExps.length; i++) {
if (regExps[i].test(curr)) return true;
}
});
}
const duplictesWithVersions = await Promise.all(
duplicates
.map((pathName) => ({
name: path.basename(pathName),
pathName,
}))
.map(getPackageVersion)
);
return duplictesWithVersions;
}
if (require.main === module) {
const cwd = process.env.INIT_CWD || process.cwd();
const argv = process.argv.slice(2);
try {
const duplicates = getDuplicates(cwd, ...argv);
duplicates.forEach((duplicate) =>
// eslint-disable-next-line no-console
console.warn('Duplicate: %s', duplicate)
);
if (duplicates.length) {
process.exit(1);
}
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
process.exit(1);
}
} else {
module.exports = { getDuplicates, getDuplicatesDetails };
}