|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Automated version bumping based on conventional commits |
| 5 | +# Usage: ./scripts/bump-version.sh |
| 6 | + |
| 7 | +# Get the current version from package.json |
| 8 | +CURRENT_VERSION=$(node -p "require('./package.json').version") |
| 9 | +echo "Current version: $CURRENT_VERSION" |
| 10 | + |
| 11 | +# Parse the version components |
| 12 | +IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION" |
| 13 | + |
| 14 | +# Get commits since last tag, or all commits if no tags exist |
| 15 | +LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") |
| 16 | +if [ -z "$LAST_TAG" ]; then |
| 17 | + echo "No tags found, analyzing all commits" |
| 18 | + COMMITS=$(git log --pretty=format:"%s") |
| 19 | +else |
| 20 | + echo "Last tag: $LAST_TAG" |
| 21 | + COMMITS=$(git log "$LAST_TAG"..HEAD --pretty=format:"%s") |
| 22 | +fi |
| 23 | + |
| 24 | +# Check if there are any commits to analyze |
| 25 | +if [ -z "$COMMITS" ]; then |
| 26 | + echo "No new commits since last tag" |
| 27 | + exit 0 |
| 28 | +fi |
| 29 | + |
| 30 | +echo "Analyzing commits..." |
| 31 | + |
| 32 | +# Determine version bump type based on conventional commits |
| 33 | +BUMP_TYPE="patch" |
| 34 | + |
| 35 | +# Check for breaking changes (BREAKING CHANGE: or feat!: or fix!:) |
| 36 | +if echo "$COMMITS" | grep -qE "^(feat|fix|chore|docs|style|refactor|perf|test)!:|BREAKING[ -]CHANGE:"; then |
| 37 | + BUMP_TYPE="major" |
| 38 | + echo "Found breaking changes, bumping major version" |
| 39 | +# Check for new features (feat:) |
| 40 | +elif echo "$COMMITS" | grep -qE "^feat(\(.*\))?:"; then |
| 41 | + BUMP_TYPE="minor" |
| 42 | + echo "Found new features, bumping minor version" |
| 43 | +# Check for fixes, chores, docs, etc. (fix:, chore:, docs:, etc.) |
| 44 | +elif echo "$COMMITS" | grep -qE "^(fix|chore|docs|style|refactor|perf|test)(\(.*\))?:"; then |
| 45 | + BUMP_TYPE="patch" |
| 46 | + echo "Found patches/fixes, bumping patch version" |
| 47 | +else |
| 48 | + echo "No conventional commits found, bumping patch version" |
| 49 | + BUMP_TYPE="patch" |
| 50 | +fi |
| 51 | + |
| 52 | +# Calculate new version |
| 53 | +case "$BUMP_TYPE" in |
| 54 | + major) |
| 55 | + NEW_VERSION="$((MAJOR + 1)).0.0" |
| 56 | + ;; |
| 57 | + minor) |
| 58 | + NEW_VERSION="$MAJOR.$((MINOR + 1)).0" |
| 59 | + ;; |
| 60 | + patch) |
| 61 | + NEW_VERSION="$MAJOR.$MINOR.$((PATCH + 1))" |
| 62 | + ;; |
| 63 | +esac |
| 64 | + |
| 65 | +echo "New version: $NEW_VERSION" |
| 66 | + |
| 67 | +# Update package.json |
| 68 | +sed -i.bak "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" package.json && rm package.json.bak |
| 69 | + |
| 70 | +# Update server.json |
| 71 | +sed -i.bak "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" server.json && rm server.json.bak |
| 72 | + |
| 73 | +# Update package-lock.json (it has version in multiple places) |
| 74 | +sed -i.bak "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/g" package-lock.json && rm package-lock.json.bak |
| 75 | + |
| 76 | +echo "Version bumped from $CURRENT_VERSION to $NEW_VERSION" |
| 77 | + |
| 78 | +# Output for GitHub Actions |
| 79 | +if [ -n "$GITHUB_OUTPUT" ]; then |
| 80 | + echo "NEW_VERSION=$NEW_VERSION" >> "$GITHUB_OUTPUT" |
| 81 | +fi |
0 commit comments