Skip to content

Commit 4b0afd7

Browse files
ci: update changesets publish (#1864)
* ci: update changesets changelogs * ci: update changelog script * docs: remove changelog header * chore: fix build order * docs: update changelog headers * chore: update changelog script * docs: undo changelog example * ci: update changelog script * ci: update changelog script
1 parent 9f1e5a9 commit 4b0afd7

19 files changed

Lines changed: 7491 additions & 14484 deletions

File tree

‎.changeset/config.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://unpkg.com/@changesets/config@2.3.1/schema.json",
3-
"changelog": "@changesets/cli/changelog",
3+
"changelog": ["@changesets/changelog-github", { "repo": "stx-labs/stacks.js", "disableThanks": true }],
44
"commit": false,
55
"fixed": [["@stacks/*"]],
66
"linked": [],
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}

‎.github/workflows/publish.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ jobs:
168168
NPM_CONFIG_IGNORE_SCRIPTS: 'true'
169169

170170
- name: Update PR sticky header
171-
uses: janniks/pull-request-fixed-header@57bc082ecbd040df344747d5b8644af5fad02c95 # v1.2.0
171+
uses: janniks/pull-request-fixed-header@f41837c1d1cee46efb1481db5b6045748202ff09 # v1.3.0
172172
with:
173173
header: |
174174
> This PR was published to npm with the version `${{ steps.snapshot.outputs.version }}` (dist-tag `pr`).

‎.npmrc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
min-release-age=7200 # 5 days in minutes
1+
min-release-age=3

0 commit comments

Comments
 (0)