|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Release script for jquery-micro-utils |
| 6 | +# - Creates a GitHub release for the current version |
| 7 | +# - Bumps patch version in package.json |
| 8 | +# - Syncs runtime version in src/jquery-micro-utils.js |
| 9 | +# - Commits and pushes the bump |
| 10 | + |
| 11 | +REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 12 | +cd "$REPO_ROOT" |
| 13 | + |
| 14 | +if ! command -v gh >/dev/null 2>&1; then |
| 15 | + echo "Error: gh (GitHub CLI) is required." >&2 |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +if ! command -v node >/dev/null 2>&1; then |
| 20 | + echo "Error: node is required." >&2 |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +if ! command -v npm >/dev/null 2>&1; then |
| 25 | + echo "Error: npm is required." >&2 |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then |
| 30 | + echo "Error: not inside a git repository." >&2 |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +# Ensure working tree is clean to avoid accidental releases of uncommitted code |
| 35 | +if ! git diff-index --quiet HEAD --; then |
| 36 | + echo "Error: working tree has uncommitted changes. Please commit or stash before releasing." >&2 |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +BRANCH="$(git rev-parse --abbrev-ref HEAD)" |
| 41 | +HEAD_SHA="$(git rev-parse HEAD)" |
| 42 | + |
| 43 | +PKG_JSON="package.json" |
| 44 | +SRC_FILE="src/jquery-micro-utils.js" |
| 45 | + |
| 46 | +if [[ ! -f "$PKG_JSON" ]]; then |
| 47 | + echo "Error: $PKG_JSON not found." >&2 |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +# Read version from package.json; initialize if missing |
| 52 | +CURRENT_VERSION="$(node -e "try{const v=require('./package.json').version||'';process.stdout.write(String(v||''))}catch(e){process.stdout.write('')}")" |
| 53 | + |
| 54 | +init_if_missing() { |
| 55 | + local init_ver="$1" |
| 56 | + echo "Initializing package version to $init_ver" |
| 57 | + npm version "$init_ver" --no-git-tag-version >/dev/null |
| 58 | + |
| 59 | + # Sync runtime version in source file if present |
| 60 | + if [[ -f "$SRC_FILE" ]]; then |
| 61 | + node - <<'NODE' "$init_ver" "$SRC_FILE" |
| 62 | +const fs = require('fs'); |
| 63 | +const path = process.argv[3]; |
| 64 | +const ver = process.argv[2]; |
| 65 | +let s = fs.readFileSync(path, 'utf8'); |
| 66 | +s = s.replace(/(jQuery\s+Micro\s+Utils\s+v)(\d+\.\d+\.\d+)/, `$1${ver}`); |
| 67 | +s = s.replace(/(\$\.microUtils\s*=\s*\{\s*version:\s*['"])([^'"]+)(['"]\s*\})/, `$1${ver}$3`); |
| 68 | +fs.writeFileSync(path, s); |
| 69 | +NODE |
| 70 | + git add "$PKG_JSON" "$SRC_FILE" |
| 71 | + else |
| 72 | + git add "$PKG_JSON" |
| 73 | + fi |
| 74 | + git commit -m "chore(version): initialize to v$init_ver" >/dev/null |
| 75 | +} |
| 76 | + |
| 77 | +if [[ -z "$CURRENT_VERSION" ]]; then |
| 78 | + # Default initial version agreed: 0.1.0 |
| 79 | + init_if_missing "0.1.0" |
| 80 | + CURRENT_VERSION="0.1.0" |
| 81 | + # Update HEAD SHA after the init commit |
| 82 | + HEAD_SHA="$(git rev-parse HEAD)" |
| 83 | +fi |
| 84 | + |
| 85 | +TAG="v$CURRENT_VERSION" |
| 86 | + |
| 87 | +# Ensure the tag doesn't already exist remotely as a release |
| 88 | +if gh release view "$TAG" >/dev/null 2>&1; then |
| 89 | + echo "Error: release $TAG already exists on GitHub." >&2 |
| 90 | + exit 1 |
| 91 | +fi |
| 92 | + |
| 93 | +echo "Creating GitHub release $TAG from $BRANCH@$HEAD_SHA" |
| 94 | +gh release create "$TAG" \ |
| 95 | + --title "$TAG" \ |
| 96 | + --generate-notes \ |
| 97 | + --target "$HEAD_SHA" |
| 98 | + |
| 99 | +# Bump patch version in package.json without creating a git tag |
| 100 | +echo "Bumping patch version" |
| 101 | +npm version patch --no-git-tag-version >/dev/null |
| 102 | + |
| 103 | +NEW_VERSION="$(node -p "require('./package.json').version")" |
| 104 | + |
| 105 | +# Sync runtime version in source file |
| 106 | +if [[ -f "$SRC_FILE" ]]; then |
| 107 | + node - <<'NODE' "$NEW_VERSION" "$SRC_FILE" |
| 108 | +const fs = require('fs'); |
| 109 | +const path = process.argv[3]; |
| 110 | +const ver = process.argv[2]; |
| 111 | +let s = fs.readFileSync(path, 'utf8'); |
| 112 | +s = s.replace(/(jQuery\s+Micro\s+Utils\s+v)(\d+\.\d+\.\d+)/, `$1${ver}`); |
| 113 | +s = s.replace(/(\$\.microUtils\s*=\s*\{\s*version:\s*['"])([^'"]+)(['"]\s*\})/, `$1${ver}$3`); |
| 114 | +fs.writeFileSync(path, s); |
| 115 | +NODE |
| 116 | + git add "$PKG_JSON" "$SRC_FILE" |
| 117 | +else |
| 118 | + git add "$PKG_JSON" |
| 119 | +fi |
| 120 | + |
| 121 | +git commit -m "chore(version): bump to v$NEW_VERSION" >/dev/null |
| 122 | + |
| 123 | +echo "Pushing changes to origin/$BRANCH" |
| 124 | +git push origin "$BRANCH" |
| 125 | + |
| 126 | +echo "Done. Released $TAG and bumped to v$NEW_VERSION" |
| 127 | + |
0 commit comments