|
| 1 | +"use strict" |
| 2 | + |
| 3 | +/** |
| 4 | + * When using the PNPM package manager, you can use pnpmfile.js to workaround |
| 5 | + * dependencies that have mistakes in their package.json file. (This feature is |
| 6 | + * functionally similar to Yarn's "resolutions".) |
| 7 | + * |
| 8 | + * For details, see the PNPM documentation: |
| 9 | + * https://pnpm.js.org/docs/en/hooks.html |
| 10 | + * |
| 11 | + * IMPORTANT: SINCE THIS FILE CONTAINS EXECUTABLE CODE, MODIFYING IT IS LIKELY TO INVALIDATE |
| 12 | + * ANY CACHED DEPENDENCY ANALYSIS. After any modification to pnpmfile.js, it's recommended to run |
| 13 | + * "rush update --full" so that PNPM will recalculate all version selections. |
| 14 | + * Or `pnpm install --fix-lockfile` for non Rush projects |
| 15 | + */ |
| 16 | +module.exports = { |
| 17 | + hooks: { |
| 18 | + readPackage, |
| 19 | + }, |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * This hook is invoked during installation before a package's dependencies |
| 24 | + * are selected. |
| 25 | + * The `packageJson` parameter is the deserialized package.json |
| 26 | + * contents for the package that is about to be installed. |
| 27 | + * The `context` parameter provides a log() function. |
| 28 | + * The return value is the updated object. |
| 29 | + */ |
| 30 | + |
| 31 | +const TYPES = { |
| 32 | + PEER: "peerDependencies", |
| 33 | + DEPS: "dependencies", |
| 34 | +} |
| 35 | + |
| 36 | +const prettyType = (type) => (type === TYPES.DEPS ? "dependency" : "peerDependency") |
| 37 | + |
| 38 | +function readPackage(packageJson, context) { |
| 39 | + function removeGlobal(type, name, noLog) { |
| 40 | + if (packageJson[type] && packageJson[type][name]) { |
| 41 | + !noLog && |
| 42 | + context.log(`Removed "${name}" ${prettyType(type)} for ${packageJson.name}`) |
| 43 | + delete packageJson[type][name] |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + function changeGlobal(type, name, ver, noLog) { |
| 48 | + if (packageJson[type] && packageJson[type][name]) { |
| 49 | + const originalVersion = packageJson[type][name] |
| 50 | + if (originalVersion !== ver) { |
| 51 | + !noLog && |
| 52 | + context.log( |
| 53 | + `Changed "${name}" ${prettyType( |
| 54 | + type, |
| 55 | + )} from ${originalVersion} to ${ver} for ${packageJson.name}`, |
| 56 | + ) |
| 57 | + packageJson[type][name] = ver |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + function add(type, forPackage, dep, ver, noLog) { |
| 63 | + if (packageJson.name === forPackage) { |
| 64 | + if (!packageJson[type]) { |
| 65 | + packageJson[type] = {} |
| 66 | + } |
| 67 | + !noLog && context.log(`Added "${dep}" ${prettyType(type)} for ${packageJson.name}`) |
| 68 | + packageJson[type][dep] = ver |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + function remove(type, forPackage, dep, noLog) { |
| 73 | + if (packageJson.name === forPackage && !packageJson?.[type]?.[dep]) { |
| 74 | + context.log( |
| 75 | + `No ${type} "${dep}" in the package ${forPackage} to remove it. You sure about it?`, |
| 76 | + ) |
| 77 | + } else if (packageJson.name === forPackage) { |
| 78 | + !noLog && context.log(`Removed "${dep}" dependency for "${packageJson.name}"`) |
| 79 | + delete packageJson[type][dep] |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + function change(type, forPackage, dep, ver, noLog) { |
| 84 | + if (packageJson.name === forPackage && packageJson[type]) { |
| 85 | + if (!packageJson[type][dep]) { |
| 86 | + context.log( |
| 87 | + `No such ${type} in the package ${forPackage} to change it. You sure about it?`, |
| 88 | + ) |
| 89 | + } else if (packageJson.name === forPackage) { |
| 90 | + const originalVersion = packageJson[type][dep] |
| 91 | + if (originalVersion !== ver) { |
| 92 | + !noLog && |
| 93 | + context.log( |
| 94 | + `Changed "${dep}" ${prettyType( |
| 95 | + type, |
| 96 | + )} from ${originalVersion} to ${ver} for ${packageJson.name}`, |
| 97 | + ) |
| 98 | + packageJson[type][dep] = ver |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + change(TYPES.PEER, "vitest-dom", "vitest", "<1", true) |
| 105 | + |
| 106 | + return packageJson |
| 107 | +} |
0 commit comments