Skip to content

Commit 3698841

Browse files
authored
[code-infra] Reuse useReactVersion script from the monorepo (#13710)
1 parent 0fc8707 commit 3698841

File tree

5 files changed

+16
-132
lines changed

5 files changed

+16
-132
lines changed

.circleci/config.yml

+7-16
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,6 @@ commands:
5959
description: 'Set to true if you intend to any browser (for example with playwright).'
6060

6161
steps:
62-
- run:
63-
name: Resolve React version
64-
command: |
65-
node scripts/useReactVersion.mjs
66-
# log a patch for maintainers who want to check out this change
67-
git --no-pager diff HEAD
68-
6962
- when:
7063
condition: << parameters.browsers >>
7164
steps:
@@ -97,16 +90,14 @@ commands:
9790
pnpm --version
9891
- run:
9992
name: Install js dependencies
93+
command: pnpm install
94+
95+
- run:
96+
name: Resolve React version
10097
command: |
101-
echo "React version $REACT_VERSION"
102-
if [ $REACT_VERSION == "stable" ];
103-
then
104-
echo "pnpm install"
105-
pnpm install
106-
else
107-
echo "pnpm install --no-frozen-lockfile"
108-
pnpm install --no-frozen-lockfile
109-
fi
98+
pnpm use-react-version
99+
# log a patch for maintainers who want to check out this change
100+
git --no-pager diff HEAD
110101
111102
- when:
112103
condition: << parameters.browsers >>

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"test:argos": "node ./scripts/pushArgos.mjs",
5555
"typescript": "lerna run --no-bail --parallel typescript",
5656
"typescript:ci": "lerna run --concurrency 3 --no-bail --no-sort typescript",
57+
"use-react-version": "node scripts/useReactVersion.mjs",
5758
"build:codesandbox": "pnpm release:build",
5859
"install:codesandbox": "pnpm install --no-frozen-lockfile",
5960
"release:changelog": "node scripts/releaseChangelog.mjs",
@@ -89,7 +90,7 @@
8990
"@mui/internal-markdown": "^1.0.7",
9091
"@mui/internal-test-utils": "^1.0.4",
9192
"@mui/material": "^5.16.2",
92-
"@mui/monorepo": "github:mui/material-ui#d3d1675b919e937a46e7991856dd8edfa9b53a3e",
93+
"@mui/monorepo": "github:mui/material-ui#288863bd2f8681a82c4bfbaf13215b41043bc551",
9394
"@mui/utils": "^5.16.2",
9495
"@next/eslint-plugin-next": "14.2.5",
9596
"@octokit/plugin-retry": "^7.1.1",

pnpm-lock.yaml

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/useReactVersion.mjs

+1-109
Original file line numberDiff line numberDiff line change
@@ -1,109 +1 @@
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 = ['@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-
'@testing-library/react': '^12.1.0',
29-
},
30-
19: {},
31-
};
32-
33-
async function main(version) {
34-
if (typeof version !== 'string') {
35-
throw new TypeError(`expected version: string but got '${version}'`);
36-
}
37-
38-
if (version === 'stable') {
39-
console.log('Nothing to do with stable');
40-
return;
41-
}
42-
43-
const packageJsonPath = path.resolve(getWorkspaceRoot(), 'package.json');
44-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' }));
45-
46-
// the version is something in format: "17.0.0"
47-
let majorVersion = null;
48-
49-
if (version.startsWith('^') || version.startsWith('~') || !Number.isNaN(version.charAt(0))) {
50-
majorVersion = version.replace('^', '').replace('~', '').split('.')[0];
51-
}
52-
53-
await Promise.all(
54-
reactPackageNames.map(async (reactPackageName) => {
55-
const { stdout: versions } = await exec(`npm dist-tag ls ${reactPackageName} ${version}`);
56-
const tagMapping = versions.split('\n').find((mapping) => {
57-
return mapping.startsWith(`${version}: `);
58-
});
59-
60-
let packageVersion = null;
61-
62-
if (tagMapping === undefined) {
63-
// Some specific version is being requested
64-
if (majorVersion) {
65-
packageVersion = version;
66-
if (reactPackageName === 'scheduler') {
67-
// get the scheduler version from the react-dom's dependencies entry
68-
const { stdout: reactDOMDependenciesString } = await exec(
69-
`npm view --json react-dom@${version} dependencies`,
70-
);
71-
packageVersion = JSON.parse(reactDOMDependenciesString).scheduler;
72-
}
73-
} else {
74-
throw new Error(`Could not find '${version}' in "${versions}"`);
75-
}
76-
} else {
77-
packageVersion = tagMapping.replace(`${version}: `, '');
78-
}
79-
80-
packageJson.resolutions[reactPackageName] = packageVersion;
81-
}),
82-
);
83-
84-
// At this moment all dist tags reference React 18 version, so we don't need
85-
// to update these dependencies unless an older version is used, or when the
86-
// next/experimental dist tag reference to a future version of React
87-
// packageJson.devDependencies['@testing-library/react'] = 'alpha';
88-
89-
if (majorVersion && additionalVersionsMappings[majorVersion]) {
90-
devDependenciesPackageNames.forEach((packageName) => {
91-
if (!additionalVersionsMappings[majorVersion][packageName]) {
92-
throw new Error(
93-
`Version ${majorVersion} does not have version defined for the ${packageName}`,
94-
);
95-
}
96-
packageJson.devDependencies[packageName] =
97-
additionalVersionsMappings[majorVersion][packageName];
98-
});
99-
}
100-
101-
// add newline for clean diff
102-
fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}${os.EOL}`);
103-
}
104-
105-
const [version = process.env.REACT_VERSION] = process.argv.slice(2);
106-
main(version).catch((error) => {
107-
console.error(error);
108-
process.exit(1);
109-
});
1+
import '@mui/monorepo/scripts/useReactVersion.mjs';

test/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
You can check integration of different versions of React (for example different [release channels](https://react.dev/community/versioning-policy) or PRs to React) by running the following commands:
66

7-
1. `node scripts/useReactVersion.mjs <version>`.
7+
1. `pnpm use-react-version <version>`.
88

99
Possible values for `version`:
1010

0 commit comments

Comments
 (0)