Skip to content

Commit 39ed659

Browse files
chore: improve publish-npm script --latest tag logic
1 parent 69c73d1 commit 39ed659

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

bin/publish-npm

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,35 @@ set -eux
44

55
npm config set '//registry.npmjs.org/:_authToken' "$NPM_TOKEN"
66

7-
# Build the project
87
yarn build
9-
10-
# Navigate to the dist directory
118
cd dist
129

13-
# Get the version from package.json
10+
# Get latest version from npm
11+
#
12+
# If the package doesn't exist, yarn will return
13+
# {"type":"error","data":"Received invalid response from npm."}
14+
# where .data.version doesn't exist so LAST_VERSION will be an empty string.
15+
LAST_VERSION="$(yarn info --json 2> /dev/null | jq -r '.data.version')"
16+
17+
# Get current version from package.json
1418
VERSION="$(node -p "require('./package.json').version")"
1519

16-
# Extract the pre-release tag if it exists
20+
# Check if current version is pre-release (e.g. alpha / beta / rc)
21+
CURRENT_IS_PRERELEASE=false
1722
if [[ "$VERSION" =~ -([a-zA-Z]+) ]]; then
18-
# Extract the part before any dot in the pre-release identifier
19-
TAG="${BASH_REMATCH[1]}"
23+
CURRENT_IS_PRERELEASE=true
24+
CURRENT_TAG="${BASH_REMATCH[1]}"
25+
fi
26+
27+
# Check if last version is a stable release
28+
LAST_IS_STABLE_RELEASE=true
29+
if [[ -z "$LAST_VERSION" || "$LAST_VERSION" =~ -([a-zA-Z]+) ]]; then
30+
LAST_IS_STABLE_RELEASE=false
31+
fi
32+
33+
# Use a corresponding alpha/beta tag if there already is a stable release and we're publishing a prerelease.
34+
if $CURRENT_IS_PRERELEASE && $LAST_IS_STABLE_RELEASE; then
35+
TAG="$CURRENT_TAG"
2036
else
2137
TAG="latest"
2238
fi

0 commit comments

Comments
 (0)