Files
common/npm/npm-test/configure-auth.sh
2026-03-12 18:34:44 -07:00

49 lines
1.4 KiB
Bash

#!/bin/bash
set -e
ACTION="$1"
AUTH_URLS="$2"
API_KEYS="$3"
if [ "$ACTION" == "add" ]; then
# Parse newline-separated URLs and tokens into arrays (preserves empty lines)
mapfile -t URLS <<< "$AUTH_URLS"
mapfile -t KEYS <<< "$API_KEYS"
# Verify arrays have matching lengths
if [ ${#URLS[@]} -ne ${#KEYS[@]} ]; then
echo "Error: Number of registries (${#URLS[@]}) does not match number of apiKeys (${#KEYS[@]})"
exit 1
fi
# Configure authentication for each registry
for i in "${!URLS[@]}"; do
URL="${URLS[$i]}"
KEY="${KEYS[$i]}"
# Skip empty registry entries
if [ -z "$URL" ]; then
continue
fi
# Skip registries that don't need authentication
if [ -z "$KEY" ] || [ "$KEY" == "none" ]; then
echo "Skipping authentication for: $URL (no API key provided)"
continue
fi
# Remove protocol from URL for npm config
REGISTRY_PATH=$(echo "$URL" | sed -E 's|https?://||')
echo "Configuring authentication for: $URL"
npm config set --location=project "//${REGISTRY_PATH}:_authToken" "$KEY"
done
elif [ "$ACTION" == "remove" ]; then
# Clean up project .npmrc
rm -f .npmrc
echo "Removed npm authentication configuration"
else
echo "Error: Invalid action '$ACTION'. Use 'add' or 'remove'"
exit 1
fi