Skip to content

Commit be3283b

Browse files
fix: publish script — handle NPM errors correctly
1 parent 94014af commit be3283b

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

bin/publish-npm

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,35 @@ npm config set '//registry.npmjs.org/:_authToken' "$NPM_TOKEN"
77
yarn build
88
cd dist
99

10+
# Get package name and version from package.json
11+
PACKAGE_NAME="$(jq -r -e '.name' ./package.json)"
12+
VERSION="$(jq -r -e '.version' ./package.json)"
13+
1014
# Get latest version from npm
1115
#
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
18-
VERSION="$(node -p "require('./package.json').version")"
16+
# If the package doesn't exist, npm will return:
17+
# {
18+
# "error": {
19+
# "code": "E404",
20+
# "summary": "Unpublished on 2025-06-05T09:54:53.528Z",
21+
# "detail": "'the_package' is not in this registry..."
22+
# }
23+
# }
24+
NPM_INFO="$(npm view "$PACKAGE_NAME" version --json 2>/dev/null || true)"
25+
26+
# Check if we got an E404 error
27+
if echo "$NPM_INFO" | jq -e '.error.code == "E404"' > /dev/null 2>&1; then
28+
# Package doesn't exist yet, no last version
29+
LAST_VERSION=""
30+
elif echo "$NPM_INFO" | jq -e '.error' > /dev/null 2>&1; then
31+
# Report other errors
32+
echo "ERROR: npm returned unexpected data:"
33+
echo "$NPM_INFO"
34+
exit 1
35+
else
36+
# Success - get the version
37+
LAST_VERSION=$(echo "$NPM_INFO" | jq -r '.') # strip quotes
38+
fi
1939

2040
# Check if current version is pre-release (e.g. alpha / beta / rc)
2141
CURRENT_IS_PRERELEASE=false

0 commit comments

Comments
 (0)