|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -e |
3 | 3 |
|
| 4 | +# Optional version argument: npm run release [version] |
| 5 | +# Example: npm run release 2.0.0 |
| 6 | +if [[ -n "$1" ]]; then |
| 7 | + TARGET_VERSION="$1" |
| 8 | + # Validate semver format |
| 9 | + if [[ ! "$TARGET_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 10 | + echo "❌ Error: Invalid version format. Use semantic versioning (e.g., 1.2.3)" |
| 11 | + exit 1 |
| 12 | + fi |
| 13 | + echo "🎯 Target version specified: $TARGET_VERSION" |
| 14 | +fi |
| 15 | + |
4 | 16 | PACKAGE_NAME=$(node -p "require('./package.json').name") |
5 | 17 |
|
6 | 18 | # Step 1: Push all committed changes to origin first |
|
79 | 91 | echo "✅ Step 3: Local version matches origin, no sync needed" |
80 | 92 | fi |
81 | 93 |
|
| 94 | +# Handle explicit version argument |
| 95 | +if [[ -n "$TARGET_VERSION" ]]; then |
| 96 | + echo "📝 Using specified version: $TARGET_VERSION" |
| 97 | + |
| 98 | + # Check if target version is higher than npm |
| 99 | + TARGET_CMP=$(compare_versions "$TARGET_VERSION" "$NPM_VERSION") |
| 100 | + if [[ $TARGET_CMP -ne 1 ]]; then |
| 101 | + echo "❌ Error: Target version ($TARGET_VERSION) must be higher than npm version ($NPM_VERSION)" |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + |
| 105 | + # Update package.json with target version |
| 106 | + node -e " |
| 107 | + const fs = require('fs'); |
| 108 | + const pkg = require('./package.json'); |
| 109 | + pkg.version = '$TARGET_VERSION'; |
| 110 | + fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2) + '\n'); |
| 111 | + " |
| 112 | + |
| 113 | + # Update package-lock.json |
| 114 | + if [[ -f "package-lock.json" ]]; then |
| 115 | + node -e " |
| 116 | + const fs = require('fs'); |
| 117 | + const lockfile = require('./package-lock.json'); |
| 118 | + lockfile.version = '$TARGET_VERSION'; |
| 119 | + if (lockfile.packages && lockfile.packages['']) { |
| 120 | + lockfile.packages[''].version = '$TARGET_VERSION'; |
| 121 | + } |
| 122 | + fs.writeFileSync('./package-lock.json', JSON.stringify(lockfile, null, 2) + '\n'); |
| 123 | + " |
| 124 | + fi |
| 125 | + |
| 126 | + git add package.json package-lock.json 2>/dev/null || git add package.json |
| 127 | + git commit -m "chore: bump version to $TARGET_VERSION" |
| 128 | + git push origin HEAD |
| 129 | + |
| 130 | + LOCAL_VERSION="$TARGET_VERSION" |
| 131 | + echo "✅ Version set to $TARGET_VERSION" |
| 132 | +fi |
| 133 | + |
82 | 134 | # Check version relationship against npm |
83 | 135 | VERSION_CMP=$(compare_versions "$LOCAL_VERSION" "$NPM_VERSION") |
84 | 136 |
|
|
0 commit comments