cloudflare/cloudflare-typescript
Publicmirrored fromhttps://github.com/cloudflare/cloudflare-typescriptAvailable
bin/publish-npm
61lines · modecode
| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -eux |
| 4 | |
| 5 | npm config set '//registry.npmjs.org/:_authToken' "$NPM_TOKEN" |
| 6 | |
| 7 | yarn build |
| 8 | cd dist |
| 9 | |
| 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 | |
| 14 | # Get latest version from npm |
| 15 | # |
| 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 |
| 39 | |
| 40 | # Check if current version is pre-release (e.g. alpha / beta / rc) |
| 41 | CURRENT_IS_PRERELEASE=false |
| 42 | if [[ "$VERSION" =~ -([a-zA-Z]+) ]]; then |
| 43 | CURRENT_IS_PRERELEASE=true |
| 44 | CURRENT_TAG="${BASH_REMATCH[1]}" |
| 45 | fi |
| 46 | |
| 47 | # Check if last version is a stable release |
| 48 | LAST_IS_STABLE_RELEASE=true |
| 49 | if [[ -z "$LAST_VERSION" || "$LAST_VERSION" =~ -([a-zA-Z]+) ]]; then |
| 50 | LAST_IS_STABLE_RELEASE=false |
| 51 | fi |
| 52 | |
| 53 | # Use a corresponding alpha/beta tag if there already is a stable release and we're publishing a prerelease. |
| 54 | if $CURRENT_IS_PRERELEASE && $LAST_IS_STABLE_RELEASE; then |
| 55 | TAG="$CURRENT_TAG" |
| 56 | else |
| 57 | TAG="latest" |
| 58 | fi |
| 59 | |
| 60 | # Publish with the appropriate tag |
| 61 | yarn publish --tag "$TAG" |
| 62 | |