-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[code-infra] Reuse useReactVersion script from the monorepo
#13710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cherniavskii
merged 14 commits into
mui:master
from
cherniavskii:useReactVersion-monorepo
Jul 22, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f45b186
use monorepo from a fork
cherniavskii 9501043
import useReactVersion from monorepo
cherniavskii 4ee7b9b
resolve react version after installing deps (need monorepo script)
cherniavskii aaccb33
fix exit code 1
cherniavskii 27fd41c
Merge branch 'master' into useReactVersion-monorepo
cherniavskii f169bea
use monorepo from a fork
cherniavskii 421e84b
add use-react-version script
cherniavskii 8cdf63a
upgrade monorepo
cherniavskii 998ef66
Merge branch 'master' into useReactVersion-monorepo
cherniavskii d55f6c4
Merge branch 'master' into useReactVersion-monorepo
cherniavskii f634f7a
bump monorepo
cherniavskii cd7cc3a
bump monorepo
cherniavskii 905575e
use-react-version installs deps automatically
cherniavskii 550d6b6
Merge branch 'master' into useReactVersion-monorepo
cherniavskii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,109 +1 @@ | ||
| /* eslint-disable no-console */ | ||
| /** | ||
| * Given the dist tag fetch the corresponding | ||
| * version and make sure this version is used throughout the repository. | ||
| * | ||
| * If you work on this file: | ||
| * WARNING: This script can only use built-in modules since it has to run before | ||
| * `pnpm install` | ||
| */ | ||
| import childProcess from 'child_process'; | ||
| import fs from 'fs'; | ||
| import os from 'os'; | ||
| import path from 'path'; | ||
| import { promisify } from 'util'; | ||
| import { getWorkspaceRoot } from './utils.mjs'; | ||
|
|
||
| // TODO: reuse the `useReactVersion.mjs` from the monorepo | ||
|
|
||
| const exec = promisify(childProcess.exec); | ||
|
|
||
| // packages published from the react monorepo using the same version | ||
| const reactPackageNames = ['react', 'react-dom', 'react-is', 'react-test-renderer', 'scheduler']; | ||
| const devDependenciesPackageNames = ['@testing-library/react']; | ||
|
|
||
| // if we need to support more versions we will need to add new mapping here | ||
| const additionalVersionsMappings = { | ||
| 17: { | ||
| '@testing-library/react': '^12.1.0', | ||
| }, | ||
| 19: {}, | ||
| }; | ||
|
|
||
| async function main(version) { | ||
| if (typeof version !== 'string') { | ||
| throw new TypeError(`expected version: string but got '${version}'`); | ||
| } | ||
|
|
||
| if (version === 'stable') { | ||
| console.log('Nothing to do with stable'); | ||
| return; | ||
| } | ||
|
|
||
| const packageJsonPath = path.resolve(getWorkspaceRoot(), 'package.json'); | ||
| const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' })); | ||
|
|
||
| // the version is something in format: "17.0.0" | ||
| let majorVersion = null; | ||
|
|
||
| if (version.startsWith('^') || version.startsWith('~') || !Number.isNaN(version.charAt(0))) { | ||
| majorVersion = version.replace('^', '').replace('~', '').split('.')[0]; | ||
| } | ||
|
|
||
| await Promise.all( | ||
| reactPackageNames.map(async (reactPackageName) => { | ||
| const { stdout: versions } = await exec(`npm dist-tag ls ${reactPackageName} ${version}`); | ||
| const tagMapping = versions.split('\n').find((mapping) => { | ||
| return mapping.startsWith(`${version}: `); | ||
| }); | ||
|
|
||
| let packageVersion = null; | ||
|
|
||
| if (tagMapping === undefined) { | ||
| // Some specific version is being requested | ||
| if (majorVersion) { | ||
| packageVersion = version; | ||
| if (reactPackageName === 'scheduler') { | ||
| // get the scheduler version from the react-dom's dependencies entry | ||
| const { stdout: reactDOMDependenciesString } = await exec( | ||
| `npm view --json react-dom@${version} dependencies`, | ||
| ); | ||
| packageVersion = JSON.parse(reactDOMDependenciesString).scheduler; | ||
| } | ||
| } else { | ||
| throw new Error(`Could not find '${version}' in "${versions}"`); | ||
| } | ||
| } else { | ||
| packageVersion = tagMapping.replace(`${version}: `, ''); | ||
| } | ||
|
|
||
| packageJson.resolutions[reactPackageName] = packageVersion; | ||
| }), | ||
| ); | ||
|
|
||
| // At this moment all dist tags reference React 18 version, so we don't need | ||
| // to update these dependencies unless an older version is used, or when the | ||
| // next/experimental dist tag reference to a future version of React | ||
| // packageJson.devDependencies['@testing-library/react'] = 'alpha'; | ||
|
|
||
| if (majorVersion && additionalVersionsMappings[majorVersion]) { | ||
| devDependenciesPackageNames.forEach((packageName) => { | ||
| if (!additionalVersionsMappings[majorVersion][packageName]) { | ||
| throw new Error( | ||
| `Version ${majorVersion} does not have version defined for the ${packageName}`, | ||
| ); | ||
| } | ||
| packageJson.devDependencies[packageName] = | ||
| additionalVersionsMappings[majorVersion][packageName]; | ||
| }); | ||
| } | ||
|
|
||
| // add newline for clean diff | ||
| fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}${os.EOL}`); | ||
| } | ||
|
|
||
| const [version = process.env.REACT_VERSION] = process.argv.slice(2); | ||
| main(version).catch((error) => { | ||
| console.error(error); | ||
| process.exit(1); | ||
| }); | ||
| import '@mui/monorepo/scripts/useReactVersion.mjs'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mui/material-ui@288863b