|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -e # Exit immediately on error |
3 | 3 |
|
4 | | -# Ask for version number |
5 | | -read -p "Enter new version (e.g., 1.3.2-beta-0): " VERSION |
6 | | - |
7 | | -# Check if input is empty |
8 | | -if [ -z "$VERSION" ]; then |
9 | | - echo "❌ Version cannot be empty." |
| 4 | +# Ensure package.json exists |
| 5 | +if [ ! -f package.json ]; then |
| 6 | + echo "❌ package.json not found!" |
10 | 7 | exit 1 |
11 | 8 | fi |
12 | 9 |
|
13 | | -# Check if package.json exists |
14 | | -if [ ! -f package.json ]; then |
15 | | - echo "❌ package.json not found!" |
| 10 | +# Read current version from package.json |
| 11 | +CURRENT_VERSION=$(grep '"version"' package.json | sed -E 's/.*"version": *"([^"]+)".*/\1/') |
| 12 | + |
| 13 | +if [[ -z "$CURRENT_VERSION" ]]; then |
| 14 | + echo "❌ Could not read current version from package.json" |
16 | 15 | exit 1 |
17 | 16 | fi |
18 | 17 |
|
19 | | -# Update version field in package.json |
| 18 | +# Auto-increment patch version |
| 19 | +IFS='.' read -r MAJOR MINOR PATCH <<< "$(echo "$CURRENT_VERSION" | sed 's/-.*//')" # strip prerelease suffix for bump |
| 20 | +PATCH=$((PATCH + 1)) |
| 21 | +DEFAULT_VERSION="${MAJOR}.${MINOR}.${PATCH}" |
| 22 | + |
| 23 | +echo "Current version: $CURRENT_VERSION" |
| 24 | +read -p "Enter new version (press Enter for ${DEFAULT_VERSION}): " VERSION |
| 25 | + |
| 26 | +# If empty, use default patch bump |
| 27 | +if [ -z "$VERSION" ]; then |
| 28 | + VERSION="$DEFAULT_VERSION" |
| 29 | +fi |
| 30 | + |
20 | 31 | echo "🔧 Updating package.json to version $VERSION..." |
| 32 | + |
21 | 33 | # Works on macOS and Linux |
22 | 34 | sed -i.bak -E "s/\"version\": *\"[^\"]+\"/\"version\": \"$VERSION\"/" package.json |
23 | 35 | rm -f package.json.bak |
24 | 36 |
|
25 | | -# Show updated version line for confirmation |
26 | 37 | grep '"version"' package.json |
27 | 38 |
|
28 | 39 | # Commit the change |
29 | 40 | git add package.json |
30 | 41 | git commit -m "chore(release): bump version to $VERSION" |
31 | 42 |
|
32 | | -# Create a new git tag |
| 43 | +# Create tag |
33 | 44 | TAG="v$VERSION" |
34 | 45 | git tag -a "$TAG" -m "Release $TAG" |
35 | 46 |
|
36 | | -# Push changes and tag |
| 47 | +# Push |
37 | 48 | git push origin main |
38 | 49 | git push origin "$TAG" |
39 | 50 |
|
|
0 commit comments