11import { version as oldPkgVersion } from "../package.json" with { type : "json" } ;
22import { 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
1013if ( 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
5671const jsrJsonContent = await Bun . file ( "jsr.json" ) . text ( ) ;
5772const updatedJsrJsonContent = replaceVersion (
5873 jsrJsonContent ,
59- oldVersion ,
74+ oldJsrVersion ,
6075 newVersion ,
6176) ;
6277await 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 ` ;
82102await Bun . $ `git commit -m "chore(release): v${ newVersion } "` ;
83103await Bun . $ `git tag v${ newVersion } ` ;
84104await Bun . $ `git push origin main` ;
85105await Bun . $ `git push --tags` ;
106+ await createGithubRelease ( changelogConfig , {
107+ tag_name : `v${ newVersion } ` ,
108+ body : md ,
109+ } ) ;
86110await Bun . $ `bun jsr publish` ;
0 commit comments