|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Get the current version from a package.json file |
| 4 | +get_version() { |
| 5 | + grep -m1 '"version":' "$1" | awk -F'"' '{print $4}' |
| 6 | +} |
| 7 | + |
| 8 | +ui_version=$(get_version "packages/ui-extensions/package.json") |
| 9 | +react_version=$(get_version "packages/ui-extensions-react/package.json") |
| 10 | + |
| 11 | +create_new_version() { |
| 12 | + branch_name=$(git rev-parse --abbrev-ref HEAD) |
| 13 | + # Split the branch name into year and month |
| 14 | + IFS='-' read -r year month <<<"$branch_name" |
| 15 | + |
| 16 | + # Remove leading zero from month |
| 17 | + month=${month#0} |
| 18 | + |
| 19 | + echo "$year.$month.0" |
| 20 | +} |
| 21 | + |
| 22 | +increment_patch() { |
| 23 | + echo "$1" | awk -F. '{$NF = $NF + 1;}1' OFS=. |
| 24 | +} |
| 25 | + |
| 26 | +# Determine the new versions |
| 27 | +if [ "$ui_version" = "0.0.0" ]; then |
| 28 | + new_version=$(create_new_version) |
| 29 | +else |
| 30 | + new_version=$(increment_patch "$ui_version") |
| 31 | +fi |
| 32 | +if [ "$react_version" = "0.0.0" ]; then |
| 33 | + new_version_react=$(create_new_version) |
| 34 | +else |
| 35 | + new_version_react=$(increment_patch "$react_version") |
| 36 | +fi |
| 37 | + |
| 38 | +update_version() { |
| 39 | + local package_file="$1" |
| 40 | + local changelog_file="$2" |
| 41 | + local version="$3" |
| 42 | + |
| 43 | + # Update package.json |
| 44 | + awk -v version="$version" ' |
| 45 | + /"version":/ { gsub(/"version": "[^"]*"/, "\"version\": \"" version "\"") } |
| 46 | + /"peerDependencies"/, /}/ { |
| 47 | + if (/"@shopify\/ui-extensions":/) { |
| 48 | + gsub(/"@shopify\/ui-extensions": "[^"]*"/, "\"@shopify/ui-extensions\": \"" version "\"") |
| 49 | + } |
| 50 | + } |
| 51 | + /"devDependencies"/, /}/ { |
| 52 | + if (/"@shopify\/ui-extensions":/) { |
| 53 | + gsub(/"@shopify\/ui-extensions": "[^"]*"/, "\"@shopify/ui-extensions\": \"" version "\"") |
| 54 | + } |
| 55 | + } |
| 56 | + { print } |
| 57 | + ' "$package_file" >"${package_file}.tmp" && mv "${package_file}.tmp" "$package_file" |
| 58 | + |
| 59 | + # Update CHANGELOG.md |
| 60 | + awk -v version="$version" ' |
| 61 | + /^## 0\.0\.0/ { sub(/^## 0\.0\.0/, "## " version); } |
| 62 | + { print } |
| 63 | + ' "$changelog_file" >"${changelog_file}.tmp" && mv "${changelog_file}.tmp" "$changelog_file" |
| 64 | +} |
| 65 | + |
| 66 | +# Update versions in all relevant files |
| 67 | +update_version "packages/ui-extensions/package.json" "packages/ui-extensions/CHANGELOG.md" "$new_version" |
| 68 | +update_version "packages/ui-extensions-react/package.json" "packages/ui-extensions-react/CHANGELOG.md" "$new_version_react" |
| 69 | + |
| 70 | +echo "Version updated to $new_version" |
0 commit comments