You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
set -euo pipefail # Exit on error, undefined vars, and pipe failures
72
+
73
+
# Function to validate semver format
74
+
# Supports standard versions (x.y.z) and pre-release versions (x.y.z-beta.n)
75
+
validate_version() {
76
+
local version="$1"
77
+
if ! [[ "$version" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$ ]]; then
78
+
echo "Error: Invalid version format: $version"
79
+
echo "Version must be in format: x.y.z or x.y.z-pre.n"
80
+
exit 1
81
+
fi
82
+
}
83
+
84
+
# Function to update version in JSON files
85
+
update_json_version() {
86
+
local file="$1"
87
+
local cur_ver="$2"
88
+
local new_ver="$3"
89
+
90
+
if [[ ! -f "$file" ]]; then
91
+
echo "Error: File $file not found"
92
+
exit 1
93
+
fi
94
+
95
+
# Check if version field exists
96
+
if ! grep -q "\"version\":" "$file"; then
97
+
echo "Error: No version field found in $file"
98
+
exit 1
99
+
fi
100
+
101
+
# Backup file before modification
102
+
cp "$file" "${file}.bak"
103
+
104
+
if ! sed -i "s/\"version\": \"$cur_ver\"/\"version\": \"$new_ver\"/" "$file"; then
105
+
echo "Error: Failed to update version in $file"
106
+
mv "${file}.bak" "$file" # Restore backup
107
+
exit 1
108
+
fi
109
+
110
+
rm "${file}.bak" # Remove backup if successful
111
+
}
112
+
113
+
# Function to compare versions
114
+
version_gt() {
115
+
test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"
116
+
}
117
+
118
+
# Get current version with error handling
119
+
if [[ ! -f "package.json" ]]; then
120
+
echo "Error: package.json not found"
121
+
exit 1
122
+
fi
123
+
124
+
if ! current_version=$(node -p "try { require('./package.json').version } catch(e) { console.error(e); process.exit(1) }" 2>/dev/null); then
125
+
echo "Error: Failed to read version from package.json"
126
+
exit 1
127
+
fi
128
+
129
+
# Validate version format
130
+
validate_version "$current_version"
131
+
132
+
# Get highest existing version with error handling
133
+
if ! highest_version=$(git ls-remote --tags origin | grep "refs/tags/chrome-extension-v" | sed 's/.*chrome-extension-v//' | sort -V | tail -n 1); then
134
+
echo "Warning: Failed to fetch remote tags, proceeding with caution"
135
+
fi
136
+
137
+
# Prevent version downgrade
138
+
# Note: To downgrade versions, you need to manually:
139
+
# 1. Delete the higher version tag: git push origin :refs/tags/chrome-extension-v<version>
140
+
# 2. Delete the corresponding GitHub release
141
+
if [ ! -z "$highest_version" ] && version_gt "$highest_version" "$current_version"; then
142
+
echo "Error: Version downgrade not allowed. Current: $current_version, Highest: $highest_version"
143
+
exit 1
144
+
fi
145
+
146
+
# Handle pre-release versions
147
+
if [[ "$current_version" == *"-"* ]]; then
148
+
echo "Pre-release version detected: $current_version"
149
+
150
+
# Extract base version and pre-release parts
151
+
base_version=${current_version%%-*}
152
+
pre_release=${current_version#*-}
153
+
pre_type=${pre_release%%.*}
154
+
pre_num=${pre_release#*.}
155
+
156
+
# Check if same pre-release version exists
157
+
if git ls-remote --tags origin | grep -q "refs/tags/chrome-extension-v$current_version"; then
0 commit comments