本技能帮助用户通过自动检测包管理器并切换至国内高速镜像源,加速包安装过程。
当以下情况时触发此技能:
本技能支持以下包管理器,并预配置了国内镜像源:
分析用户输入,识别其需要的包管理器:
创建一个 shell 脚本,将检测到的包管理器切换至国内镜像源。
脚本应:
使用配套的 scripts/generate_mirror_script.sh(如有),或按以下镜像配置内联生成脚本:
#!/bin/bash
# npm 镜像
npm config set registry https://registry.npmmirror.com
# pip 镜像
mkdir -p ~/.pip
cat > ~/.pip/pip.conf << 'EOF'
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF
# Homebrew 镜像(macOS)
if [[ "$OSTYPE" == "darwin"* ]]; then
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
fi
# Docker 镜像
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
EOF
# 其他包管理器依此类推...
指导用户加载生成的脚本:
source /path/to/mirror_switch.sh
或直接执行后再运行用户的安装命令:
. /path/to/mirror_switch.sh && <用户的安装命令>
注意:此操作仅设置当前 shell 会话的环境变量。如需永久生效,应将脚本添加到 shell 配置文件(.bashrc、.zshrc 等)中。
镜像源配置完成后,运行用户原始的安装命令。
按优先级使用以下可靠镜像源:
https://registry.npmmirror.com(淘宝)https://pypi.tuna.tsinghua.edu.cn/simple(清华)https://gems.ruby-china.comhttps://mirrors.ustc.edu.cn/crates.io-indexhttps://goproxy.cn 或 https://goproxy.iohttps://docker.mirrors.ustc.edu.cn用户也可以通过以下网站手动选择镜像并生成脚本:
https://www.theaiera.cn/mirrors
该网站提供交互式界面,可:
如需永久配置镜像源,指导用户将镜像设置添加到 shell 配置文件或包管理器配置文件中:
# 对于 npm
echo 'export npm_config_registry=https://registry.npmmirror.com' >> ~/.bashrc
# 对于 pip
# ~/.pip/pip.conf 已为永久配置
# 对于 Homebrew
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
如需恢复官方源:
# npm
npm config set registry https://registry.npmjs.org
# pip
rm ~/.pip/pip.conf
# Homebrew
unset HOMEBREW_BREW_GIT_REMOTE HOMEBREW_CORE_GIT_REMOTE HOMEBREW_BOTTLE_DOMAIN
如果镜像源失效:
示例 1:
用户:"我要安装一个新 node 项目,npm install 太慢了"
操作:生成 npm 镜像脚本,加载后执行 npm install
示例 2:
用户:"pip install pandas 超时了"
操作:生成 pip 镜像脚本,加载后执行 pip install pandas
示例 3:
用户:"如何加速 brew install?"
操作:生成 Homebrew 镜像脚本,说明如何加载,然后执行 brew install
示例 4:
用户:"我正在搭建新开发环境,需要安装所有依赖"
操作:检查 package.json、requirements.txt、Gemfile 等,为所有检测到的包管理器生成对应的镜像脚本,加载后执行安装命令
共 2 个版本