Skip to content

Commit

Permalink
make useReactVersion script reusable in other repos
Browse files Browse the repository at this point in the history
  • Loading branch information
cherniavskii committed Jul 2, 2024
1 parent 2938126 commit 27e5c38
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ commands:
- run:
name: Resolve React version
command: |
node scripts/useReactVersion.mjs
node scripts/useReactVersionCLI.mjs
# log a patch for maintainers who want to check out this change
git --no-pager diff HEAD
Expand Down
22 changes: 9 additions & 13 deletions scripts/useReactVersion.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ const reactPackageNames = ['react', 'react-dom', 'react-is', 'scheduler'];
const devDependenciesPackageNames = ['@testing-library/react'];

// if we need to support more versions we will need to add new mapping here
const additionalVersionsMappings = {
export const additionalVersionsMappings = {
17: {
'@testing-library/react': '^12.1.0',
},
};

async function main(version) {
export async function setReactVersion(
version,
{ versionMappings = additionalVersionsMappings, workspaceRoot = getWorkspaceRoot() } = {},
) {
if (typeof version !== 'string') {
throw new TypeError(`expected version: string but got '${version}'`);
}
Expand All @@ -37,7 +40,7 @@ async function main(version) {
return;
}

const packageJsonPath = path.resolve(getWorkspaceRoot(), 'package.json');
const packageJsonPath = path.resolve(workspaceRoot, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' }));

// the version is something in format: "17.0.0"
Expand Down Expand Up @@ -83,24 +86,17 @@ async function main(version) {
// next/experimental dist tag reference to a future version of React
// packageJson.devDependencies['@testing-library/react'] = 'alpha';

if (majorVersion && additionalVersionsMappings[majorVersion]) {
if (majorVersion && versionMappings[majorVersion]) {
devDependenciesPackageNames.forEach((packageName) => {
if (!additionalVersionsMappings[majorVersion][packageName]) {
if (!versionMappings[majorVersion][packageName]) {
throw new Error(
`Version ${majorVersion} does not have version defined for the ${packageName}`,
);
}
packageJson.devDependencies[packageName] =
additionalVersionsMappings[majorVersion][packageName];
packageJson.devDependencies[packageName] = versionMappings[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);
});
7 changes: 7 additions & 0 deletions scripts/useReactVersionCLI.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { setReactVersion } from './useReactVersion.mjs';

const [version = process.env.REACT_VERSION] = process.argv.slice(2);
setReactVersion(version).catch((error) => {
console.error(error);
process.exit(1);
});
2 changes: 1 addition & 1 deletion test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ For example, in https://app.circleci.com/pipelines/github/mui/material-ui/32796/

### Testing multiple versions of React

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 `node scripts/useReactVersion.mjs <version>`.
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 `node scripts/useReactVersionCLI.mjs <version>`.

Possible values for `version`:

Expand Down

0 comments on commit 27e5c38

Please sign in to comment.