|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { execFileSync } from 'node:child_process'; |
| 4 | +import { readFile, writeFile } from 'node:fs/promises'; |
| 5 | +import path from 'node:path'; |
| 6 | + |
| 7 | +const ROOT_CHANGELOG = 'CHANGELOG.md'; |
| 8 | +const REPO_SLUG = 'stx-labs/stacks.js'; |
| 9 | +const PACKAGE_CHANGELOG_RE = /^packages\/[^/]+\/CHANGELOG\.md$/; |
| 10 | + |
| 11 | +const changed = git(['diff', '--name-only', '--', 'packages/*/CHANGELOG.md']) |
| 12 | + .split('\n') |
| 13 | + .filter((file) => PACKAGE_CHANGELOG_RE.test(file)); |
| 14 | + |
| 15 | +const releases = (await Promise.all(changed.map(readRelease))).filter(Boolean); |
| 16 | +if (releases.length === 0) { |
| 17 | + console.log('No package release sections found; root changelog unchanged.'); |
| 18 | + process.exit(0); |
| 19 | +} |
| 20 | + |
| 21 | +releases.sort((a, b) => a.name.localeCompare(b.name)); |
| 22 | + |
| 23 | +const versions = new Set(releases.map((release) => release.version)); |
| 24 | +if (versions.size !== 1) throw new Error(`Expected one release version, found: ${[...versions].join(', ')}`); |
| 25 | + |
| 26 | +const version = [...versions][0]; |
| 27 | +const root = await readFile(ROOT_CHANGELOG, 'utf8'); |
| 28 | +const section = renderSection({ |
| 29 | + date: new Date().toISOString().slice(0, 10), |
| 30 | + previousVersion: previousReleaseVersion(root, version), |
| 31 | + version, |
| 32 | + groups: groupByBody(releases), |
| 33 | +}); |
| 34 | + |
| 35 | +await writeFile(ROOT_CHANGELOG, upsertSection(root, version, section)); |
| 36 | +console.log(`Updated ${ROOT_CHANGELOG} for ${version}.`); |
| 37 | + |
| 38 | +async function readRelease(changelogPath) { |
| 39 | + const before = topRelease(gitShow(`HEAD:${changelogPath}`)); |
| 40 | + const after = topRelease(await readFile(changelogPath, 'utf8')); |
| 41 | + if (!after || after.heading === before?.heading) return null; |
| 42 | + |
| 43 | + const body = rootBody(after.body); |
| 44 | + if (!body) return null; |
| 45 | + |
| 46 | + const packageJson = JSON.parse(await readFile(path.join(path.dirname(changelogPath), 'package.json'), 'utf8')); |
| 47 | + return { name: packageJson.name, version: packageJson.version, body }; |
| 48 | +} |
| 49 | + |
| 50 | +function rootBody(body) { |
| 51 | + const normalized = body |
| 52 | + .replace(/^- Updated dependencies.*:\n(?: - .+\n?)+/gm, '') |
| 53 | + .replace(/^All notable changes to this project will be documented in this file\.\n?/gm, '') |
| 54 | + .replace(/^See \[Conventional Commits\]\(https:\/\/conventionalcommits\.org\) for commit guidelines\.\n?/gm, '') |
| 55 | + .replace(/^### /gm, '#### ') |
| 56 | + .split(/(?=^#### )/m) |
| 57 | + .map((block) => block.trim()) |
| 58 | + .filter((block) => block && !/^#### .+$/.test(block)) |
| 59 | + .join('\n\n'); |
| 60 | + |
| 61 | + return normalized || '**Note:** Updated sibling dependencies only.'; |
| 62 | +} |
| 63 | + |
| 64 | +function topRelease(markdown) { |
| 65 | + const heading = /^##\s+.+$/m.exec(markdown); |
| 66 | + if (!heading) return null; |
| 67 | + const start = heading.index + heading[0].length; |
| 68 | + const next = /^##\s+.+$/m.exec(markdown.slice(start)); |
| 69 | + const end = next ? start + next.index : markdown.length; |
| 70 | + return { heading: heading[0].trim(), body: markdown.slice(start, end).trim() }; |
| 71 | +} |
| 72 | + |
| 73 | +function groupByBody(releases) { |
| 74 | + return [...releases.reduce((groups, release) => { |
| 75 | + const group = groups.get(release.body) ?? { names: [], body: release.body }; |
| 76 | + group.names.push(release.name); |
| 77 | + return groups.set(release.body, group); |
| 78 | + }, new Map()).values()]; |
| 79 | +} |
| 80 | + |
| 81 | +function renderSection({ date, previousVersion, version, groups }) { |
| 82 | + const compare = `https://github.com/${REPO_SLUG}/compare/v${previousVersion}...v${version}`; |
| 83 | + const packages = groups |
| 84 | + .map(({ names, body }) => `### ${names.join(', ')}\n\n${body}`) |
| 85 | + .join('\n\n'); |
| 86 | + return `## [${version}](${compare}) (${date})\n\n${packages}\n\n`; |
| 87 | +} |
| 88 | + |
| 89 | +function upsertSection(markdown, version, section) { |
| 90 | + const existing = new RegExp(`^##(?: \\[${escapeRegExp(version)}\\]| ${escapeRegExp(version)})[\\s\\S]*?(?=^##\\s+)`, 'm'); |
| 91 | + if (existing.test(markdown)) return markdown.replace(existing, section); |
| 92 | + |
| 93 | + const firstRelease = /^##\s+/m.exec(markdown); |
| 94 | + if (!firstRelease) return `${markdown.trimEnd()}\n\n${section}`; |
| 95 | + return `${markdown.slice(0, firstRelease.index)}${section}${markdown.slice(firstRelease.index)}`; |
| 96 | +} |
| 97 | + |
| 98 | +function previousReleaseVersion(markdown, version) { |
| 99 | + const releases = [...markdown.matchAll(/^##\s+(?:\[(\d+\.\d+\.\d+)\]|\d+\.\d+\.\d+)/gm)] |
| 100 | + .map((match) => match[1] ?? match[0].replace(/^##\s+/, '')); |
| 101 | + return releases.find((release) => release !== version) ?? 'HEAD'; |
| 102 | +} |
| 103 | + |
| 104 | +function git(args) { |
| 105 | + return execFileSync('git', args, { encoding: 'utf8' }).trim(); |
| 106 | +} |
| 107 | + |
| 108 | +function gitShow(ref) { |
| 109 | + try { |
| 110 | + return execFileSync('git', ['show', ref], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }); |
| 111 | + } catch { |
| 112 | + return ''; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function escapeRegExp(value) { |
| 117 | + return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 118 | +} |
0 commit comments