|
| 1 | +/* eslint-disable no-console */ |
| 2 | +/** |
| 3 | + * Given the dist tag fetch the corresponding |
| 4 | + * version and make sure this version is used throughout the repository. |
| 5 | + * |
| 6 | + * If you work on this file: |
| 7 | + * WARNING: This script can only use built-in modules since it has to run before |
| 8 | + * `pnpm install` |
| 9 | + */ |
| 10 | +import childProcess from 'child_process'; |
| 11 | +import fs from 'fs'; |
| 12 | +import os from 'os'; |
| 13 | +import path from 'path'; |
| 14 | +import { promisify } from 'util'; |
| 15 | +import { getWorkspaceRoot } from './utils.mjs'; |
| 16 | + |
| 17 | +// TODO: reuse the `useReactVersion.mjs` from the monorepo |
| 18 | + |
| 19 | +const exec = promisify(childProcess.exec); |
| 20 | + |
| 21 | +// packages published from the react monorepo using the same version |
| 22 | +const reactPackageNames = ['react', 'react-dom', 'react-is', 'react-test-renderer', 'scheduler']; |
| 23 | +const devDependenciesPackageNames = ['@mnajdova/enzyme-adapter-react-18', '@testing-library/react']; |
| 24 | + |
| 25 | +// if we need to support more versions we will need to add new mapping here |
| 26 | +const additionalVersionsMappings = { |
| 27 | + 17: { |
| 28 | + '@mnajdova/enzyme-adapter-react-18': 'npm:@eps1lon/enzyme-adapter-react-17', |
| 29 | + '@testing-library/react': '^12.1.0', |
| 30 | + }, |
| 31 | + 19: {}, |
| 32 | +}; |
| 33 | + |
| 34 | +async function main(version) { |
| 35 | + if (typeof version !== 'string') { |
| 36 | + throw new TypeError(`expected version: string but got '${version}'`); |
| 37 | + } |
| 38 | + |
| 39 | + if (version === 'stable') { |
| 40 | + console.log('Nothing to do with stable'); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + const packageJsonPath = path.resolve(getWorkspaceRoot(), 'package.json'); |
| 45 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' })); |
| 46 | + |
| 47 | + // the version is something in format: "17.0.0" |
| 48 | + let majorVersion = null; |
| 49 | + |
| 50 | + if (version.startsWith('^') || version.startsWith('~') || !Number.isNaN(version.charAt(0))) { |
| 51 | + majorVersion = version.replace('^', '').replace('~', '').split('.')[0]; |
| 52 | + } |
| 53 | + |
| 54 | + await Promise.all( |
| 55 | + reactPackageNames.map(async (reactPackageName) => { |
| 56 | + const { stdout: versions } = await exec(`npm dist-tag ls ${reactPackageName} ${version}`); |
| 57 | + const tagMapping = versions.split('\n').find((mapping) => { |
| 58 | + return mapping.startsWith(`${version}: `); |
| 59 | + }); |
| 60 | + |
| 61 | + let packageVersion = null; |
| 62 | + |
| 63 | + if (tagMapping === undefined) { |
| 64 | + // Some specific version is being requested |
| 65 | + if (majorVersion) { |
| 66 | + packageVersion = version; |
| 67 | + if (reactPackageName === 'scheduler') { |
| 68 | + // get the scheduler version from the react-dom's dependencies entry |
| 69 | + const { stdout: reactDOMDependenciesString } = await exec( |
| 70 | + `npm view --json react-dom@${version} dependencies`, |
| 71 | + ); |
| 72 | + packageVersion = JSON.parse(reactDOMDependenciesString).scheduler; |
| 73 | + } |
| 74 | + } else { |
| 75 | + throw new Error(`Could not find '${version}' in "${versions}"`); |
| 76 | + } |
| 77 | + } else { |
| 78 | + packageVersion = tagMapping.replace(`${version}: `, ''); |
| 79 | + } |
| 80 | + |
| 81 | + packageJson.resolutions[reactPackageName] = packageVersion; |
| 82 | + }), |
| 83 | + ); |
| 84 | + |
| 85 | + // At this moment all dist tags reference React 18 version, so we don't need |
| 86 | + // to update these dependencies unless an older version is used, or when the |
| 87 | + // next/experimental dist tag reference to a future version of React |
| 88 | + // packageJson.devDependencies['@mnajdova/enzyme-adapter-react-18'] = |
| 89 | + // 'npm:@mnajdova/enzyme-adapter-react-next'; |
| 90 | + // packageJson.devDependencies['@testing-library/react'] = 'alpha'; |
| 91 | + |
| 92 | + if (majorVersion && additionalVersionsMappings[majorVersion]) { |
| 93 | + devDependenciesPackageNames.forEach((packageName) => { |
| 94 | + if (!additionalVersionsMappings[majorVersion][packageName]) { |
| 95 | + throw new Error( |
| 96 | + `Version ${majorVersion} does not have version defined for the ${packageName}`, |
| 97 | + ); |
| 98 | + } |
| 99 | + packageJson.devDependencies[packageName] = |
| 100 | + additionalVersionsMappings[majorVersion][packageName]; |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + // add newline for clean diff |
| 105 | + fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}${os.EOL}`); |
| 106 | +} |
| 107 | + |
| 108 | +const [version = process.env.REACT_VERSION] = process.argv.slice(2); |
| 109 | +main(version).catch((error) => { |
| 110 | + console.error(error); |
| 111 | + process.exit(1); |
| 112 | +}); |
0 commit comments