11#! /usr/bin/env bash
2- # Cuts a release of one package from main: bumps its package.json, pushes
3- # that commit, then pushes the <package>-v<version> tag that release.yml publishes.
2+ # Cuts a release of one package from main: sets the version in its
3+ # package.json, pushes that commit, then pushes the <package>-v<version> tag
4+ # that release.yml publishes.
5+ #
6+ # A release candidate (1.3.0-rc1) is published under the npm dist-tag `rc`, so
7+ # `npm install <package>` and version ranges keep resolving to the last
8+ # production release. From a production version name the bump; from a release
9+ # candidate, cut the next candidate or promote it.
410set -euo pipefail
511
12+ usage () {
13+ cat >&2 << 'USAGE '
14+ usage: pnpm run release <vitest|mocha|jest> <major|minor|patch> [--rc]
15+ pnpm run release <vitest|mocha|jest> <rc|promote>
16+
17+ from a production version (1.2.3):
18+ patch | minor | major 1.2.4 | 1.3.0 | 2.0.0
19+ patch | minor | major --rc 1.2.4-rc1 | 1.3.0-rc1 | 2.0.0-rc1
20+ from a release candidate (1.3.0-rc1):
21+ rc 1.3.0-rc2
22+ promote 1.3.0
23+ USAGE
24+ exit 2
25+ }
26+
627package=${1:- }
7- bump=${2:- }
28+ action=${2:- }
29+ flag=${3:- }
830case " $package " in
931 vitest | mocha | jest) ;;
10- * ) package= ;;
32+ * ) usage ;;
1133esac
12- case " $bump " in
13- major | minor | patch) ;;
14- * ) bump= ;;
34+ case " $action " in
35+ major | minor | patch) [ -z " $flag " ] || [ " $flag " = --rc ] || usage ;;
36+ rc | promote) [ -z " $flag " ] || usage ;;
37+ * ) usage ;;
1538esac
16- if [ -z " $package " ] || [ -z " $bump " ]; then
17- echo " usage: pnpm run release <vitest|mocha|jest> <major|minor|patch>" >&2
18- exit 2
19- fi
39+ [ $# -le 3 ] || usage
2040
2141cd " $( git rev-parse --show-toplevel) "
2242manifest=" packages/$package /package.json"
2343
44+ current=$( node -p " require('./$manifest ').version" )
45+ if [[ $current =~ ^([0-9]+)\. ([0-9]+)\. ([0-9]+)(-rc([0-9]+))? $ ]]; then
46+ major=${BASH_REMATCH[1]}
47+ minor=${BASH_REMATCH[2]}
48+ patch=${BASH_REMATCH[3]}
49+ candidate=${BASH_REMATCH[5]}
50+ else
51+ echo " $manifest has version $current , which is neither x.y.z nor x.y.z-rcN" >&2
52+ exit 1
53+ fi
54+
55+ if [ -n " $candidate " ]; then
56+ case " $action " in
57+ rc) next=" $major .$minor .$patch -rc$(( candidate + 1 )) " ;;
58+ promote) next=" $major .$minor .$patch " ;;
59+ * )
60+ echo " $package is at release candidate $current : use rc for the next candidate or promote to release $major .$minor .$patch " >&2
61+ exit 1
62+ ;;
63+ esac
64+ else
65+ case " $action " in
66+ major) next=" $(( major + 1 )) .0.0" ;;
67+ minor) next=" $major .$(( minor + 1 )) .0" ;;
68+ patch) next=" $major .$minor .$(( patch + 1 )) " ;;
69+ * )
70+ echo " $package is at $current , not a release candidate: name the bump (major, minor or patch), with --rc for a candidate" >&2
71+ exit 1
72+ ;;
73+ esac
74+ [ " $flag " != --rc ] || next=" $next -rc1"
75+ fi
76+
2477branch=$( git rev-parse --abbrev-ref HEAD)
2578if [ " $branch " != main ]; then
2679 echo " releases are cut from main, not from $branch " >&2
@@ -39,10 +92,10 @@ if [ "$(git rev-parse HEAD)" != "$(git rev-parse origin/main)" ]; then
3992fi
4093
4194# The workspace lockfile does not record a workspace package's own version.
42- version= $ ( cd " packages/$package " && npm version " $bump " --no-git-tag-version)
43- tag=" $package -$version "
95+ (cd " packages/$package " && npm version " $next " --no-git-tag-version > /dev/null )
96+ tag=" $package -v $next "
4497git commit --quiet -m " release: $tag " -- " $manifest "
4598git tag " $tag "
4699git push origin main
47100git push origin " $tag "
48- echo " pushed $tag ; release.yml stages it on npm"
101+ echo " pushed $tag ( $current -> $next ) ; release.yml stages it on npm"
0 commit comments