-
Notifications
You must be signed in to change notification settings - Fork 103
/
.pnpmfile.cjs
107 lines (96 loc) · 3.35 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"use strict"
/**
* When using the PNPM package manager, you can use pnpmfile.js to workaround
* dependencies that have mistakes in their package.json file. (This feature is
* functionally similar to Yarn's "resolutions".)
*
* For details, see the PNPM documentation:
* https://pnpm.js.org/docs/en/hooks.html
*
* IMPORTANT: SINCE THIS FILE CONTAINS EXECUTABLE CODE, MODIFYING IT IS LIKELY TO INVALIDATE
* ANY CACHED DEPENDENCY ANALYSIS. After any modification to pnpmfile.js, it's recommended to run
* "rush update --full" so that PNPM will recalculate all version selections.
* Or `pnpm install --fix-lockfile` for non Rush projects
*/
module.exports = {
hooks: {
readPackage,
},
}
/**
* This hook is invoked during installation before a package's dependencies
* are selected.
* The `packageJson` parameter is the deserialized package.json
* contents for the package that is about to be installed.
* The `context` parameter provides a log() function.
* The return value is the updated object.
*/
const TYPES = {
PEER: "peerDependencies",
DEPS: "dependencies",
}
const prettyType = (type) => (type === TYPES.DEPS ? "dependency" : "peerDependency")
function readPackage(packageJson, context) {
function removeGlobal(type, name, noLog) {
if (packageJson[type] && packageJson[type][name]) {
!noLog &&
context.log(`Removed "${name}" ${prettyType(type)} for ${packageJson.name}`)
delete packageJson[type][name]
}
}
function changeGlobal(type, name, ver, noLog) {
if (packageJson[type] && packageJson[type][name]) {
const originalVersion = packageJson[type][name]
if (originalVersion !== ver) {
!noLog &&
context.log(
`Changed "${name}" ${prettyType(
type,
)} from ${originalVersion} to ${ver} for ${packageJson.name}`,
)
packageJson[type][name] = ver
}
}
}
function add(type, forPackage, dep, ver, noLog) {
if (packageJson.name === forPackage) {
if (!packageJson[type]) {
packageJson[type] = {}
}
!noLog && context.log(`Added "${dep}" ${prettyType(type)} for ${packageJson.name}`)
packageJson[type][dep] = ver
}
}
function remove(type, forPackage, dep, noLog) {
if (packageJson.name === forPackage && !packageJson?.[type]?.[dep]) {
context.log(
`No ${type} "${dep}" in the package ${forPackage} to remove it. You sure about it?`,
)
} else if (packageJson.name === forPackage) {
!noLog && context.log(`Removed "${dep}" dependency for "${packageJson.name}"`)
delete packageJson[type][dep]
}
}
function change(type, forPackage, dep, ver, noLog) {
if (packageJson.name === forPackage && packageJson[type]) {
if (!packageJson[type][dep]) {
context.log(
`No such ${type} in the package ${forPackage} to change it. You sure about it?`,
)
} else if (packageJson.name === forPackage) {
const originalVersion = packageJson[type][dep]
if (originalVersion !== ver) {
!noLog &&
context.log(
`Changed "${dep}" ${prettyType(
type,
)} from ${originalVersion} to ${ver} for ${packageJson.name}`,
)
packageJson[type][dep] = ver
}
}
}
}
change(TYPES.PEER, "vitest-dom", "vitest", "<1", true)
return packageJson
}