|
| 1 | +#!/usr/bin/env node |
| 2 | +// @ts-ignore |
| 3 | +import { parseArgs, styleText } from 'node:util'; |
| 4 | + |
| 5 | +import { $, echo, fs } from 'zx'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Wrapper around `changeset add` (default) and `changeset status` validation (--check). |
| 9 | + * |
| 10 | + * Without --check: runs `changeset add` interactively with the correct upstream remote |
| 11 | + * auto-detected from package.json's repository URL, temporarily patched into config.json. |
| 12 | + * |
| 13 | + * With --check (CI mode): validates that all changed packages have changesets and that |
| 14 | + * no major version bumps are introduced. |
| 15 | + */ |
| 16 | + |
| 17 | +interface ChangesetStatusOutput { |
| 18 | + releases: Array<{ |
| 19 | + name: string; |
| 20 | + type: 'major' | 'minor' | 'patch' | 'none'; |
| 21 | + oldVersion: string; |
| 22 | + newVersion: string; |
| 23 | + changesets: string[]; |
| 24 | + }>; |
| 25 | + changesets: string[]; |
| 26 | +} |
| 27 | + |
| 28 | +const log = { |
| 29 | + error: (msg: string) => echo(styleText('red', msg)), |
| 30 | + success: (msg: string) => echo(styleText('green', msg)), |
| 31 | + warn: (msg: string) => echo(styleText('yellow', msg)), |
| 32 | + info: (msg: string) => echo(msg), |
| 33 | +}; |
| 34 | + |
| 35 | +/** Find the remote that matches the repo's own URL (works for forks and CI alike). */ |
| 36 | +async function getBaseBranch(): Promise<string> { |
| 37 | + const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8')); |
| 38 | + const repoUrl: string = pkg.repository?.url ?? ''; |
| 39 | + // Extract "org/repo" from https://github.com/org/repo.git or git@github.com:org/repo.git |
| 40 | + const repoPath = repoUrl.match(/github\.com[:/](.+?)(?:\.git)?$/)?.[1] ?? ''; |
| 41 | + |
| 42 | + const remotes = (await $`git remote -v`.quiet()).stdout; |
| 43 | + const remote = (repoPath && remotes.match(new RegExp(`^(\\S+)\\s+.*${repoPath}`, 'm'))?.[1]) ?? 'origin'; |
| 44 | + return `${remote}/main`; |
| 45 | +} |
| 46 | + |
| 47 | +/** Run `changeset add` interactively, temporarily patching config.json with the correct baseBranch. */ |
| 48 | +async function runAdd(baseBranch: string): Promise<void> { |
| 49 | + const configPath = '.changeset/config.json'; |
| 50 | + const config = fs.readJsonSync(configPath); |
| 51 | + const originalBaseBranch: string = config.baseBranch; |
| 52 | + config.baseBranch = baseBranch; |
| 53 | + fs.writeJsonSync(configPath, config, { spaces: 2 }); |
| 54 | + |
| 55 | + try { |
| 56 | + await $({ stdio: 'inherit' })`yarn changeset`; |
| 57 | + } finally { |
| 58 | + config.baseBranch = originalBaseBranch; |
| 59 | + fs.writeJsonSync(configPath, config, { spaces: 2 }); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function checkMajorBumps(releases: ChangesetStatusOutput['releases']): void { |
| 64 | + const majorBumps = releases.filter((r) => r.type === 'major'); |
| 65 | + if (majorBumps.length === 0) return; |
| 66 | + |
| 67 | + log.error('❌ Major version bumps detected!\n'); |
| 68 | + for (const release of majorBumps) { |
| 69 | + log.error(` ${release.name}: major`); |
| 70 | + if (release.changesets.length > 0) { |
| 71 | + log.error(` (from changesets: ${release.changesets.join(', ')})`); |
| 72 | + } |
| 73 | + } |
| 74 | + log.error('\nMajor version bumps are not allowed.'); |
| 75 | + log.warn('If you need to make a breaking change, please discuss with the team first.\n'); |
| 76 | + process.exit(1); |
| 77 | +} |
| 78 | + |
| 79 | +/** Validate that all changed packages have changesets and no major bumps are introduced. */ |
| 80 | +async function runCheck(baseBranch: string): Promise<void> { |
| 81 | + const STATUS_FILE = 'changeset-status.json'; |
| 82 | + |
| 83 | + log.info(`\n${'='.repeat(60)}`); |
| 84 | + log.info('Changesets Validation'); |
| 85 | + log.info(`${'='.repeat(60)}\n`); |
| 86 | + log.info(`Comparing against ${baseBranch}\n`); |
| 87 | + |
| 88 | + // Pre-write empty state so changeset status always has a file to overwrite |
| 89 | + fs.writeJsonSync(STATUS_FILE, { releases: [], changesets: [] }); |
| 90 | + |
| 91 | + const statusResult = await $`yarn changeset status --since=${baseBranch} --output ${STATUS_FILE}`.nothrow(); |
| 92 | + |
| 93 | + const data: ChangesetStatusOutput = fs.readJsonSync(STATUS_FILE); |
| 94 | + fs.removeSync(STATUS_FILE); |
| 95 | + |
| 96 | + // Fail: packages changed but no changeset written |
| 97 | + if (statusResult.exitCode !== 0) { |
| 98 | + log.error('❌ Some packages have been changed but no changesets were found.'); |
| 99 | + log.warn('Run `yarn change` to create a changeset, or `yarn changeset --empty` if no release is needed.\n'); |
| 100 | + process.exit(1); |
| 101 | + } |
| 102 | + |
| 103 | + checkMajorBumps(data.releases); |
| 104 | + |
| 105 | + // Pass |
| 106 | + if (data.releases.length === 0) { |
| 107 | + log.info('ℹ️ No public packages changed — no changeset required'); |
| 108 | + } else { |
| 109 | + log.success(`✅ Changesets found (${data.releases.map((r) => r.name).join(', ')})`); |
| 110 | + } |
| 111 | + log.success('\nAll validations passed! ✅\n'); |
| 112 | +} |
| 113 | + |
| 114 | +const { values: args } = parseArgs({ options: { check: { type: 'boolean', default: false } } }); |
| 115 | + |
| 116 | +const baseBranch = await getBaseBranch(); |
| 117 | + |
| 118 | +if (args.check) { |
| 119 | + await runCheck(baseBranch); |
| 120 | +} else { |
| 121 | + await runAdd(baseBranch); |
| 122 | +} |
0 commit comments