Skip to content

Instantly share code, notes, and snippets.

@heroheman
Created March 18, 2026 09:41
Show Gist options
  • Select an option

  • Save heroheman/8b9df4dc8447d38998d7b0556538974d to your computer and use it in GitHub Desktop.

Select an option

Save heroheman/8b9df4dc8447d38998d7b0556538974d to your computer and use it in GitHub Desktop.
set semver tag and create changelog
#!/bin/bash
# ============================================================================
# add_version.sh - Version Management Script
# ============================================================================
#
# This script automates version bumping and release tagging for this Nuxt project.
# It reads/writes versions from package.json, creates git tags, and updates the changelog.
#
# Prerequisites:
# - jq (install via: brew install jq)
# - git cliff (install via: brew install git-cliff)
#
# Usage:
# ./add_version.sh # Auto-increment patch (0.2.1 → 0.2.2)
# ./add_version.sh --semver major # Increment major (0.2.1 → 1.0.0)
# ./add_version.sh --semver minor # Increment minor (0.2.1 → 0.3.0)
# ./add_version.sh --semver patch # Increment patch (0.2.1 → 0.2.2)
# ./add_version.sh --version X.Y.Z # Set specific version (e.g., 1.2.3)
# ./add_version.sh --amend-current # Add staged changes to current version commit & re-tag
# ./add_version.sh --revert-last-commit # Undo last version commit and delete its tag
#
# What it does:
# 1. Increments version in package.json (adds +gitcount as build number)
# 2. Asks for confirmation
# 3. Commits the version change
# 4. Creates a git tag (e.g., v0.2.2)
# 5. Updates CHANGELOG.md using git cliff
# 6. Commits the changelog
# 7. Pushes the tag to origin
#
# ============================================================================
path_to_package="package.json"
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "Error: jq is required but not installed. Install it with: brew install jq"
exit 1
fi
# ── --amend-current ──────────────────────────────────────────────────────────
if [ "$1" == "--amend-current" ]; then
current_version=$(jq -r '.version' $path_to_package)
TAG="v$current_version"
echo "Amending current version $TAG..."
echo "⚠️ This will amend the last commit and force-push tag $TAG."
echo "❓ Continue? (y/n)"
read -r confirmation
if [[ "$confirmation" != "y" && "$confirmation" != "Y" ]]; then
echo "❌ Aborted"
exit 1
fi
git add -A
git commit --amend --no-edit
git tag -d "$TAG"
git tag -a "$TAG" -m "$TAG"
git push origin "$TAG" --force
echo "✅ Tag $TAG updated successfully!"
exit 0
fi
# ── --revert-last-commit ─────────────────────────────────────────────────────
if [ "$1" == "--revert-last-commit" ]; then
current_version=$(jq -r '.version' $path_to_package)
TAG="v$current_version"
echo "Reverting last commit and deleting tag $TAG..."
echo "⚠️ This will run 'git reset --soft HEAD~1' and delete tag $TAG locally."
echo "❓ Continue? (y/n)"
read -r confirmation
if [[ "$confirmation" != "y" && "$confirmation" != "Y" ]]; then
echo "❌ Aborted"
exit 1
fi
git reset --soft HEAD~1
git tag -d "$TAG"
echo "✅ Reverted. Tag $TAG deleted locally. Staged files are ready to re-commit."
exit 0
fi
current_version=$(jq -r '.version' $path_to_package)
current_version_without_build=$(echo "$current_version" | sed 's/\+.*//')
# Parse current semver version
IFS='.' read -r major minor patch <<< "$current_version_without_build"
# Determine new version based on argument
if [ -z "$1" ]; then
# No argument: increment patch by 1
patch=$((patch + 1))
new_base_version="$major.$minor.$patch"
echo "No version specified, auto-incrementing patch: $current_version_without_build -> $new_base_version"
elif [ "$1" == "--semver" ]; then
# Semver increment
case "$2" in
major)
major=$((major + 1))
minor=0
patch=0
new_base_version="$major.$minor.$patch"
echo "Incrementing major version: $current_version_without_build -> $new_base_version"
;;
minor)
minor=$((minor + 1))
patch=0
new_base_version="$major.$minor.$patch"
echo "Incrementing minor version: $current_version_without_build -> $new_base_version"
;;
patch)
patch=$((patch + 1))
new_base_version="$major.$minor.$patch"
echo "Incrementing patch version: $current_version_without_build -> $new_base_version"
;;
*)
echo "Error: Invalid semver argument. Use: major, minor, or patch"
exit 1
;;
esac
elif [ "$1" == "--version" ]; then
# Custom version provided
new_base_version="$2"
echo "Using provided version: $new_base_version"
else
echo "Error: Invalid argument. Usage:"
echo " ./add_version.sh # Auto-increment patch"
echo " ./add_version.sh --semver major # Increment major version"
echo " ./add_version.sh --semver minor # Increment minor version"
echo " ./add_version.sh --semver patch # Increment patch version"
echo " ./add_version.sh --version X.Y.Z # Set specific version"
exit 1
fi
# Add git count as build number
# gitcount=`git log | grep "^commit" | wc -l | xargs`
# new_version="$new_base_version+$gitcount"
new_version="$new_base_version"
echo "Setting version $current_version to $new_version"
# Ask user for confirmation BEFORE making any changes
echo ""
echo "❓ Is version $new_base_version correct? (y/n)"
read -r confirmation
if [[ "$confirmation" != "y" && "$confirmation" != "Y" ]]; then
echo "❌ Aborted"
exit 1
fi
# Update version in package.json using jq
jq --arg version "$new_version" '.version = $version' $path_to_package > tmp.$$.json && mv tmp.$$.json $path_to_package
# ADD OTHER SCRIPTS
# Example: WXT
# Update version in wxt.config.ts
# sed -i '' "s/version: '[0-9]*\.[0-9]*\.[0-9]*'/version: '$new_base_version'/" wxt.config.ts
# Create git tag (needed before git cliff so it appears in changelog)
TAG="v$new_base_version"
echo "Creating git tag $TAG..."
git tag -a $TAG -m "$TAG"
# Update changelog with git cliff
echo "Updating changelog with git cliff..."
git cliff --output CHANGELOG.md
# Stage and commit all files together
echo "Staging and committing all files"
git add $path_to_package wxt.config.ts CHANGELOG.md
git commit -m "chore: bump version to $new_base_version"
# Push tag to origin
echo "Pushing tag $TAG to origin..."
git push origin $TAG
echo "✅ Version $new_base_version released and tagged successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment