|
| 1 | +#!yarn ts-node |
| 2 | + |
| 3 | +import execa from 'execa'; |
| 4 | +import semver from 'semver'; |
| 5 | +import yargs from 'yargs/yargs'; |
| 6 | +import { hideBin } from 'yargs/helpers'; |
| 7 | + |
| 8 | +type Workspace = { |
| 9 | + location: string; |
| 10 | + name: string; |
| 11 | +}; |
| 12 | + |
| 13 | +enum Format { |
| 14 | + PerCommit = 'per-commit', |
| 15 | + Together = 'together', |
| 16 | +} |
| 17 | + |
| 18 | +class KnownError extends Error { |
| 19 | + // do nothing |
| 20 | +} |
| 21 | + |
| 22 | +main().catch((error) => { |
| 23 | + if (error instanceof KnownError) { |
| 24 | + process.stderr.write(`${error.message}\n`); |
| 25 | + } else { |
| 26 | + console.error(error); |
| 27 | + } |
| 28 | + process.exitCode = 1; |
| 29 | +}); |
| 30 | + |
| 31 | +/** |
| 32 | + * Show changes for a workspace package since its last release. |
| 33 | + * |
| 34 | + * Here's a couple of examples of how you can call this script: |
| 35 | + * |
| 36 | + * ``` |
| 37 | + * scripts/changes-since-last-release.ts --format per-commit @metamask/transaction-controller | less -r |
| 38 | + * scripts/changes-since-last-release.ts --format together @metamask/transaction-controller | less -r |
| 39 | + * ``` |
| 40 | + */ |
| 41 | +async function main() { |
| 42 | + const { packageName, format } = await yargs(hideBin(process.argv)) |
| 43 | + .scriptName('scripts/changes-since-last-release.ts') |
| 44 | + .command( |
| 45 | + '$0 <package-name>', |
| 46 | + 'Show changes for a workspace package since its previous release.', |
| 47 | + (y) => { |
| 48 | + y.positional('package-name', { |
| 49 | + describe: 'The name of the package to show changes for.', |
| 50 | + }); |
| 51 | + }, |
| 52 | + ) |
| 53 | + .option('format', { |
| 54 | + alias: 'f', |
| 55 | + describe: 'How to show the changes: divided by commit or all together', |
| 56 | + choices: Object.values(Format), |
| 57 | + default: Format.PerCommit, |
| 58 | + }) |
| 59 | + .string('_') |
| 60 | + .help() |
| 61 | + .strict() |
| 62 | + .parse(); |
| 63 | + |
| 64 | + const workspaces = await getWorkspaces(); |
| 65 | + const workspace = workspaces.find((w) => w.name === packageName); |
| 66 | + if (!workspace) { |
| 67 | + throw new KnownError(`Could not map ${packageName} to a workspace`); |
| 68 | + } |
| 69 | + |
| 70 | + const tags = await getTags(); |
| 71 | + const tagsForWorkspace = tags.filter((line) => |
| 72 | + line.startsWith(`${workspace.name}@`), |
| 73 | + ); |
| 74 | + if (tagsForWorkspace.length === 0) { |
| 75 | + throw new KnownError(`No version tags found for ${workspace.name}.`); |
| 76 | + } |
| 77 | + const versions = tagsForWorkspace.map((tag) => { |
| 78 | + const versionString = tag.split('@', 3)[2]; |
| 79 | + const parsedVersionString = semver.parse(versionString); |
| 80 | + if (!parsedVersionString) { |
| 81 | + throw new Error(`Invalid version tag ${tag}`); |
| 82 | + } |
| 83 | + return parsedVersionString; |
| 84 | + }); |
| 85 | + const sortedVersions = versions.sort((a, b) => a.compare(b)); |
| 86 | + const latestVersion = sortedVersions[sortedVersions.length - 1]; |
| 87 | + |
| 88 | + console.log( |
| 89 | + `Showing changes for \u001B[34m${ |
| 90 | + workspace.name |
| 91 | + }\u001B[0m since \u001B[34m${latestVersion.toString()}\u001B[0m.\n`, |
| 92 | + ); |
| 93 | + await showDiff(workspace, latestVersion, { format }); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Retrieve the set of Yarn workspaces in this project. |
| 98 | + * |
| 99 | + * @returns The set of workspaces. |
| 100 | + */ |
| 101 | +async function getWorkspaces(): Promise<Workspace[]> { |
| 102 | + const { stdout } = await execa('yarn', ['workspaces', 'list', '--json']); |
| 103 | + const workspaces = stdout.split('\n').map((line) => JSON.parse(line)); |
| 104 | + return workspaces.filter((workspace) => workspace.location !== '.'); |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Retrieve the set of Git tags in this project. |
| 109 | + * |
| 110 | + * @returns The set of tags. |
| 111 | + */ |
| 112 | +async function getTags(): Promise<string[]> { |
| 113 | + const { stdout } = await execa('git', ['tag']); |
| 114 | + return stdout.split('\n'); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Show the set of changes in the latest release of the given package. |
| 119 | + * |
| 120 | + * @param workspace - The workspace that represents the package. |
| 121 | + * @param latestVersion - The version (as a `semver` object). |
| 122 | + * @param options - An options bag. |
| 123 | + * @param options.format - How to show the changes: divided by commit or all |
| 124 | + * together. |
| 125 | + */ |
| 126 | +async function showDiff( |
| 127 | + workspace: Workspace, |
| 128 | + latestVersion: semver.SemVer, |
| 129 | + { format = Format.PerCommit } = {}, |
| 130 | +): Promise<void> { |
| 131 | + const gitOptions = ['--no-pager']; |
| 132 | + const gitCommandOptions = ['--color']; |
| 133 | + const args = |
| 134 | + format === Format.PerCommit |
| 135 | + ? [ |
| 136 | + ...gitOptions, |
| 137 | + 'log', |
| 138 | + ...gitCommandOptions, |
| 139 | + '-p', |
| 140 | + `${workspace.name}@${latestVersion.toString()}..HEAD`, |
| 141 | + '--', |
| 142 | + workspace.location, |
| 143 | + ] |
| 144 | + : [ |
| 145 | + ...gitOptions, |
| 146 | + 'diff', |
| 147 | + ...gitCommandOptions, |
| 148 | + `${workspace.name}@${latestVersion.toString()}..HEAD`, |
| 149 | + workspace.location, |
| 150 | + ]; |
| 151 | + await execa('git', args, { |
| 152 | + stdio: 'inherit', |
| 153 | + }); |
| 154 | +} |
0 commit comments