fix(shell): 优化代理脚本 npm/yarn 检测与加载逻辑

- npm/yarn 未安装时自动跳过配置
- 默认代理类型改为 socks5
- 简化脚本加载提示判断
This commit is contained in:
cnphpbb
2026-08-15 23:56:10 +08:00
parent 5cc6941588
commit 55a5cc3f1b
+22 -21
View File
@@ -19,7 +19,7 @@
# -------------------- 默认配置 --------------------
if (-not $env:_PROXY_HOST) { $env:_PROXY_HOST = "127.0.0.1" }
if (-not $env:_PROXY_PORT) { $env:_PROXY_PORT = "7890" }
if (-not $env:_PROXY_TYPE) { $env:_PROXY_TYPE = "http" } # http | socks5 | socks4
if (-not $env:_PROXY_TYPE) { $env:_PROXY_TYPE = "socks5" } # http | socks5 | socks4
$global:NO_PROXY_LIST = "localhost,127.0.0.1,::1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
@@ -93,13 +93,17 @@ function Proxy-On {
git config --global http.proxy $url 2>$null
git config --global https.proxy $url 2>$null
# --- NPM ---
npm config set proxy $url 2>$null
npm config set https-proxy $url 2>$null
# --- NPM (未安装则跳过) ---
if (Get-Command npm -ErrorAction SilentlyContinue) {
npm config set proxy $url 2>$null
npm config set https-proxy $url 2>$null
}
# --- Yarn ---
yarn config set proxy $url 2>$null
yarn config set https-proxy $url 2>$null
# --- Yarn (未安装则跳过) ---
if (Get-Command yarn -ErrorAction SilentlyContinue) {
yarn config set proxy $url 2>$null
yarn config set https-proxy $url 2>$null
}
# --- Windows 系统 WinINET (IE/Edge/Chrome 等) ---
_SetWinInetProxy $url
@@ -128,13 +132,17 @@ function Proxy-Off {
git config --global --unset http.proxy 2>$null
git config --global --unset https.proxy 2>$null
# --- NPM ---
npm config delete proxy 2>$null
npm config delete https-proxy 2>$null
# --- NPM (未安装则跳过) ---
if (Get-Command npm -ErrorAction SilentlyContinue) {
npm config delete proxy 2>$null
npm config delete https-proxy 2>$null
}
# --- Yarn ---
yarn config delete proxy 2>$null
yarn config delete https-proxy 2>$null
# --- Yarn (未安装则跳过) ---
if (Get-Command yarn -ErrorAction SilentlyContinue) {
yarn config delete proxy 2>$null
yarn config delete https-proxy 2>$null
}
# --- Windows 系统 WinINET ---
_SetWinInetProxy $null
@@ -341,13 +349,6 @@ function proxy {
}
# -------------------- 加载提示 --------------------
if ($MyInvocation.Line -notmatch '\$\s*\(?\s*\.\s+.*proxy\.ps1') {
# 被直接执行而非 dot-source 时
if ($args.Count -eq 0) {
# 无参数直接执行,先 dot-source 自己
Write-Host "[proxy.ps1] 请使用 '. .\proxy.ps1' 来加载脚本。" -ForegroundColor Yellow
Write-Host "[proxy.ps1] 如果要直接执行命令, 请运行: .\proxy.ps1 on" -ForegroundColor Yellow
}
} else {
if ($MyInvocation.InvocationName -eq '.' -or $MyInvocation.Line -match '^\s*\.\s+.*proxy\.ps1') {
Write-Host "[proxy.ps1] 已加载。使用 'proxy help' 查看用法。" -ForegroundColor Cyan
}