Skip to content

Commit a1d9ca4

Browse files
committed
chore: add version.sh script to automate version bumping
The script automates the process of incrementing the version in package.json based on the specified release type (patch, minor, major). It also supports an optional --publish flag to build and publish the package to npm, along with committing and tagging the changes in git. This reduces manual errors and streamlines the versioning process.
1 parent dd39c86 commit a1d9ca4

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

version.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
3+
# Function to display usage
4+
show_usage() {
5+
echo "Usage: $0 [patch|minor|major] [--publish]"
6+
echo "Example: $0 patch --publish"
7+
exit 1
8+
}
9+
10+
# Function to increment version
11+
increment_version() {
12+
local version=$1
13+
local release=$2
14+
15+
IFS='.' read -ra ver <<< "$version"
16+
major=${ver[0]}
17+
minor=${ver[1]}
18+
patch=${ver[2]}
19+
20+
case $release in
21+
major)
22+
major=$((major + 1))
23+
minor=0
24+
patch=0
25+
;;
26+
minor)
27+
minor=$((minor + 1))
28+
patch=0
29+
;;
30+
patch)
31+
patch=$((patch + 1))
32+
;;
33+
*)
34+
echo "Invalid release type. Use: patch, minor, or major"
35+
exit 1
36+
;;
37+
esac
38+
39+
echo "$major.$minor.$patch"
40+
}
41+
42+
# Check arguments
43+
if [ $# -lt 1 ]; then
44+
show_usage
45+
fi
46+
47+
# Get current version from package.json
48+
current_version=$(node -p "require('./package.json').version")
49+
release_type=$1
50+
should_publish=false
51+
52+
# Check for publish flag
53+
if [ "$2" == "--publish" ]; then
54+
should_publish=true
55+
fi
56+
57+
# Calculate new version
58+
new_version=$(increment_version "$current_version" "$release_type")
59+
60+
# Update package.json version
61+
tmp=$(mktemp)
62+
jq ".version = \"$new_version\"" package.json > "$tmp" && mv "$tmp" package.json
63+
64+
echo "Version updated from $current_version to $new_version"
65+
66+
# Git operations
67+
git add package.json
68+
git commit -m "chore: bump version to $new_version"
69+
git tag "v$new_version"
70+
71+
# Publish if flag is set
72+
if [ "$should_publish" = true ]; then
73+
npm run build
74+
npm publish
75+
git push origin main
76+
git push origin "v$new_version"
77+
echo "Published version $new_version to npm"
78+
else
79+
echo "Run with --publish flag to publish to npm"
80+
fi

0 commit comments

Comments
 (0)