publish-npm 1.7 KB

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