|
| 1 | +/* |
| 2 | + * Copyright 2024 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +// This is a Yarn plugin to detect multiple installed versions of React Spectrum/React Aria packages. |
| 14 | +// It can be installed by running `yarn plugin import https://raw.githubusercontent.com/adobe/react-spectrum/main/lib/yarn-plugin-rsp-duplicates.js`. |
| 15 | +module.exports = { |
| 16 | + name: 'plugin-rsp-duplicates', |
| 17 | + factory: require => { |
| 18 | + const {structUtils, MessageName, ReportError} = require('@yarnpkg/core'); |
| 19 | + |
| 20 | + return { |
| 21 | + default: { |
| 22 | + hooks: { |
| 23 | + afterAllInstalled(project) { |
| 24 | + let packages = new Map(); |
| 25 | + let hasRSP = false; |
| 26 | + for (const pkg of project.storedPackages.values()) { |
| 27 | + if (!structUtils.isVirtualLocator(pkg) && (pkg.scope === 'react-aria' || pkg.scope === 'react-spectrum' || pkg.scope === 'react-stately')) { |
| 28 | + let key = `@${pkg.scope}/${pkg.name}`; |
| 29 | + if (!packages.has(key)) { |
| 30 | + packages.set(key, new Set([pkg.version])); |
| 31 | + } else { |
| 32 | + packages.get(key).add(pkg.version); |
| 33 | + } |
| 34 | + hasRSP ||= pkg.scope === 'react-spectrum'; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + let errors = []; |
| 39 | + for (let [pkg, versions] of packages) { |
| 40 | + if (versions.size > 1) { |
| 41 | + errors.push(` ${pkg}: ${[...versions].join(', ')}`); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (errors.length) { |
| 46 | + errors.sort(); |
| 47 | + throw new ReportError( |
| 48 | + MessageName.EXCEPTION, |
| 49 | + `Found multiple installed versions of ${hasRSP ? 'React Spectrum' : 'React Aria'} packages: \n\n` |
| 50 | + + errors.join('\n') |
| 51 | + + '\n\n You may be able to fix this by running `yarn dedupe`.' |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + }; |
| 58 | + } |
| 59 | +}; |
0 commit comments