-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
.pnpmfile.cjs
91 lines (85 loc) · 2.15 KB
/
.pnpmfile.cjs
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
const enforceSingleVersion = [
'react-hot-toast', //
'react',
'react-dom',
'next-auth',
'next',
// 'tailwindcss',
'@prisma/client',
'next-themes',
'@chakra-ui/react',
'@nextui-org/react',
'@headlessui/react',
'@heroicons/react',
]
function afterAllResolved(lockfile, context) {
console.log(`Checking duplicate packages`)
const packagesKeys = Object.keys(lockfile.packages)
const found = {}
for (let p of packagesKeys) {
for (let x of enforceSingleVersion) {
if (p.startsWith(`/${x}/`)) {
if (found[x]) {
found[x] += 1
} else {
found[x] = 1
}
}
}
}
let msg = ''
for (let p in found) {
const count = found[p]
if (count && count > 1) {
msg += `${p} found ${count} times\n`
msg += explainProblemInDuplicateDep(p, lockfile)
}
}
if (msg) {
throw new Error(msg)
}
return lockfile
}
function explainProblemInDuplicateDep(package, lockfile) {
const packagesKeys = Object.keys(lockfile.packages)
let found = {}
for (let p of packagesKeys) {
if (p.startsWith(`/${package}/`)) {
const config = lockfile.packages[p]
found[p] = Object.keys(config.dependencies || {})
}
}
const differences = getDifferences(Object.values(found))
if (differences.length) {
return `${package} has different set of dependencies:\n${JSON.stringify(
differences,
null,
2,
)}`
}
return ''
}
// return different items from a list of arrays of strings
function getDifferences(arrays) {
const result = {}
for (let a of arrays) {
for (let x of a) {
if (result[x]) {
result[x] += 1
} else {
result[x] = 1
}
}
}
for (let x in result) {
if (result[x] === arrays.length) {
delete result[x]
}
}
return Object.keys(result).filter(Boolean)
}
module.exports = {
hooks: {
afterAllResolved,
},
}