refactor(scripts): simplify switch-network to env-var based approach
Single env var NETWORK=corp|home controls all proxy config. Source from ~/.zshrc — sets http_proxy, NPM_CONFIG_REGISTRY, PIP_TRUSTED_HOST, NODE_TLS_REJECT_UNAUTHORIZED automatically. No more ~/.npmrc rewriting.
This commit is contained in:
parent
97b6f4b8d1
commit
2c588c51a3
@ -1,81 +1,52 @@
|
||||
#!/bin/bash
|
||||
# Toggle npm/pnpm between corporate proxy and direct (home) network.
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
# Dual-network development setup
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
#
|
||||
# Usage:
|
||||
# source scripts/switch-network.sh corp # AT&T / corporate proxy
|
||||
# source scripts/switch-network.sh home # Direct internet (home/VPN off)
|
||||
# source scripts/switch-network.sh status # Show current config
|
||||
# Controls: one env var — NETWORK=corp or NETWORK=home
|
||||
#
|
||||
# This modifies ~/.npmrc. Must be sourced (not executed) to affect current shell.
|
||||
# Usage (add to ~/.zshrc):
|
||||
# export NETWORK=corp # at work
|
||||
# export NETWORK=home # at home (or just unset it)
|
||||
#
|
||||
# Then source this file from ~/.zshrc:
|
||||
# source "$HOME/code/mygh/learning_ai_common_plat/scripts/switch-network.sh"
|
||||
#
|
||||
# What it sets:
|
||||
# NETWORK=corp → http_proxy, https_proxy, npm/pnpm registry, pip trusted-host
|
||||
# NETWORK=home → all proxy vars unset, default registries
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
|
||||
set -euo pipefail
|
||||
_CORP_PROXY="http://cso.proxy.att.com:8080/"
|
||||
_CORP_NPM_REGISTRY="https://jfrog-pkg-proxy.it.att.com/artifactory/api/npm/att-npm-proxy-group/"
|
||||
|
||||
NPMRC="$HOME/.npmrc"
|
||||
CORP_PROXY="http://cso.proxy.att.com:8080/"
|
||||
CORP_REGISTRY="https://jfrog-pkg-proxy.it.att.com/artifactory/api/npm/att-npm-proxy-group/"
|
||||
HOME_REGISTRY="https://registry.npmjs.org/"
|
||||
if [ "${NETWORK:-home}" = "corp" ]; then
|
||||
# ── Corporate proxy ──
|
||||
export http_proxy="$_CORP_PROXY"
|
||||
export https_proxy="$_CORP_PROXY"
|
||||
export HTTP_PROXY="$_CORP_PROXY"
|
||||
export HTTPS_PROXY="$_CORP_PROXY"
|
||||
export NPM_CONFIG_REGISTRY="$_CORP_NPM_REGISTRY"
|
||||
export NPM_CONFIG_PROXY="$_CORP_PROXY"
|
||||
export NPM_CONFIG_HTTPS_PROXY="$_CORP_PROXY"
|
||||
export NPM_CONFIG_STRICT_SSL="false"
|
||||
export PIP_TRUSTED_HOST="pypi.org files.pythonhosted.org"
|
||||
export NODE_TLS_REJECT_UNAUTHORIZED="0"
|
||||
else
|
||||
# ── Home / direct internet ──
|
||||
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY 2>/dev/null
|
||||
unset NPM_CONFIG_REGISTRY NPM_CONFIG_PROXY NPM_CONFIG_HTTPS_PROXY 2>/dev/null
|
||||
unset NPM_CONFIG_STRICT_SSL NODE_TLS_REJECT_UNAUTHORIZED 2>/dev/null
|
||||
unset PIP_TRUSTED_HOST 2>/dev/null
|
||||
fi
|
||||
|
||||
show_status() {
|
||||
echo "=== Current npm config ==="
|
||||
echo " proxy: $(npm config get proxy 2>/dev/null || echo 'not set')"
|
||||
echo " https-proxy: $(npm config get https-proxy 2>/dev/null || echo 'not set')"
|
||||
echo " registry: $(npm config get registry 2>/dev/null || echo 'not set')"
|
||||
if command -v pnpm &>/dev/null; then
|
||||
echo " pnpm store: $(pnpm store path 2>/dev/null || echo 'unknown')"
|
||||
fi
|
||||
echo ""
|
||||
if [ -f "$NPMRC" ]; then
|
||||
echo "=== ~/.npmrc ==="
|
||||
cat "$NPMRC"
|
||||
# Quick status on new shell (only if interactive)
|
||||
if [[ $- == *i* ]]; then
|
||||
if [ "${NETWORK:-home}" = "corp" ]; then
|
||||
echo "🏢 NETWORK=corp — proxy active"
|
||||
else
|
||||
echo "No ~/.npmrc found"
|
||||
echo "🏠 NETWORK=home — direct internet"
|
||||
fi
|
||||
}
|
||||
fi
|
||||
|
||||
set_corp() {
|
||||
cat > "$NPMRC" <<EOF
|
||||
proxy=$CORP_PROXY
|
||||
https-proxy=$CORP_PROXY
|
||||
registry=$CORP_REGISTRY
|
||||
strict-ssl=false
|
||||
EOF
|
||||
echo "✅ Switched to CORPORATE network"
|
||||
echo " proxy: $CORP_PROXY"
|
||||
echo " registry: $CORP_REGISTRY"
|
||||
echo ""
|
||||
echo " Lock files will resolve through JFrog proxy."
|
||||
echo " Run 'npm install' / 'pnpm install' as normal."
|
||||
}
|
||||
|
||||
set_home() {
|
||||
cat > "$NPMRC" <<EOF
|
||||
registry=$HOME_REGISTRY
|
||||
EOF
|
||||
echo "✅ Switched to HOME network"
|
||||
echo " proxy: (none)"
|
||||
echo " registry: $HOME_REGISTRY"
|
||||
echo ""
|
||||
echo " Lock files will resolve from npmjs.org."
|
||||
echo " To regenerate clean lock files:"
|
||||
echo " rm package-lock.json && npm install"
|
||||
echo " rm pnpm-lock.yaml && pnpm install"
|
||||
}
|
||||
|
||||
case "${1:-status}" in
|
||||
corp|corporate|work)
|
||||
set_corp
|
||||
;;
|
||||
home|direct|off)
|
||||
set_home
|
||||
;;
|
||||
status|show)
|
||||
show_status
|
||||
;;
|
||||
*)
|
||||
echo "Usage: source scripts/switch-network.sh [corp|home|status]"
|
||||
echo ""
|
||||
echo " corp — Enable corporate proxy (AT&T JFrog)"
|
||||
echo " home — Direct internet (no proxy)"
|
||||
echo " status — Show current npm config"
|
||||
;;
|
||||
esac
|
||||
unset _CORP_PROXY _CORP_NPM_REGISTRY
|
||||
|
||||
Loading…
Reference in New Issue
Block a user