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