Files
AI-Testing/scripts/configure-mirrors.sh
2026-04-18 20:20:38 +08:00

64 lines
1.4 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
MODE="${1:-cn}"
NPM_CN_REGISTRY="https://registry.npmmirror.com"
PIP_CN_INDEX_URL="https://mirrors.aliyun.com/pypi/simple/"
PIP_CN_TRUSTED_HOST="mirrors.aliyun.com"
DOCKER_CN_REGISTRY_MIRROR="https://registry.cn-hangzhou.aliyuncs.com"
if [[ "${MODE}" != "cn" && "${MODE}" != "global" ]]; then
echo "用法: ./scripts/configure-mirrors.sh [cn|global]"
exit 1
fi
echo "==> 配置 npm"
if command -v npm >/dev/null 2>&1; then
if [[ "${MODE}" == "cn" ]]; then
npm config set registry "${NPM_CN_REGISTRY}"
else
npm config set registry "https://registry.npmjs.org"
fi
npm config get registry
else
echo "未检测到 npm跳过"
fi
echo "==> 配置 pip (用户级 ~/.pip/pip.conf)"
mkdir -p "${HOME}/.pip"
if [[ "${MODE}" == "cn" ]]; then
cat > "${HOME}/.pip/pip.conf" <<EOF
[global]
index-url = ${PIP_CN_INDEX_URL}
trusted-host = ${PIP_CN_TRUSTED_HOST}
timeout = 120
EOF
else
cat > "${HOME}/.pip/pip.conf" <<EOF
[global]
index-url = https://pypi.org/simple
timeout = 120
EOF
fi
echo "pip.conf 已更新: ${HOME}/.pip/pip.conf"
echo "==> Docker 镜像加速器"
if [[ "${MODE}" == "cn" ]]; then
cat <<EOF
请在 Docker Desktop -> Settings -> Docker Engine 中加入:
{
"registry-mirrors": [
"${DOCKER_CN_REGISTRY_MIRROR}"
]
}
EOF
else
cat <<EOF
如需恢复 Docker 默认源,请移除 Docker Engine 中的 "registry-mirrors" 配置后重启 Docker Desktop。
EOF
fi
echo "==> 完成 (${MODE})"