|
| 1 | +import gitUrlParse from 'git-url-parse'; |
| 2 | +import childProcess from 'node:child_process'; |
| 3 | +import path from 'path'; |
| 4 | +import { promisify } from 'util'; |
| 5 | +import yargs from 'yargs'; |
| 6 | + |
| 7 | +import { findWorkspaceRoot } from '../utils/findWorkspaceRoot.mjs'; |
| 8 | +import { readJSON } from '../utils/readJson.mjs'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Only directly call it with side-effect free commands. |
| 12 | + * Otherwise use the `exec` that's considering whether the context is supposed to be "dry" i.e. have no side-effects. |
| 13 | + */ |
| 14 | +const execActual = promisify(childProcess.exec); |
| 15 | +/** |
| 16 | + * @param {string} command |
| 17 | + * @param {unknown} [options] |
| 18 | + */ |
| 19 | +async function execDry(command, options) { |
| 20 | + // eslint-disable-next-line no-console |
| 21 | + console.log(`exec(\`${command}\`, ${JSON.stringify(options)})`); |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Find the remote pointing to mui/material-ui. |
| 26 | + * |
| 27 | + * Conventionally this should be named `upstream` but some collaborators might've used a different naming scheme. |
| 28 | + * |
| 29 | + * @param {string} repo |
| 30 | + */ |
| 31 | +async function findMuiOrgRemote(repo) { |
| 32 | + const { stdout } = await execActual(['git', 'remote', '-v'].join(' ')); |
| 33 | + const remoteLines = stdout.trim().split(/\r?\n/); |
| 34 | + |
| 35 | + return remoteLines |
| 36 | + .map((remoteLine) => { |
| 37 | + const [name, url, method] = remoteLine.split(/\s/); |
| 38 | + return { name, url, method }; |
| 39 | + }) |
| 40 | + .find((remote) => { |
| 41 | + const parsed = gitUrlParse(remote.url); |
| 42 | + return parsed.owner === 'mui' && parsed.name === repo; |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +async function main(argv) { |
| 47 | + const { dryRun, repo } = argv; |
| 48 | + |
| 49 | + const exec = dryRun ? execDry : execActual; |
| 50 | + |
| 51 | + const rootWorkspace = findWorkspaceRoot(); |
| 52 | + const rootWorkspaceManifest = await readJSON(path.join(rootWorkspace, 'package.json')); |
| 53 | + |
| 54 | + const tag = `v${rootWorkspaceManifest.version}`; |
| 55 | + const message = `Version ${rootWorkspaceManifest.version}`; |
| 56 | + |
| 57 | + await exec(['git', 'tag', '-a', tag, '-m', `"${message}"`].join(' ')); |
| 58 | + // eslint-disable-next-line no-console -- verbose logging |
| 59 | + console.log(`Created tag '${tag}'. To remove enter 'git tag -d ${tag}'`); |
| 60 | + |
| 61 | + const muiOrgRemote = await findMuiOrgRemote(repo); |
| 62 | + if (muiOrgRemote === undefined) { |
| 63 | + throw new TypeError( |
| 64 | + 'Unable to find the upstream remote. It should be a remote pointing to "mui/material-ui". ' + |
| 65 | + 'Did you forget to add it via `git remote add upstream [email protected]:mui/material-ui.git`? ' + |
| 66 | + 'If you think this is a bug please include `git remote -v` in your report.', |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + await exec(['git', 'push', muiOrgRemote.name, tag].join(' ')); |
| 71 | + |
| 72 | + // eslint-disable-next-line no-console -- verbose logging |
| 73 | + console.log( |
| 74 | + `Pushed tag '${tag}' to ${muiOrgRemote.name}. This should not be reversed. In case of emergency enter 'git push --delete ${muiOrgRemote.name} ${tag}' to remove.`, |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +yargs(process.argv.slice(2)) |
| 79 | + .command({ |
| 80 | + command: '$0', |
| 81 | + description: 'Tags the current release and pushes these changes to mui/<repo-name>.', |
| 82 | + builder: (command) => { |
| 83 | + return command |
| 84 | + .option('dryRun', { |
| 85 | + default: false, |
| 86 | + describe: 'When true, the script will not have any permanent side-effects.', |
| 87 | + type: 'boolean', |
| 88 | + }) |
| 89 | + .option('repo', { |
| 90 | + describe: 'Repository to tag', |
| 91 | + type: 'string', |
| 92 | + }); |
| 93 | + }, |
| 94 | + handler: main, |
| 95 | + }) |
| 96 | + .help() |
| 97 | + .strict(true) |
| 98 | + .version(false) |
| 99 | + .parse(); |
0 commit comments