Files
common/npm/npm-install/download_package.sh
2026-01-01 13:44:23 -08:00

66 lines
1.5 KiB
Bash

#!/bin/bash
set -e
PACKAGE_NAME="$1"
VERSION="$2"
REGISTRY="$3"
AUTH_TOKEN="$4"
OUTPUT_DIR="${5:-.}"
if [[ ! -f "$GITHUB_OUTPUT" ]]; then
GITHUB_OUTPUT="/dev/fd/1"
fi
# Ensure version is provided
if [[ -z "$VERSION" ]]; then
echo "Error: VERSION is required"
exit 1
fi
# Download the tarball
TARBALL_URL="$REGISTRY/$PACKAGE_NAME/-/$PACKAGE_NAME-$VERSION.tgz"
TGZ_NAME="$PACKAGE_NAME-$VERSION.tgz"
mkdir -p "$OUTPUT_DIR"
TGZ_PATH="$OUTPUT_DIR/$TGZ_NAME"
echo "Downloading $TARBALL_URL"
CURL_HEADERS=()
if [[ -n "$AUTH_TOKEN" ]]; then
CURL_HEADERS+=(-H "Authorization: Bearer $AUTH_TOKEN")
fi
# Download and capture HTTP status separately
# Temporarily disable 'exit on error' to capture status
set +e
HTTP_CODE=$(curl -sL -w "%{http_code}" -o "$TGZ_PATH" "${CURL_HEADERS[@]}" "$TARBALL_URL")
CURL_EXIT=$?
set -e
# Check if curl command itself failed (network issues, DNS, etc)
if [[ $CURL_EXIT -ne 0 ]]; then
echo "Error: curl failed with exit code $CURL_EXIT (network error or invalid URL)"
rm -f "$TGZ_PATH"
exit 1
fi
# Check HTTP status code
if [[ "$HTTP_CODE" != "200" ]]; then
echo "Error: Failed to download package (HTTP $HTTP_CODE)"
if [[ -f "$TGZ_PATH" ]]; then
echo "Response preview:"
head -c 500 "$TGZ_PATH"
echo ""
rm -f "$TGZ_PATH"
fi
exit 1
fi
if [[ ! -f "$TGZ_PATH" ]]; then
echo "Error: Download failed - file not created"
exit 1
fi
echo "tgz=$TGZ_PATH" >> "$GITHUB_OUTPUT"
echo "Downloaded $TGZ_NAME"