8000 chore: Use changelogen to generate changelog · aklinker1/zeta@ec4c987 · GitHub 7FFF
[go: up one dir, main page]

Skip to content

Commit ec4c987

Browse files
committed
chore: Use changelogen to generate changelog
1 parent d8f5326 commit ec4c987

3 files changed

Lines changed: 129 additions & 33 deletions

File tree

‎bun.lock‎

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"devDependencies": {
2121
"@aklinker1/check": "^2.1.0",
2222
"@types/bun": "latest",
23+
"changelogen": "^0.6.2",
2324
"elysia": "^1.3.5",
2425
"expect-type": "^1.2.1",
2526
"hono": "^4.8.3",

‎scripts/publish.ts‎

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { version as oldPkgVersion } from "../package.json" with { type: "json" };
22
import { version as oldJsrVersion } from "../jsr.json" with { type: "json" };
3-
4-
const newVersion = process.argv[2];
5-
if (!newVersion) {
6-
console.error("Usage: bun run scripts/publish.ts <new_version>");
7-
process.exit(1);
8-
}
3+
import {
4+
loadChangelogConfig,
5+
getGitDiff,
6+
generateMarkDown,
7+
createGithubRelease,
8+
parseChangelogMarkdown,
9+
bumpVersion,
10+
parseCommits,
11+
} from "changelogen";
912

1013
if (oldPkgVersion !== oldJsrVersion) {
1114
console.error(
@@ -29,34 +32,46 @@ if (currentBranch !== "main") {
2932
process.exit(1);
3033
}
3134

32-
console.log("\nBumping version...", {
33-
oldVersion,
34-
newVersion,
35-
});
35+
console.log(`\nBumping version and generating changelog...`);
3636

37-
function replaceVersion(
38-
text: string,
39-
oldVersion: string,
40-
newVersion: string,
41-
): string {
42-
return text.replace(
43-
`"version": "${oldVersion}"`,
44-
`"version": "${newVersion}"`,
45-
);
37+
const changelogConfig = await loadChangelogConfig(process.cwd(), {});
38+
const commits = parseCommits(
39+
await getGitDiff(changelogConfig.from),
40+
changelogConfig,
41+
);
42+
const newVersion = await bumpVersion(commits, changelogConfig);
43+
if (!newVersion) {
44+
console.error("Failed to bump version", { commits });
45+
process.exit(1);
4646
}
4747

48-
const pkgJsonContent = await Bun.file("package.json").text();
49-
const updatedPkgJsonContent = replaceVersion(
50-
pkgJsonContent,
51-
oldVersion,
52-
newVersion,
48+
const md = await generateMarkDown(commits, changelogConfig);
49+
console.log(md);
50+
const changelog = parseChangelogMarkdown(
51+
await Bun.file("CHANGELOG.md")
52+
.text()
53+
.catch(() => ""),
54+
);
55+
changelog.releases.unshift({
56+
body: md,
57+
version: newVersion,
58+
});
59+
await Bun.write(
60+
"CHANGELOG.md",
61+
`# Changelog\n\n${changelog.releases.map((release) => release.body).join("\n\n")}\n`,
5362
);
54-
await Bun.write("package.json", updatedPkgJsonContent);
63+
64+
console.log(`Bumped version from ${oldVersion} to ${newVersion}`);
65+
66+
// Manually update jsr.json to match the new version bumped by changelogen
67+
function replaceVersion(text: string, oldVer: string, newVer: string): string {
68+
return text.replace(`"version": "${oldVer}"`, `"version": "${newVer}"`);
69+
}
5570

5671
const jsrJsonContent = await Bun.file("jsr.json").text();
5772
const updatedJsrJsonContent = replaceVersion(
5873
jsrJsonContent,
59-
oldVersion,
74+
oldJsrVersion,
6075
newVersion,
6176
);
6277
await Bun.write("jsr.json", updatedJsrJsonContent);
@@ -66,21 +81,30 @@ const changedFiles = (await Bun.$`git diff --name-only`)
6681
.split("\n")
6782
.map((line) => line.trim())
6883
.filter((line) => line !== "");
69-
if (
70-
changedFiles.length !== 2 ||
71-
changedFiles[0] !== "jsr.json" ||
72-
changedFiles[1] !== "package.json"
73-
) {
84+
console.log(changedFiles);
85+
86+
const expectedFiles = ["CHANGELOG.md", "jsr.json", "package.json"].sort();
87+
const actualFiles = changedFiles.sort();
88+
89+
if (JSON.stringify(actualFiles) !== JSON.stringify(expectedFiles)) {
7490
console.error(
75-
"Missing required changes - jsr.json and package.json should be the only changed files.",
91+
"Unexpected changed files after changelogen and jsr.json update.",
92+
"Expected:",
93+
expectedFiles,
94+
"Actual:",
95+
actualFiles,
7696
);
7797
await Bun.$`git status`;
7898
process.exit(1);
7999
}
80100

81-
await Bun.$`git add jsr.json package.json`;
101+
await Bun.$`git add jsr.json package.json CHANGELOG.md`;
82102
await Bun.$`git commit -m "chore(release): v${newVersion}"`;
83103
await Bun.$`git tag v${newVersion}`;
84104
await Bun.$`git push origin main`;
85105
await Bun.$`git push --tags`;
106+
await createGithubRelease(changelogConfig, {
107+
tag_name: `v${newVersion}`,
108+
body: md,
109+
});
86110
await Bun.$`bun jsr publish`;

0 commit comments

Comments
 (0)
0