|
| 1 | +/** |
| 2 | + * Builds merged app + Modrinth Hosting release notes for GitHub releases and Tauri updates.json. |
| 3 | + * Hosting bullets are folded into each ## section as `- **Modrinth Hosting:** …`. |
| 4 | + * |
| 5 | + * Hosting-only sections (present in hosting changelog but not app) are appended after app sections, |
| 6 | + * ordered by: added, changed, deprecated, removed, fixed, security, then other titles alphabetically. |
| 7 | + * |
| 8 | + * Run locally: `pnpm changelog:combine-for-app -- --dry-run 0.13.2` or `pnpm scripts build-theseus-release-notes -- ...` |
| 9 | + */ |
| 10 | + |
| 11 | +import * as fs from 'fs' |
| 12 | +import { dirname, join } from 'path' |
| 13 | +import { fileURLToPath } from 'url' |
| 14 | + |
| 15 | +const __dirname = dirname(fileURLToPath(import.meta.url)) |
| 16 | +const REPO_ROOT = join(__dirname, '..') |
| 17 | + |
| 18 | +type Product = 'web' | 'app' | 'hosting' |
| 19 | + |
| 20 | +interface ChangelogEntry { |
| 21 | + date: string |
| 22 | + product: Product |
| 23 | + version: string | undefined |
| 24 | + body: string |
| 25 | +} |
| 26 | + |
| 27 | +interface ParsedSection { |
| 28 | + key: string |
| 29 | + title: string |
| 30 | + rawLines: string[] |
| 31 | +} |
| 32 | + |
| 33 | +interface HostingSectionAgg { |
| 34 | + title: string |
| 35 | + bullets: string[] |
| 36 | +} |
| 37 | + |
| 38 | +// Mirror scripts/collect-changelog.ts — used to order hosting-only sections at the end |
| 39 | +const KNOWN_SECTION_ORDER = ['added', 'changed', 'deprecated', 'removed', 'fixed', 'security'] as const |
| 40 | + |
| 41 | +function parseArgs(argv: string[]): { dryRun: boolean; version: string; outFile: string } { |
| 42 | + let dryRun = false |
| 43 | + let dryRunVersion: string | undefined |
| 44 | + let version = process.env.VERSION ? process.env.VERSION.replace(/^v/, '') : undefined |
| 45 | + |
| 46 | + for (let i = 0; i < argv.length; i++) { |
| 47 | + const a = argv[i] |
| 48 | + if (a === '--dry-run') { |
| 49 | + dryRun = true |
| 50 | + dryRunVersion = argv[++i] |
| 51 | + if (!dryRunVersion || dryRunVersion.startsWith('--')) { |
| 52 | + console.error('Usage: --dry-run <version>') |
| 53 | + process.exit(1) |
| 54 | + } |
| 55 | + } else if (a === '--version') { |
| 56 | + const v = argv[++i] |
| 57 | + if (!v || v.startsWith('--')) { |
| 58 | + console.error('--version requires a value') |
| 59 | + process.exit(1) |
| 60 | + } |
| 61 | + version = v.replace(/^v/, '') |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (dryRun) { |
| 66 | + version = dryRunVersion!.replace(/^v/, '') |
| 67 | + } else if (!version) { |
| 68 | + console.error('Set VERSION (e.g. from tag) or pass --version') |
| 69 | + process.exit(1) |
| 70 | + } |
| 71 | + |
| 72 | + const outFile = dryRun ? join(REPO_ROOT, 'test_result.md') : join(process.cwd(), 'release-notes.md') |
| 73 | + return { dryRun, version, outFile } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Parse every entry in changelog.ts VERSIONS (reverse chronological order). |
| 78 | + */ |
| 79 | +function parseChangelogEntries(src: string): ChangelogEntry[] { |
| 80 | + const entryRe = |
| 81 | + /\{\s*date:\s*`([^`]+)`,\s*product:\s*'(\w+)',(?:\s*version:\s*[`']([^`']+)[`'],)?\s*body:\s*`([\s\S]*?)`,\s*\}/g |
| 82 | + const entries: ChangelogEntry[] = [] |
| 83 | + let match: RegExpExecArray | null |
| 84 | + while ((match = entryRe.exec(src)) !== null) { |
| 85 | + entries.push({ |
| 86 | + date: match[1], |
| 87 | + product: match[2] as Product, |
| 88 | + version: match[3], |
| 89 | + body: match[4], |
| 90 | + }) |
| 91 | + } |
| 92 | + return entries |
| 93 | +} |
| 94 | + |
| 95 | +function findAppAndHosting(entries: ChangelogEntry[], version: string): { appBody: string; hostingEntries: ChangelogEntry[] } { |
| 96 | + const currentIdx = entries.findIndex((e) => e.product === 'app' && e.version === version) |
| 97 | + if (currentIdx === -1) { |
| 98 | + throw new Error(`No app changelog entry found for version ${version}`) |
| 99 | + } |
| 100 | + |
| 101 | + let newerAppIdx = -1 |
| 102 | + for (let i = currentIdx - 1; i >= 0; i--) { |
| 103 | + if (entries[i].product === 'app') { |
| 104 | + newerAppIdx = i |
| 105 | + break |
| 106 | + } |
| 107 | + } |
| 108 | + let previousAppIdx = entries.length |
| 109 | + for (let i = currentIdx + 1; i < entries.length; i++) { |
| 110 | + if (entries[i].product === 'app') { |
| 111 | + previousAppIdx = i |
| 112 | + break |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + const hostingEntries: ChangelogEntry[] = [] |
| 117 | + for (let i = newerAppIdx + 1; i < previousAppIdx; i++) { |
| 118 | + if (entries[i].product === 'hosting') { |
| 119 | + hostingEntries.push(entries[i]) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return { |
| 124 | + appBody: entries[currentIdx].body, |
| 125 | + hostingEntries, |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function parseSections(markdown: string): ParsedSection[] { |
| 130 | + const lines = markdown.split('\n') |
| 131 | + const sections: ParsedSection[] = [] |
| 132 | + let current: ParsedSection | null = null |
| 133 | + |
| 134 | + for (const line of lines) { |
| 135 | + const m = line.match(/^## (.+)$/) |
| 136 | + if (m) { |
| 137 | + const title = m[1].trim() |
| 138 | + const key = title.toLowerCase() |
| 139 | + current = { key, title, rawLines: [] } |
| 140 | + sections.push(current) |
| 141 | + } else if (current) { |
| 142 | + current.rawLines.push(line) |
| 143 | + } |
| 144 | + } |
| 145 | + return sections |
| 146 | +} |
| 147 | + |
| 148 | +function extractBulletLines(rawLines: string[]): string[] { |
| 149 | + const out: string[] = [] |
| 150 | + for (const line of rawLines) { |
| 151 | + if (/^\s*-\s/.test(line)) { |
| 152 | + out.push(line.trim()) |
| 153 | + } |
| 154 | + } |
| 155 | + return out |
| 156 | +} |
| 157 | + |
| 158 | +function toHostingBullet(line: string): string { |
| 159 | + const m = line.match(/^\s*-\s(.*)$/) |
| 160 | + const rest = m ? m[1].trim() : line.trim() |
| 161 | + return `- **Modrinth Hosting:** ${rest}` |
| 162 | +} |
| 163 | + |
| 164 | +function sortHostingOnlyKeys(keys: string[]): string[] { |
| 165 | + return [...keys].sort((a, b) => { |
| 166 | + const ia = KNOWN_SECTION_ORDER.indexOf(a as (typeof KNOWN_SECTION_ORDER)[number]) |
| 167 | + const ib = KNOWN_SECTION_ORDER.indexOf(b as (typeof KNOWN_SECTION_ORDER)[number]) |
| 168 | + const aKnown = ia !== -1 |
| 169 | + const bKnown = ib !== -1 |
| 170 | + if (aKnown && bKnown) return ia - ib |
| 171 | + if (aKnown) return -1 |
| 172 | + if (bKnown) return 1 |
| 173 | + return a.localeCompare(b) |
| 174 | + }) |
| 175 | +} |
| 176 | + |
| 177 | +function mergeAppAndHosting(appBody: string, hostingEntries: ChangelogEntry[]): string { |
| 178 | + if (!hostingEntries.length) { |
| 179 | + return appBody.replace(/\s*$/, '\n') |
| 180 | + } |
| 181 | + |
| 182 | + const appSections = parseSections(appBody) |
| 183 | + const hostingByKey = new Map<string, HostingSectionAgg>() |
| 184 | + |
| 185 | + for (const entry of hostingEntries) { |
| 186 | + for (const sec of parseSections(entry.body)) { |
| 187 | + const bullets = extractBulletLines(sec.rawLines).map(toHostingBullet) |
| 188 | + if (bullets.length === 0) continue |
| 189 | + |
| 190 | + if (!hostingByKey.has(sec.key)) { |
| 191 | + hostingByKey.set(sec.key, { title: sec.title, bullets: [] }) |
| 192 | + } |
| 193 | + hostingByKey.get(sec.key)!.bullets.push(...bullets) |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + const parts: string[] = [] |
| 198 | + |
| 199 | + for (const sec of appSections) { |
| 200 | + const appBullets = extractBulletLines(sec.rawLines) |
| 201 | + const hostBlock = hostingByKey.get(sec.key) |
| 202 | + const hostBullets = hostBlock ? hostBlock.bullets : [] |
| 203 | + if (hostBlock) { |
| 204 | + hostingByKey.delete(sec.key) |
| 205 | + } |
| 206 | + |
| 207 | + const lines = [`## ${sec.title}`, '', ...appBullets, ...hostBullets] |
| 208 | + parts.push(lines.join('\n')) |
| 209 | + } |
| 210 | + |
| 211 | + for (const key of sortHostingOnlyKeys([...hostingByKey.keys()])) { |
| 212 | + const block = hostingByKey.get(key)! |
| 213 | + parts.push(`## ${block.title}\n\n${block.bullets.join('\n')}`) |
| 214 | + } |
| 215 | + |
| 216 | + return `${parts.join('\n\n')}\n` |
| 217 | +} |
| 218 | + |
| 219 | +function main() { |
| 220 | + const { dryRun, version, outFile } = parseArgs(process.argv.slice(2)) |
| 221 | + const changelogPath = join(REPO_ROOT, 'packages/blog/changelog.ts') |
| 222 | + const src = fs.readFileSync(changelogPath, 'utf8') |
| 223 | + const entries = parseChangelogEntries(src) |
| 224 | + const { appBody, hostingEntries } = findAppAndHosting(entries, version) |
| 225 | + const output = mergeAppAndHosting(appBody, hostingEntries) |
| 226 | + |
| 227 | + fs.writeFileSync(outFile, output, 'utf8') |
| 228 | + const mode = dryRun ? 'dry-run' : 'release' |
| 229 | + const n = hostingEntries.length |
| 230 | + console.log(`Wrote ${outFile} (${mode}, app ${version}, ${n} hosting entr${n === 1 ? 'y' : 'ies'})`) |
| 231 | +} |
| 232 | + |
| 233 | +main() |
0 commit comments