|
| 1 | +#!/usr/bin/env bash |
| 2 | +# release.sh — cut a release entirely from your machine: bump, build, test, |
| 3 | +# publish to npm, push, and create the GitHub Release. |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# ./scripts/release.sh patch # 0.3.0 → 0.3.1 |
| 7 | +# ./scripts/release.sh minor # 0.3.0 → 0.4.0 |
| 8 | +# ./scripts/release.sh major # 0.3.0 → 1.0.0 |
| 9 | +# ./scripts/release.sh 1.2.3 # explicit version |
| 10 | +# |
| 11 | +# Prerequisites (one-time): `npm login` (publishing uses your local npm auth, |
| 12 | +# including a 2FA OTP prompt if enabled) and `gh auth login` (for the release). |
| 13 | + |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 17 | +cd "$ROOT" |
| 18 | + |
| 19 | +current="$(node -p "require('./package.json').version")" |
| 20 | + |
| 21 | +# ── Resolve the bump ──────────────────────────────────────────────────────── |
| 22 | +if [[ -z "${1:-}" ]]; then |
| 23 | + echo "Usage: ./scripts/release.sh <patch|minor|major|x.y.z>" >&2 |
| 24 | + echo "Current version: $current" >&2 |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +bump="$1" |
| 29 | +case "$bump" in |
| 30 | + patch|minor|major) ;; |
| 31 | + [0-9]*.[0-9]*.[0-9]*) ;; |
| 32 | + *) echo "Error: '$bump' must be patch, minor, major, or an explicit x.y.z" >&2; exit 1 ;; |
| 33 | +esac |
| 34 | + |
| 35 | +# ── Safety checks ─────────────────────────────────────────────────────────── |
| 36 | +branch="$(git rev-parse --abbrev-ref HEAD)" |
| 37 | +if [[ "$branch" != "main" ]]; then |
| 38 | + echo "Error: releases must be cut from 'main' (currently on '$branch')." >&2 |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +if [[ -n "$(git status --porcelain)" ]]; then |
| 43 | + echo "Error: working tree is not clean — commit or stash changes first." >&2 |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | +echo "Syncing with origin/main…" |
| 48 | +git fetch --quiet origin main |
| 49 | +if [[ "$(git rev-parse HEAD)" != "$(git rev-parse origin/main)" ]]; then |
| 50 | + echo "Error: local main is not in sync with origin/main — pull/push first." >&2 |
| 51 | + exit 1 |
| 52 | +fi |
| 53 | + |
| 54 | +if ! npm whoami >/dev/null 2>&1; then |
| 55 | + echo "Error: not logged in to npm — run 'npm login' first." >&2 |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +# ── Validate (before bumping, so a failure leaves no dangling version) ─────── |
| 60 | +echo "Running tests…" |
| 61 | +npm test |
| 62 | +echo "Building…" |
| 63 | +npm run build:all |
| 64 | + |
| 65 | +# ── Bump + tag (local only so far) ────────────────────────────────────────── |
| 66 | +# npm version updates package.json + package-lock.json, commits, and tags vX.Y.Z. |
| 67 | +newtag="$(npm version "$bump" -m "Release v%s")" # prints e.g. "v0.4.0" |
| 68 | +echo "Bumped $current → $newtag" |
| 69 | + |
| 70 | +# ── Publish to npm ────────────────────────────────────────────────────────── |
| 71 | +echo "Publishing to npm (enter your 2FA OTP if prompted)…" |
| 72 | +if ! npm publish; then |
| 73 | + echo "" >&2 |
| 74 | + echo "Error: npm publish failed. The version bump was committed and tagged locally," >&2 |
| 75 | + echo "but nothing was pushed. To undo and retry:" >&2 |
| 76 | + echo " git tag -d $newtag && git reset --hard HEAD~1" >&2 |
| 77 | + exit 1 |
| 78 | +fi |
| 79 | + |
| 80 | +# ── Push + GitHub Release ─────────────────────────────────────────────────── |
| 81 | +git push --follow-tags origin main |
| 82 | + |
| 83 | +repo="RaspberryPiFoundation/python-friendly-error-messages" |
| 84 | +if command -v gh >/dev/null 2>&1; then |
| 85 | + if ! gh release create "$newtag" --title "$newtag" --generate-notes; then |
| 86 | + echo "Warning: GitHub Release creation failed (npm publish + push already succeeded)." >&2 |
| 87 | + echo "Create it manually: gh release create $newtag --title $newtag --generate-notes" >&2 |
| 88 | + fi |
| 89 | +else |
| 90 | + echo "Warning: gh CLI not found — GitHub Release not created." >&2 |
| 91 | + echo "Install gh, then run: gh release create $newtag --title $newtag --generate-notes" >&2 |
| 92 | +fi |
| 93 | + |
| 94 | +echo "" |
| 95 | +echo "✓ Released $newtag" |
| 96 | +echo " npm: https://www.npmjs.com/package/@raspberrypifoundation/python-friendly-error-messages/v/${newtag#v}" |
| 97 | +echo " GitHub: https://github.com/$repo/releases/tag/$newtag" |
0 commit comments