platform-tooling/scripts/dev-setup/configure-verdaccio-client.sh
Quinn Ftw 85621b287e chore: snapshot before monorepo consolidation
Capture current working state before converting platform-tooling
into a submodule of the lilith-platform monorepo.
2026-01-29 07:04:39 -08:00

274 lines
8.5 KiB
Bash
Executable file

#!/bin/bash
#
# Configure Workstation for Verdaccio NPM Cache
# Updates local ~/.npmrc to consume from Verdaccio while publishing to Forgejo
#
# Usage:
# ./configure-verdaccio-client.sh # Configure this workstation
# ./configure-verdaccio-client.sh --verify # Verify configuration
# ./configure-verdaccio-client.sh --revert # Revert to direct Forgejo access
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INFRA_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Load utilities
source "$SCRIPT_DIR/../lib/colors.sh"
source "$SCRIPT_DIR/../lib/logger.sh"
log_init "VERDACCIO-CLIENT"
NPMRC_FILE="$HOME/.npmrc"
NPMRC_BACKUP="$HOME/.npmrc.backup.$(date +%Y%m%d-%H%M%S)"
# Configuration
VERDACCIO_REGISTRY="http://npm.nasty.sh:4873/"
FORGE_REGISTRY="http://forge.nasty.sh/api/packages/lilith/npm/"
# ============================================================================
# Helper Functions
# ============================================================================
check_prerequisites() {
log_step "Checking prerequisites..."
# Check npm/pnpm installed
if ! command -v npm &>/dev/null; then
log_failure "npm not installed"
exit 1
fi
# Check FORGEJO_NPM_TOKEN exists
if [ -z "${FORGEJO_NPM_TOKEN:-}" ]; then
log_failure "FORGEJO_NPM_TOKEN environment variable not set"
log_info "Add to ~/.bashrc:"
echo -e "${COLOR_CYAN} export FORGEJO_NPM_TOKEN=<your-token>${COLOR_NC}"
exit 1
fi
log_success "Prerequisites OK"
}
backup_npmrc() {
if [ -f "$NPMRC_FILE" ]; then
log_step "Backing up existing .npmrc..."
cp "$NPMRC_FILE" "$NPMRC_BACKUP"
log_success "Backup created: $NPMRC_BACKUP"
else
log_info "No existing .npmrc found"
fi
}
configure_npmrc() {
log_section "Configuring ~/.npmrc"
backup_npmrc
log_step "Writing new .npmrc configuration..."
cat > "$NPMRC_FILE" <<'EOF'
# =============================================================================
# NPM Registry Configuration - Verdaccio Hybrid Cache
# =============================================================================
# Architecture:
# - Consumption: Verdaccio (npm.nasty.sh:4873)
# - @lilith/* packages: Proxied from forge.nasty.sh
# - Public packages: Cached from npmjs.org
# - Publishing: Forge (forge.nasty.sh) via publishConfig in package.json
# =============================================================================
# Default registry for consumption (Verdaccio cache)
# Note: publishConfig in package.json overrides this for publishing
@lilith:registry=http://npm.nasty.sh:4873/
# Authentication for Verdaccio (reuses Forgejo token)
//npm.nasty.sh:4873/:_authToken=${FORGEJO_NPM_TOKEN}
# Authentication for Forge (required for publishing @lilith/* packages)
//forge.nasty.sh/api/packages/lilith/npm/:_authToken=${FORGEJO_NPM_TOKEN}
# Standard npm settings
strict-ssl=true
legacy-peer-deps=false
EOF
log_success ".npmrc configured"
}
verify_connectivity() {
log_section "Verifying Connectivity"
# Check Verdaccio health
log_step "Checking Verdaccio connectivity..."
if curl -sf "$VERDACCIO_REGISTRY-/ping" &>/dev/null; then
log_success "Verdaccio is accessible"
else
log_failure "Cannot connect to Verdaccio at $VERDACCIO_REGISTRY"
log_info "Ensure:"
echo -e "${COLOR_CYAN} 1. VPN is connected${COLOR_NC}"
echo -e "${COLOR_CYAN} 2. /etc/hosts has: 10.0.0.11 npm.nasty.sh${COLOR_NC}"
echo -e "${COLOR_CYAN} 3. Verdaccio is deployed on black${COLOR_NC}"
return 1
fi
# Check Forge connectivity
log_step "Checking Forge connectivity..."
if curl -sf "$FORGE_REGISTRY" &>/dev/null; then
log_success "Forge is accessible"
else
log_warn "Cannot connect to Forge (may be expected if not on VPN)"
fi
}
test_package_install() {
log_section "Testing Package Installation"
local test_dir="/tmp/verdaccio-test-$$"
mkdir -p "$test_dir"
cd "$test_dir"
log_step "Initializing test project..."
npm init -y &>/dev/null
# Test public package installation
log_step "Testing public package (react)..."
if npm install react --loglevel=warn 2>&1 | grep -q "npm.nasty.sh"; then
log_success "Public packages use Verdaccio"
else
log_warn "Public packages may not be using Verdaccio"
fi
# Test @lilith/* package installation
log_step "Testing @lilith/* package..."
if npm install @lilith/ui-theme --loglevel=warn &>/dev/null; then
log_success "@lilith/* packages install from Verdaccio"
else
log_warn "@lilith/* package installation failed (may not exist or auth issue)"
fi
# Cleanup
cd - &>/dev/null
rm -rf "$test_dir"
}
verify_publishing() {
log_section "Verifying Publishing Configuration"
log_step "Checking npm whoami..."
# Check Verdaccio auth
if npm whoami --registry="$VERDACCIO_REGISTRY" &>/dev/null; then
log_success "Authenticated to Verdaccio"
else
log_warn "Not authenticated to Verdaccio (may need to login)"
log_info "Run: npm adduser --registry=$VERDACCIO_REGISTRY"
fi
# Check Forge auth
if npm whoami --registry="$FORGE_REGISTRY" &>/dev/null; then
log_success "Authenticated to Forge"
else
log_warn "Not authenticated to Forge"
fi
log_info "Publishing to Forge still works via publishConfig in package.json"
}
show_verification() {
log_section "Configuration Verification"
log_step "Current .npmrc settings:"
echo ""
grep -E "^(registry=|@lilith:registry=|//.*(authToken|_auth))" "$NPMRC_FILE" | \
sed 's/authToken=.*/authToken=***/' | \
while read -r line; do
echo -e " ${COLOR_CYAN}$line${COLOR_NC}"
done
echo ""
log_step "Environment variables:"
echo -e " ${COLOR_CYAN}FORGEJO_NPM_TOKEN:${COLOR_NC} ${FORGEJO_NPM_TOKEN:+***}"
echo ""
}
revert_configuration() {
log_section "Reverting Configuration"
# Find latest backup
local latest_backup=$(ls -t "$HOME"/.npmrc.backup.* 2>/dev/null | head -1)
if [ -n "$latest_backup" ]; then
log_step "Restoring from backup: $(basename "$latest_backup")"
cp "$latest_backup" "$NPMRC_FILE"
log_success "Configuration reverted"
else
log_warn "No backup found"
log_info "Manually restore .npmrc or reconfigure with:"
echo -e "${COLOR_CYAN} @lilith:registry=$FORGE_REGISTRY${COLOR_NC}"
fi
}
show_next_steps() {
log_section "Next Steps"
echo ""
echo -e "${COLOR_SUCCESS}✓ Workstation configured for Verdaccio${COLOR_NC}"
echo ""
echo -e " ${COLOR_BOLD}Consumption:${COLOR_NC}"
echo -e " npm install <package> ${COLOR_COMMENT}# Uses Verdaccio cache${COLOR_NC}"
echo ""
echo -e " ${COLOR_BOLD}Publishing:${COLOR_NC}"
echo -e " npm publish ${COLOR_COMMENT}# Uses Forge (via publishConfig)${COLOR_NC}"
echo ""
echo -e " ${COLOR_BOLD}Verification:${COLOR_NC}"
echo -e " npm install react ${COLOR_COMMENT}# Should cache from npmjs.org${COLOR_NC}"
echo -e " npm install @lilith/ui-theme ${COLOR_COMMENT}# Should proxy from Forge${COLOR_NC}"
echo ""
}
# ============================================================================
# Main
# ============================================================================
main() {
local action="${1:-configure}"
case "$action" in
--verify|-v)
log_banner "Verdaccio Client - Verification"
check_prerequisites
show_verification
verify_connectivity
test_package_install
verify_publishing
;;
--revert|-r)
log_banner "Verdaccio Client - Revert"
revert_configuration
;;
configure|--configure|-c|"")
log_banner "Verdaccio Client - Configure"
check_prerequisites
configure_npmrc
verify_connectivity
show_verification
show_next_steps
;;
--help|-h)
echo "Usage: $0 [action]"
echo ""
echo "Actions:"
echo " (default) Configure workstation for Verdaccio"
echo " --verify, -v Verify current configuration"
echo " --revert, -r Revert to previous configuration"
echo " --help, -h Show this help"
;;
*)
log_error "Unknown action: $action"
echo "Run '$0 --help' for usage"
exit 1
;;
esac
}
main "$@"