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