Repository: mui/material-ui
File: scripts/releaseTag.mjs (JavaScript)

Code before:
}

/**
 * Find the remote pointing to mui/material-ui.
 *
 * Conventionally this should be named `upstream` but some collaborators might've used a different naming scheme.
 *
 * @param {string} repo
 */
async function findMuiOrgRemote(repo) {
  const { stdout } = await execActual(['git', 'remote', '-v'].join(' '));
  const remoteLines = stdout.trim().split(/\r?\n/);

  return remoteLines
    .map((remoteLine) => {
      const [name, url, method] = remoteLine.split(/\s/);
      return { name, url, method };
    })
    .find((remote) => {
      // test: https://regex101.com/r/fBVJUX/1
      // matching:
      // - https://github.com/mui/material-ui
      // - git@github.com:mui/material-ui.git
      // but not:
      // - git@github.com:mui/material-ui-docs.git
      return new RegExp(`mui/${repo}(.git)?$`).test(remote.url) && remote.method === '(push)';
    });
}

async function main(argv) {
  const { dryRun, repo } = argv;

  const exec = dryRun ? execDry : execActual;

  const rootWorkspace = getWorkspaceRoot();
  const rootWorkspaceManifest = await fse.readJSON(path.join(rootWorkspace, 'package.json'));

  const tag = `v${rootWorkspaceManifest.version}`;
  const message = `Version ${rootWorkspaceManifest.version}`;

  await exec(['git', 'tag', '-a', tag, '-m', `"${message}"`].join(' '));
  // eslint-disable-next-line no-console -- verbose logging
  console.log(`Created tag '${tag}'. To remove enter 'git tag -d ${tag}'`);

  const muiOrgRemote = await findMuiOrgRemote(repo);
  if (muiOrgRemote === undefined) {
    throw new TypeError(
      'Unable to find the upstream remote. It should be a remote pointing to "mui/material-ui". ' +
        'Did you forget to add it via `git remote add upstream git@github.com:mui/material-ui.git`? ' +
        'If you think this is a bug please include `git remote -v` in your report.',
    );

Review comment (bug):
Wouldn't be surprised there's some hidden bugs in this regex still. My advice would be to always avoid regex if proper parsers exist. What do you think about using [`git-url-parse`](https://www.npmjs.com/package/git-url-parse) instead? It's not a new dependency, it's already in the node_modules through lerna.

Code after:
  // eslint-disable-next-line no-console
  console.log(`exec(\`${command}\`, ${JSON.stringify(options)})`);
}

/**
 * Find the remote pointing to mui/material-ui.
 *
 * Conventionally this should be named `upstream` but some collaborators might've used a different naming scheme.
 *
 * @param {string} repo
 */
async function findMuiOrgRemote(repo) {
  const { stdout } = await execActual(['git', 'remote', '-v'].join(' '));
  const remoteLines = stdout.trim().split(/\r?\n/);

  return remoteLines
    .map((remoteLine) => {
      const [name, url, method] = remoteLine.split(/\s/);
      return { name, url, method };
    })
    .find((remote) => {
      const parsed = gitUrlParse(remote.url);
      return parsed.owner === 'mui' && parsed.name === repo;
    });
}

async function main(argv) {
  const { dryRun, repo } = argv;

  const exec = dryRun ? execDry : execActual;

  const rootWorkspace = getWorkspaceRoot();
  const rootWorkspaceManifest = await fse.readJSON(path.join(rootWorkspace, 'package.json'));

  const tag = `v${rootWorkspaceManifest.version}`;
  const message = `Version ${rootWorkspaceManifest.version}`;

  await exec(['git', 'tag', '-a', tag, '-m', `"${message}"`].join(' '));
  // eslint-disable-next-line no-console -- verbose logging
  console.log(`Created tag '${tag}'. To remove enter 'git tag -d ${tag}'`);

  const muiOrgRemote = await findMuiOrgRemote(repo);
  if (muiOrgRemote === undefined) {
    throw new TypeError(
      'Unable to find the upstream remote. It should be a remote pointing to "mui/material-ui". ' +
        'Did you forget to add it via `git remote add upstream git@github.com:mui/material-ui.git`? ' +
        'If you think this is a bug please include `git remote -v` in your report.',
    );
  }

  await exec(['git', 'push', muiOrgRemote.name, tag].join(' '));