Files
common/npm/npm-query-versions/query_versions.sh

46 lines
1.3 KiB
Bash

#!/bin/bash
set -e
PACKAGE_NAME="$1"
REGISTRY="$2"
AUTH_TOKEN="$3"
PRERELEASE="$4"
if [[ ! -f "$GITHUB_OUTPUT" ]]; then
GITHUB_OUTPUT="/dev/fd/1"
fi
# Construct the registry URL
REGISTRY_URL="$REGISTRY"
if [[ "$REGISTRY" == ${{ github.server_url }}/api/packages/${{ github.repository_owner }}/nuget/index.json ]]; then
# GitHub Packages uses a specific URL format
REGISTRY_URL="$REGISTRY"
fi
# Build curl headers
CURL_HEADERS=(-H "Accept: application/vnd.npm.install-v1+json")
if [[ -n "$AUTH_TOKEN" ]]; then
CURL_HEADERS+=(-H "Authorization: Bearer $AUTH_TOKEN")
fi
# Query the package metadata
RESPONSE=$(curl -sL "${CURL_HEADERS[@]}" "$REGISTRY_URL/$PACKAGE_NAME" 2>/dev/null || echo "{}")
# Extract versions
if [[ "$PRERELEASE" == "true" ]]; then
# Get all versions
VERSIONS=$(echo "$RESPONSE" | jq -r '.versions | keys[]' 2>/dev/null || echo "")
else
# Filter out prerelease versions (those with - in them)
VERSIONS=$(echo "$RESPONSE" | jq -r '.versions | keys[] | select(contains("-") | not)' 2>/dev/null || echo "")
fi
# Output versions separated by newlines
if [[ -n "$VERSIONS" ]]; then
echo "versions<<EOF" >> "$GITHUB_OUTPUT"
echo "$VERSIONS" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
else
echo "versions=" >> "$GITHUB_OUTPUT"
fi