From 55a5cc3f1b08ed54239d3abff09980cd5fac505e Mon Sep 17 00:00:00 2001 From: cnphpbb Date: Sat, 15 Aug 2026 23:56:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(shell):=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E8=84=9A=E6=9C=AC=20npm/yarn=20=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E4=B8=8E=E5=8A=A0=E8=BD=BD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - npm/yarn 未安装时自动跳过配置 - 默认代理类型改为 socks5 - 简化脚本加载提示判断 --- shell/proxy.ps1 | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/shell/proxy.ps1 b/shell/proxy.ps1 index 2a4a017..51c3a1c 100644 --- a/shell/proxy.ps1 +++ b/shell/proxy.ps1 @@ -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 }