#!/usr/bin/env bash
set -euo pipefail
BUMP="${1:-patch}"
if [[ "$BUMP" != "patch" && "$BUMP" != "minor" && "$BUMP" != "major" ]]; then
echo "Usage: ./release.sh [patch|minor|major]"
exit 1
fi
cd "$(dirname "$0")"
echo "🔍 Pre-flight checks..."
if [[ -n "$(git status --porcelain)" ]]; then
echo "❌ Working tree is dirty. Commit or stash changes first."
exit 1
fi
BRANCH=$(git branch --show-current)
if [[ "$BRANCH" != "master" ]]; then
echo "❌ Not on master (on $BRANCH). Switch to master first."
exit 1
fi
git fetch origin master --quiet
LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse origin/master)
if [[ "$LOCAL" != "$REMOTE" ]]; then
echo "❌ Local master ($LOCAL) differs from origin ($REMOTE). Pull/push first."
exit 1
fi
echo ""
echo "🧪 Running tests..."
JEST_OUTPUT=$(NODE_OPTIONS='--experimental-vm-modules' npx jest --runInBand --forceExit --testPathPatterns='tests/unit' 2>&1)
echo "$JEST_OUTPUT" | tail -5
if echo "$JEST_OUTPUT" | grep -q 'Tests:.*failed'; then
echo "❌ Tests failed"
exit 1
fi
echo ""
CURRENT=$(node -p "require('./package.json').version")
echo "📦 Current version: $CURRENT"
echo "📦 Bumping: $BUMP"
echo ""
npm version "$BUMP" --message "v%s"
NEW_VERSION=$(node -p "require('./package.json').version")
echo ""
echo "📦 New version: $NEW_VERSION"
echo ""
echo "📤 Pushing commit and tag (CI will publish to npm)..."
git push origin master --follow-tags
echo ""
echo "✅ Release v${NEW_VERSION} triggered"
echo " CI will publish @askjo/camofox-browser@${NEW_VERSION} with provenance"
echo " Watch: https://github.com/jo-inc/camofox-browser/actions"
echo " Package: https://www.npmjs.com/package/@askjo/camofox-browser"