|
| 1 | +import { git, npm, readPackageJSON } from './utils.js'; |
| 2 | + |
| 3 | +let args: ParsedArgs; |
| 4 | +try { |
| 5 | + args = parseArgs(process.argv.slice(2)); |
| 6 | + ensureGitHubToken(); |
| 7 | + validateBranchState(args.releaseBranch); |
| 8 | +} catch (error) { |
| 9 | + console.error(error instanceof Error ? error.message : String(error)); |
| 10 | + process.exit(1); |
| 11 | +} |
| 12 | + |
| 13 | +console.log('Running npm version without creating a tag...'); |
| 14 | +npm().version(...args.npmVersionArgs, '--no-git-tag-version'); |
| 15 | + |
| 16 | +const { version } = readPackageJSON(); |
| 17 | +console.log(`Generating changelog for v${version}...`); |
| 18 | +const releaseChangelog = npm().runOutput('--silent', 'changelog'); |
| 19 | + |
| 20 | +console.log('Creating release commit...'); |
| 21 | +git().add('package.json', 'package-lock.json', 'src/version.ts'); |
| 22 | +git().commit('-m', releaseChangelog); |
| 23 | + |
| 24 | +const currentBranch = git({ quiet: true }).revParse('--abbrev-ref', 'HEAD'); |
| 25 | + |
| 26 | +console.log(''); |
| 27 | +console.log(`Release commit created for v${version}.`); |
| 28 | +console.log( |
| 29 | + `Next steps: push "${currentBranch}", open a PR to "${args.releaseBranch}", wait for CI, then merge.`, |
| 30 | +); |
| 31 | + |
| 32 | +interface ParsedArgs { |
| 33 | + releaseBranch: string; |
| 34 | + npmVersionArgs: Array<string>; |
| 35 | +} |
| 36 | + |
| 37 | +function parseArgs(rawArgs: ReadonlyArray<string>): ParsedArgs { |
| 38 | + const releaseBranch = rawArgs[0]; |
| 39 | + if (releaseBranch == null || releaseBranch.trim() === '') { |
| 40 | + throwUsage('Missing required release branch as the first argument.'); |
| 41 | + } |
| 42 | + |
| 43 | + const npmVersionArgs = rawArgs.slice(1); |
| 44 | + if (npmVersionArgs.length === 0) { |
| 45 | + throwUsage( |
| 46 | + 'Missing npm version arguments (e.g. patch, major, prerelease --preid alpha).', |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + return { |
| 51 | + releaseBranch, |
| 52 | + npmVersionArgs, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +function validateBranchState(releaseBranch: string): void { |
| 57 | + const checkedBranch = git({ quiet: true }).revParse('--abbrev-ref', 'HEAD'); |
| 58 | + if (checkedBranch === 'HEAD') { |
| 59 | + throw new Error( |
| 60 | + 'Git is in detached HEAD state (not on a local branch). ' + |
| 61 | + 'Switch to a local branch based on the release branch first, for example:\n' + |
| 62 | + ` git switch -c release-${releaseBranch.replace(/[^a-zA-Z0-9._-]/g, '-')} ${releaseBranch}`, |
| 63 | + ); |
| 64 | + } |
| 65 | + if (checkedBranch === releaseBranch) { |
| 66 | + throw new Error( |
| 67 | + `Release prepare must not run on "${releaseBranch}". Create a local release branch first.`, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + const status = git().status('--porcelain').trim(); |
| 72 | + if (status !== '') { |
| 73 | + throw new Error( |
| 74 | + 'Working directory must be clean before running release:prepare.', |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + let releaseBranchHead: string; |
| 79 | + try { |
| 80 | + releaseBranchHead = git({ quiet: true }).revParse(releaseBranch); |
| 81 | + } catch { |
| 82 | + throw new Error( |
| 83 | + `Release branch "${releaseBranch}" does not exist locally.`, |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + let releaseBranchUpstream: string; |
| 88 | + try { |
| 89 | + releaseBranchUpstream = git({ quiet: true }).revParse( |
| 90 | + '--abbrev-ref', |
| 91 | + `${releaseBranch}@{upstream}`, |
| 92 | + ); |
| 93 | + } catch { |
| 94 | + throw new Error( |
| 95 | + `Release branch "${releaseBranch}" does not track a remote branch. ` + |
| 96 | + 'Set one first (for example: git branch --set-upstream-to ' + |
| 97 | + `<remote>/${releaseBranch} ${releaseBranch}).`, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + const upstreamRemote = releaseBranchUpstream.split('/')[0]; |
| 102 | + try { |
| 103 | + git().fetch('--quiet', upstreamRemote, releaseBranch); |
| 104 | + } catch { |
| 105 | + throw new Error( |
| 106 | + `Failed to fetch "${releaseBranchUpstream}". ` + |
| 107 | + 'Verify network access and git remote configuration, then retry.', |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + const upstreamReleaseBranchHead = git({ quiet: true }).revParse( |
| 112 | + `${releaseBranch}@{upstream}`, |
| 113 | + ); |
| 114 | + if (releaseBranchHead !== upstreamReleaseBranchHead) { |
| 115 | + throw new Error( |
| 116 | + `Local "${releaseBranch}" is not up to date with "${releaseBranchUpstream}". ` + |
| 117 | + `Update it first (for example: git switch ${releaseBranch} && git pull --ff-only).`, |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + const currentHead = git({ quiet: true }).revParse('HEAD'); |
| 122 | + if (currentHead !== releaseBranchHead) { |
| 123 | + throw new Error( |
| 124 | + `Current branch "${checkedBranch}" must match "${releaseBranch}" before preparing a release.`, |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function ensureGitHubToken(): void { |
| 130 | + if (process.env.GH_TOKEN == null || process.env.GH_TOKEN.trim() === '') { |
| 131 | + throw new Error( |
| 132 | + 'Missing GH_TOKEN environment variable.\n' + |
| 133 | + 'Example: GH_TOKEN=<token> npm run release:prepare -- 17.x.x patch', |
| 134 | + ); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +function throwUsage(message: string): never { |
| 139 | + throw new Error( |
| 140 | + `${message}\n` + |
| 141 | + 'Usage: npm run release:prepare -- <release-branch> <npm version args>\n' + |
| 142 | + 'Examples:\n' + |
| 143 | + ' npm run release:prepare -- 17.x.x patch\n' + |
| 144 | + ' npm run release:prepare -- 17.x.x prerelease --preid alpha', |
| 145 | + ); |
| 146 | +} |
0 commit comments