1+ #! /bin/bash
2+
3+ # Check if version argument is provided
4+ if [ -z " $1 " ]; then
5+ echo " Error: Version argument is required"
6+ echo " Usage: ./scripts/generate-changelog.sh <version>"
7+ exit 1
8+ fi
9+
10+ # Get the current date with ordinal suffix
11+ day_suffix=$( date +" %e" | sed ' s/^[[:space:]]*//' )
12+ if [ " $day_suffix " = " 1" ] || [ " $day_suffix " = " 21" ] || [ " $day_suffix " = " 31" ]; then
13+ suffix=" st"
14+ elif [ " $day_suffix " = " 2" ] || [ " $day_suffix " = " 22" ]; then
15+ suffix=" nd"
16+ elif [ " $day_suffix " = " 3" ] || [ " $day_suffix " = " 23" ]; then
17+ suffix=" rd"
18+ else
19+ suffix=" th"
20+ fi
21+ today=" $( date +" %b %-d" ) $suffix $( date +" %Y" ) "
22+
23+ # Check if conventional-changelog is installed
24+ if ! command -v conventional-changelog & > /dev/null; then
25+ echo " Error: conventional-changelog is not installed"
26+ echo " Please install it using: npm install -g conventional-changelog-cli"
27+ exit 1
28+ fi
29+
30+ # Get the previous version from package.json
31+ prev_version=$( node -p " require('./package.json').version" )
32+ echo " Previous version: $prev_version "
33+
34+ # Create a temporary file for the new changelog
35+ temp_file=$( mktemp)
36+
37+ # Generate new changelog entries for the specific version
38+ version=" $1 "
39+ version=${version# v}
40+ echo " Generating changelog for version $version ..."
41+
42+ # Get the previous version tag
43+ prev_tag=$( git tag --sort=-v:refname | grep -A 1 " v$version " | tail -n 1)
44+ if [ -z " $prev_tag " ]; then
45+ echo " Error: Could not find previous tag for version $version "
46+ exit 1
47+ fi
48+ echo " Previous tag: $prev_tag "
49+
50+ # Get commit messages between tags
51+ commit_messages=$( git log --pretty=format:" - %s" $prev_tag ..v$version )
52+
53+ # Create the new version header and content
54+ version_header=" ## $version ($today )
55+
56+ $commit_messages
57+ "
58+
59+ # Debug: Show the final header
60+ echo " Final version header:"
61+ echo " $version_header "
62+
63+ # Add the new version at the top of the existing changelog
64+ if [ -f CHANGELOG.md ]; then
65+ # If the file exists, add the new version after the title
66+ # Create a temporary file for the new content
67+ temp_changelog=$( mktemp)
68+ # Add the title
69+ head -n 1 CHANGELOG.md > " $temp_changelog "
70+ # Add the new version header
71+ echo " $version_header " >> " $temp_changelog "
72+ # Add the rest of the existing changelog
73+ tail -n +2 CHANGELOG.md >> " $temp_changelog "
74+ # Replace the original file
75+ mv " $temp_changelog " CHANGELOG.md
76+ else
77+ # If the file doesn't exist, create it with the title and new version
78+ echo " # Change Log
79+
80+ $version_header " > CHANGELOG.md
81+ fi
82+
83+ # Clean up
84+ rm " $temp_file "
85+
86+ # Extract release notes for GitHub release
87+ notes=$( sed -n " /## $version /,/^##/p" ./CHANGELOG.md | grep -v ' ##' )
88+
89+ # If running in GitHub Actions, output the notes
90+ if [ -n " $GITHUB_OUTPUT " ]; then
91+ # Escape special characters for GitHub Actions
92+ notes=" ${notes// ' /' % 27} "
93+ notes=" ${notes// $' \n ' / ' %0A' } "
94+ notes=" ${notes// $' \r ' / ' %0D' } "
95+ delimiter=" $( openssl rand -hex 8) "
96+ echo " notes<<${delimiter} " >> " $GITHUB_OUTPUT "
97+ echo " $notes " >> " $GITHUB_OUTPUT "
98+ echo " ${delimiter} " >> " $GITHUB_OUTPUT "
99+ else
100+ # If running locally, just print the notes
101+ echo " Generated changelog for version $version "
102+ echo " Release notes:"
103+ echo " $notes "
104+ fi
0 commit comments