cloudflare/cloudflare-typescript
Publicmirrored fromhttps://github.com/cloudflare/cloudflare-typescriptAvailable
bin/publish-npm
60lines · modecode
| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -eux |
| 4 | |
| 5 | yarn build |
| 6 | cd dist |
| 7 | |
| 8 | # Get package name and version from package.json |
| 9 | PACKAGE_NAME="$(jq -r -e '.name' ./package.json)" |
| 10 | VERSION="$(jq -r -e '.version' ./package.json)" |
| 11 | |
| 12 | # Get latest version from npm |
| 13 | # |
| 14 | # If the package doesn't exist, npm will return: |
| 15 | # { |
| 16 | # "error": { |
| 17 | # "code": "E404", |
| 18 | # "summary": "Unpublished on 2025-06-05T09:54:53.528Z", |
| 19 | # "detail": "'the_package' is not in this registry..." |
| 20 | # } |
| 21 | # } |
| 22 | NPM_INFO="$(npm view "$PACKAGE_NAME" version --json 2>/dev/null || true)" |
| 23 | |
| 24 | # Check if we got an E404 error |
| 25 | if echo "$NPM_INFO" | jq -e '.error.code == "E404"' > /dev/null 2>&1; then |
| 26 | # Package doesn't exist yet, no last version |
| 27 | LAST_VERSION="" |
| 28 | elif echo "$NPM_INFO" | jq -e '.error' > /dev/null 2>&1; then |
| 29 | # Report other errors |
| 30 | echo "ERROR: npm returned unexpected data:" |
| 31 | echo "$NPM_INFO" |
| 32 | exit 1 |
| 33 | else |
| 34 | # Success - get the version |
| 35 | LAST_VERSION=$(echo "$NPM_INFO" | jq -r '.') # strip quotes |
| 36 | fi |
| 37 | |
| 38 | # Check if current version is pre-release (e.g. alpha / beta / rc) |
| 39 | CURRENT_IS_PRERELEASE=false |
| 40 | if [[ "$VERSION" =~ -([a-zA-Z]+) ]]; then |
| 41 | CURRENT_IS_PRERELEASE=true |
| 42 | CURRENT_TAG="${BASH_REMATCH[1]}" |
| 43 | fi |
| 44 | |
| 45 | # Check if last version is a stable release |
| 46 | LAST_IS_STABLE_RELEASE=true |
| 47 | if [[ -z "$LAST_VERSION" || "$LAST_VERSION" =~ -([a-zA-Z]+) ]]; then |
| 48 | LAST_IS_STABLE_RELEASE=false |
| 49 | fi |
| 50 | |
| 51 | # Use a corresponding alpha/beta tag if there already is a stable release and we're publishing a prerelease. |
| 52 | if $CURRENT_IS_PRERELEASE && $LAST_IS_STABLE_RELEASE; then |
| 53 | TAG="$CURRENT_TAG" |
| 54 | else |
| 55 | TAG="latest" |
| 56 | fi |
| 57 | |
| 58 | # Publish with the appropriate tag |
| 59 | unset NODE_AUTH_TOKEN |
| 60 | npm publish --provenance --tag "$TAG" |