Repository: mui/material-ui
File: scripts/use-react-version.js (JavaScript)

Code before:
// if we need to support more versions we will need to add new mapping here
const additionalVersionsMappings = {
  17: {
    scheduler: '^0.20.0',
    '@mnajdova/enzyme-adapter-react-18': 'npm:@eps1lon/enzyme-adapter-react-17',
    '@testing-library/react': '^12.1.0',
  },
};

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(__dirname, '../package.json');
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' }));

  let majorVersion = null;

  if (version.startsWith('^') || version.startsWith('~') || !Number.isNaN(version.charAt(0))) {
    majorVersion = version.replace('^', '').replace('~', '').split('.')[0];
  }

  if (majorVersion) {
    if (Object.keys(additionalVersionsMappings).indexOf(majorVersion) < 0) {
      console.log(
        `This version is not supported. This is the list of all supported versions: ${Object.keys(
          additionalVersionsMappings,
        )}`,
      );
      return;
    }
  }

  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) {

Review comment (refactor):
If this ever becomes too tedious to maintain, `npm view --json react@^17.0.0 version` might be better to resolve to an exact (SemVer compatible) version. Extracting the MAJOR from that is probably simpler.

Code after:
// if we need to support more versions we will need to add new mapping here
const additionalVersionsMappings = {
  17: {
    '@mnajdova/enzyme-adapter-react-18': 'npm:@eps1lon/enzyme-adapter-react-17',
    '@testing-library/react': '^12.1.0',
  },
};

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(__dirname, '../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}"`);
        }