Skip to content

Commit 857e27f

Browse files
author
Brian
committed
feat: add optional version argument to publish script
- Add TARGET_VERSION parameter to allow explicit version specification - Add semver format validation for version argument - Add version comparison check against npm registry - Update package.json and package-lock.json with target version - Auto-commit and push version bump when using explicit version
1 parent 6253039 commit 857e27f

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

scripts/publish.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
#!/usr/bin/env bash
22
set -e
33

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+
416
PACKAGE_NAME=$(node -p "require('./package.json').name")
517

618
# Step 1: Push all committed changes to origin first
@@ -79,6 +91,46 @@ else
7991
echo "✅ Step 3: Local version matches origin, no sync needed"
8092
fi
8193

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+
82134
# Check version relationship against npm
83135
VERSION_CMP=$(compare_versions "$LOCAL_VERSION" "$NPM_VERSION")
84136

0 commit comments

Comments
 (0)