forked from DevOps/deploy.stack
fix(shell): 优化代理脚本 npm/yarn 检测与加载逻辑
- npm/yarn 未安装时自动跳过配置 - 默认代理类型改为 socks5 - 简化脚本加载提示判断
This commit is contained in:
+22
-21
@@ -19,7 +19,7 @@
|
|||||||
# -------------------- 默认配置 --------------------
|
# -------------------- 默认配置 --------------------
|
||||||
if (-not $env:_PROXY_HOST) { $env:_PROXY_HOST = "127.0.0.1" }
|
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_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"
|
$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 http.proxy $url 2>$null
|
||||||
git config --global https.proxy $url 2>$null
|
git config --global https.proxy $url 2>$null
|
||||||
|
|
||||||
# --- NPM ---
|
# --- NPM (未安装则跳过) ---
|
||||||
npm config set proxy $url 2>$null
|
if (Get-Command npm -ErrorAction SilentlyContinue) {
|
||||||
npm config set https-proxy $url 2>$null
|
npm config set proxy $url 2>$null
|
||||||
|
npm config set https-proxy $url 2>$null
|
||||||
|
}
|
||||||
|
|
||||||
# --- Yarn ---
|
# --- Yarn (未安装则跳过) ---
|
||||||
yarn config set proxy $url 2>$null
|
if (Get-Command yarn -ErrorAction SilentlyContinue) {
|
||||||
yarn config set https-proxy $url 2>$null
|
yarn config set proxy $url 2>$null
|
||||||
|
yarn config set https-proxy $url 2>$null
|
||||||
|
}
|
||||||
|
|
||||||
# --- Windows 系统 WinINET (IE/Edge/Chrome 等) ---
|
# --- Windows 系统 WinINET (IE/Edge/Chrome 等) ---
|
||||||
_SetWinInetProxy $url
|
_SetWinInetProxy $url
|
||||||
@@ -128,13 +132,17 @@ function Proxy-Off {
|
|||||||
git config --global --unset http.proxy 2>$null
|
git config --global --unset http.proxy 2>$null
|
||||||
git config --global --unset https.proxy 2>$null
|
git config --global --unset https.proxy 2>$null
|
||||||
|
|
||||||
# --- NPM ---
|
# --- NPM (未安装则跳过) ---
|
||||||
npm config delete proxy 2>$null
|
if (Get-Command npm -ErrorAction SilentlyContinue) {
|
||||||
npm config delete https-proxy 2>$null
|
npm config delete proxy 2>$null
|
||||||
|
npm config delete https-proxy 2>$null
|
||||||
|
}
|
||||||
|
|
||||||
# --- Yarn ---
|
# --- Yarn (未安装则跳过) ---
|
||||||
yarn config delete proxy 2>$null
|
if (Get-Command yarn -ErrorAction SilentlyContinue) {
|
||||||
yarn config delete https-proxy 2>$null
|
yarn config delete proxy 2>$null
|
||||||
|
yarn config delete https-proxy 2>$null
|
||||||
|
}
|
||||||
|
|
||||||
# --- Windows 系统 WinINET ---
|
# --- Windows 系统 WinINET ---
|
||||||
_SetWinInetProxy $null
|
_SetWinInetProxy $null
|
||||||
@@ -341,13 +349,6 @@ function proxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# -------------------- 加载提示 --------------------
|
# -------------------- 加载提示 --------------------
|
||||||
if ($MyInvocation.Line -notmatch '\$\s*\(?\s*\.\s+.*proxy\.ps1') {
|
if ($MyInvocation.InvocationName -eq '.' -or $MyInvocation.Line -match '^\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 {
|
|
||||||
Write-Host "[proxy.ps1] 已加载。使用 'proxy help' 查看用法。" -ForegroundColor Cyan
|
Write-Host "[proxy.ps1] 已加载。使用 'proxy help' 查看用法。" -ForegroundColor Cyan
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user