|
| 1 | +#!/bin/bash |
| 2 | +# Generate a changeset file from conventional commits since the last tag. |
| 3 | +# Usage: ./.changeset/changeset-gen.sh <package> [bump] |
| 4 | +# |
| 5 | +# package: npm name, e.g. @wdio/elements |
| 6 | +# bump: major | minor | patch (inferred from commits if omitted) |
| 7 | +# |
| 8 | +# Commits are filtered to only those that touched the package's directory. |
| 9 | +# Inferred bumps: feat/feature → minor, BREAKING CHANGE → major, else patch. |
| 10 | +# Changelog body groups commits by type (Features / Fixes / Improvements). |
| 11 | +# chore, ci, test, build, and deps commits are excluded from the body. |
| 12 | + |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +PACKAGE="${1:-}" |
| 16 | +BUMP="${2:-}" |
| 17 | + |
| 18 | +if [ -z "$PACKAGE" ]; then |
| 19 | + echo "Usage: $0 <package> [bump]" |
| 20 | + echo " package: npm name, e.g. @wdio/elements" |
| 21 | + echo " bump: major | minor | patch (inferred from commits if omitted)" |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +# ---- Resolve package name to directory ---- |
| 26 | +PKG_DIR="" |
| 27 | +for d in packages/*/package.json; do |
| 28 | + if [ "$(jq -r '.name' "$d")" = "$PACKAGE" ]; then |
| 29 | + PKG_DIR="$(dirname "$d")" |
| 30 | + break |
| 31 | + fi |
| 32 | +done |
| 33 | + |
| 34 | +if [ -z "$PKG_DIR" ]; then |
| 35 | + echo "❌ Package '$PACKAGE' not found in packages/*/package.json" |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null) || { echo "No git tags found"; exit 1; } |
| 40 | +COMMITS=$(git log "$LAST_TAG"..HEAD --format="%s" --no-merges -- "$PKG_DIR") |
| 41 | + |
| 42 | +if [ -z "$COMMITS" ]; then |
| 43 | + echo "No commits touching $PKG_DIR since $LAST_TAG" |
| 44 | + exit 0 |
| 45 | +fi |
| 46 | + |
| 47 | +# ---- Infer bump type ---- |
| 48 | +if [ -z "$BUMP" ]; then |
| 49 | + if echo "$COMMITS" | grep -q "BREAKING CHANGE"; then |
| 50 | + BUMP="major" |
| 51 | + elif echo "$COMMITS" | grep -qiE "^(feat|feature)[(:]"; then |
| 52 | + BUMP="minor" |
| 53 | + else |
| 54 | + BUMP="patch" |
| 55 | + fi |
| 56 | +fi |
| 57 | + |
| 58 | +# ---- Group commits by type, stripping the conventional prefix ---- |
| 59 | +# Include: feat, fix, refactor, perf, revert |
| 60 | +# Exclude: chore, ci, test, build, docs, style, deps |
| 61 | + |
| 62 | +strip_prefix() { sed -E 's/^[a-z]+(\([^)]*\))?:\s*//i'; } |
| 63 | + |
| 64 | +FEATS=$(echo "$COMMITS" | grep -iE "^(feat|feature)[(:]" | strip_prefix || true) |
| 65 | +FIXES=$(echo "$COMMITS" | grep -iE "^(fix)[(:]" | strip_prefix || true) |
| 66 | +PERF=$(echo "$COMMITS" | grep -iE "^(perf|performance)[(:]" | strip_prefix || true) |
| 67 | +REFAC=$(echo "$COMMITS" | grep -iE "^(refactor)[(:]" | strip_prefix || true) |
| 68 | +REVERT=$(echo "$COMMITS" | grep -iE "^(revert)[(:]" | strip_prefix || true) |
| 69 | + |
| 70 | +# ---- Generate the changeset file ---- |
| 71 | +SLUG=$(date +%s)-$(($$ % 10000))-$(openssl rand -hex 2 2>/dev/null || echo "$RANDOM") |
| 72 | +OUT=".changeset/$SLUG.md" |
| 73 | + |
| 74 | +{ |
| 75 | + echo "---" |
| 76 | + echo "\"$PACKAGE\": $BUMP" |
| 77 | + echo "---" |
| 78 | + echo "" |
| 79 | + |
| 80 | + if [ -n "$FEATS" ]; then |
| 81 | + echo "### 🚀 Features" |
| 82 | + while IFS= read -r line; do [ -n "$line" ] && echo "- $line"; done <<< "$FEATS" |
| 83 | + echo "" |
| 84 | + fi |
| 85 | + |
| 86 | + if [ -n "$FIXES" ]; then |
| 87 | + echo "### 🐛 Fixes" |
| 88 | + while IFS= read -r line; do [ -n "$line" ] && echo "- $line"; done <<< "$FIXES" |
| 89 | + echo "" |
| 90 | + fi |
| 91 | + |
| 92 | + if [ -n "$PERF" ] || [ -n "$REFAC" ]; then |
| 93 | + echo "### ⚡ Improvements" |
| 94 | + while IFS= read -r line; do [ -n "$line" ] && echo "- $line"; done <<< "$PERF" |
| 95 | + while IFS= read -r line; do [ -n "$line" ] && echo "- $line"; done <<< "$REFAC" |
| 96 | + echo "" |
| 97 | + fi |
| 98 | + |
| 99 | + if [ -n "$REVERT" ]; then |
| 100 | + echo "### ↩ Reverts" |
| 101 | + while IFS= read -r line; do [ -n "$line" ] && echo "- $line"; done <<< "$REVERT" |
| 102 | + echo "" |
| 103 | + fi |
| 104 | + |
| 105 | +} > "$OUT" |
| 106 | + |
| 107 | +echo "✅ $OUT (bump: $BUMP, scope: $PKG_DIR)" |
| 108 | +echo "" |
| 109 | +echo "--- Preview ---" |
| 110 | +cat "$OUT" |
0 commit comments