|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readFile, writeFile } from 'node:fs/promises'; |
| 4 | +import process from 'node:process'; |
| 5 | + |
| 6 | +const VERSION_HEADING_REGEX = /^##\s+\[([^\]]+)\](?:\s+-\s+.*)?\s*$/; |
| 7 | + |
| 8 | +function normalizeVersion(value) { |
| 9 | + return value.trim().replace(/^v/, ''); |
| 10 | +} |
| 11 | + |
| 12 | +function parseArgs(argv) { |
| 13 | + const args = { |
| 14 | + changelog: 'CHANGELOG.md', |
| 15 | + out: '', |
| 16 | + packageName: 'xcodebuildmcp', |
| 17 | + version: '', |
| 18 | + }; |
| 19 | + |
| 20 | + for (let i = 0; i < argv.length; i += 1) { |
| 21 | + const arg = argv[i]; |
| 22 | + const next = argv[i + 1]; |
| 23 | + |
| 24 | + if (arg === '--version') { |
| 25 | + if (!next) { |
| 26 | + throw new Error('Missing value for --version'); |
| 27 | + } |
| 28 | + args.version = next; |
| 29 | + i += 1; |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + if (arg === '--changelog') { |
| 34 | + if (!next) { |
| 35 | + throw new Error('Missing value for --changelog'); |
| 36 | + } |
| 37 | + args.changelog = next; |
| 38 | + i += 1; |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + if (arg === '--out') { |
| 43 | + if (!next) { |
| 44 | + throw new Error('Missing value for --out'); |
| 45 | + } |
| 46 | + args.out = next; |
| 47 | + i += 1; |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + if (arg === '--package') { |
| 52 | + if (!next) { |
| 53 | + throw new Error('Missing value for --package'); |
| 54 | + } |
| 55 | + args.packageName = next; |
| 56 | + i += 1; |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + if (arg === '--help' || arg === '-h') { |
| 61 | + printHelp(); |
| 62 | + process.exit(0); |
| 63 | + } |
| 64 | + |
| 65 | + throw new Error(`Unknown argument: ${arg}`); |
| 66 | + } |
| 67 | + |
| 68 | + if (!args.version) { |
| 69 | + throw new Error('Missing required argument: --version'); |
| 70 | + } |
| 71 | + |
| 72 | + return args; |
| 73 | +} |
| 74 | + |
| 75 | +function printHelp() { |
| 76 | + console.log(`Generate GitHub release notes from CHANGELOG.md. |
| 77 | +
|
| 78 | +Usage: |
| 79 | + node scripts/generate-github-release-notes.mjs --version <version> [options] |
| 80 | +
|
| 81 | +Options: |
| 82 | + --version <version> Required release version (e.g. 2.0.0 or 2.0.0-beta.1) |
| 83 | + --changelog <path> Changelog path (default: CHANGELOG.md) |
| 84 | + --out <path> Output file path (default: stdout) |
| 85 | + --package <name> Package name for install snippets (default: xcodebuildmcp) |
| 86 | + -h, --help Show this help |
| 87 | +`); |
| 88 | +} |
| 89 | + |
| 90 | +function extractChangelogSection(changelog, version) { |
| 91 | + const normalizedTarget = normalizeVersion(version); |
| 92 | + const lines = changelog.split(/\r?\n/); |
| 93 | + let sectionStartLine = -1; |
| 94 | + |
| 95 | + for (let index = 0; index < lines.length; index += 1) { |
| 96 | + const match = lines[index].match(VERSION_HEADING_REGEX); |
| 97 | + if (!match) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + if (normalizeVersion(match[1]) === normalizedTarget) { |
| 102 | + sectionStartLine = index + 1; |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (sectionStartLine === -1) { |
| 108 | + throw new Error( |
| 109 | + `Missing CHANGELOG section for version: ${normalizedTarget}\n` + |
| 110 | + `Add a heading like: ## [${normalizedTarget}] (or ## [v${normalizedTarget}] - YYYY-MM-DD)` |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + let sectionEndLine = lines.length; |
| 115 | + for (let index = sectionStartLine; index < lines.length; index += 1) { |
| 116 | + if (VERSION_HEADING_REGEX.test(lines[index])) { |
| 117 | + sectionEndLine = index; |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + const section = lines.slice(sectionStartLine, sectionEndLine).join('\n').trim(); |
| 123 | + if (!section) { |
| 124 | + throw new Error(`CHANGELOG section for version ${normalizedTarget} is empty`); |
| 125 | + } |
| 126 | + |
| 127 | + return section; |
| 128 | +} |
| 129 | + |
| 130 | +function buildInstallAndSetupSection(version, packageName) { |
| 131 | + const normalizedVersion = normalizeVersion(version); |
| 132 | + return [ |
| 133 | + '### CLI Installation', |
| 134 | + '```bash', |
| 135 | + `npm install -g ${packageName}@${normalizedVersion}`, |
| 136 | + `${packageName} --help`, |
| 137 | + '```', |
| 138 | + '', |
| 139 | + '### MCP Setup', |
| 140 | + '```json', |
| 141 | + '"XcodeBuildMCP": {', |
| 142 | + ' "command": "npx",', |
| 143 | + ` "args": ["-y", "${packageName}@${normalizedVersion}", "mcp"]`, |
| 144 | + '}', |
| 145 | + '```', |
| 146 | + '', |
| 147 | + `📦 **NPM Package**: https://www.npmjs.com/package/${packageName}/v/${normalizedVersion}`, |
| 148 | + ].join('\n'); |
| 149 | +} |
| 150 | + |
| 151 | +function buildReleaseBody(version, changelogSection, packageName) { |
| 152 | + const normalizedVersion = normalizeVersion(version); |
| 153 | + const installAndSetup = buildInstallAndSetupSection(normalizedVersion, packageName); |
| 154 | + return [ |
| 155 | + `## Release v${normalizedVersion}`, |
| 156 | + '', |
| 157 | + changelogSection, |
| 158 | + '', |
| 159 | + installAndSetup, |
| 160 | + '', |
| 161 | + ].join('\n'); |
| 162 | +} |
| 163 | + |
| 164 | +async function main() { |
| 165 | + try { |
| 166 | + const { changelog, out, packageName, version } = parseArgs(process.argv.slice(2)); |
| 167 | + const changelogContent = await readFile(changelog, 'utf8').catch(() => { |
| 168 | + throw new Error(`Could not read CHANGELOG.md at ${changelog}`); |
| 169 | + }); |
| 170 | + |
| 171 | + const section = extractChangelogSection(changelogContent, version); |
| 172 | + const body = buildReleaseBody(version, section, packageName); |
| 173 | + |
| 174 | + if (out) { |
| 175 | + await writeFile(out, body, 'utf8'); |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + process.stdout.write(body); |
| 180 | + } catch (error) { |
| 181 | + const message = error instanceof Error ? error.message : String(error); |
| 182 | + process.stderr.write(`❌ ${message}\n`); |
| 183 | + process.exit(1); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +await main(); |
0 commit comments