Skip to content

Commit d346653

Browse files
authored
Merge pull request #40 from sanshu-rom/patch-4
升级win10+脚本
2 parents 696d1b0 + feb4ad1 commit d346653

File tree

1 file changed

+84
-53
lines changed

1 file changed

+84
-53
lines changed

autorun.ps1

Lines changed: 84 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
#Requires -RunAsAdministrator
21
param(
32
[switch]$UseProxy,
43
[string]$ProxyHost = "127.0.0.1:7890",
54
[switch]$SkipConfirm
65
)
76
$ErrorActionPreference = "Stop"
87

8+
# 自动提权 & 切回脚本目录
9+
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
10+
Write-Host "当前非管理员权限,正在尝试以管理员身份重新启动..." -ForegroundColor Yellow
11+
$arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`""
12+
$params = $MyInvocation.UnboundArguments
13+
foreach ($p in $params) { $arguments += " `"$p`"" }
14+
Start-Process powershell -ArgumentList $arguments -Verb RunAs
15+
exit
16+
}
17+
18+
# ----------- 切回脚本目录 -----------
19+
Set-Location -LiteralPath $PSScriptRoot
20+
921
# ---------- 代理开关 ----------
1022
function Use-ProxyIfNeeded {
1123
param([scriptblock]$Script)
@@ -22,62 +34,76 @@ function Use-ProxyIfNeeded {
2234
} else { & $Script }
2335
}
2436

25-
# ---------- 工具检测 ----------
2637
function Test-Cmd { param($cmd); $null -ne (Get-Command $cmd -ErrorAction SilentlyContinue) }
38+
function Invoke-WebRequestWithProxy([string]$Url, [string]$OutFile) {
39+
if ($UseProxy) {
40+
Invoke-WebRequest $Url -OutFile $OutFile -Proxy "http://$ProxyHost"
41+
} else {
42+
Invoke-WebRequest $Url -OutFile $OutFile
43+
}
44+
}
2745

2846
# ---------- 用户确认 ----------
2947
if (-not $SkipConfirm) {
30-
Write-Host "警告:此脚本仅适用于 Windows 10/11 64 位"
31-
Write-Host "建议使用Windows Terminal终端"
32-
Write-Host "默认执行命令.\drpys.ps1"
33-
Write-Host "下载失败可以指定旁路由代理执行命令.\drpys.ps1 -UseProxy -ProxyHost "旁路由/clash:7890""
34-
Write-Host "警告:此脚本仅适用于 Windows 10/11 64 位"
48+
Write-Host "警告:此脚本仅适用于 Windows 10/11 64 位" -ForegroundColor Green
49+
Write-Host "建议使用 Windows Terminal 终端管理员方式运行" -ForegroundColor Green
50+
Write-Host "如果 nvm、git、python 安装失败,建议手动安装" -ForegroundColor Green
51+
Write-Host "下载失败可指定旁路由代理:.\autorun.ps1 -UseProxy -ProxyHost `"192.168.1.21:7890`"" -ForegroundColor Green
52+
Write-Host "如果旁路由也下载失败那就换成道长那个白嫖地址" -ForegroundColor Green
3553
$confirm = Read-Host "您是否理解并同意继续?(y/n) 默认(y)"
3654
if ($confirm -eq "n") { exit 1 }
3755
}
3856

39-
# ---------- 安装 Node ----------
57+
# ---------- 安装 Node / nvm / git / python ----------
4058
Use-ProxyIfNeeded -Script {
59+
$needNode = $false
4160
if (Test-Cmd "node") {
4261
$nodeVer = (node -v) -replace '^v','' -split '\.' | ForEach-Object { [int]$_ }
4362
if ($nodeVer[0] -ge 20) {
44-
Write-Host "已检测到 Node v$($nodeVer -join '.') ≥20,跳过安装"
63+
Write-Host "已检测到 Node v$($nodeVer -join '.') ≥20,跳过安装" -ForegroundColor Green
4564
} else {
46-
if (-not (Test-Cmd "nvm")) {
47-
Write-Host "正在安装 nvm-windows..."
48-
$nvmSetup = "$env:TEMP\nvm-setup.exe"
49-
Invoke-WebRequest "https://download.fastgit.org/coreybutler/nvm-windows/releases/latest/download/nvm-setup.exe" -OutFile $nvmSetup
50-
Start-Process -Wait -FilePath $nvmSetup -ArgumentList "/silent"
51-
Remove-Item $nvmSetup
52-
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
53-
}
54-
nvm install 20
55-
nvm use 20
65+
Write-Host "Node 版本低于 20,将使用 nvm 安装/切换到 20" -ForegroundColor Yellow
66+
$needNode = $true
5667
}
5768
} else {
58-
if (-not (Test-Cmd "nvm")) {
59-
Write-Host "正在安装 nvm-windows..."
60-
$nvmSetup = "$env:TEMP\nvm-setup.exe"
61-
Invoke-WebRequest "https://download.fastgit.org/coreybutler/nvm-windows/releases/latest/download/nvm-setup.exe" -OutFile $nvmSetup
62-
Start-Process -Wait -FilePath $nvmSetup -ArgumentList "/silent"
63-
Remove-Item $nvmSetup
64-
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
65-
}
69+
Write-Host "未检测到 Node,准备安装" -ForegroundColor Yellow
70+
$needNode = $true
71+
}
72+
73+
if (-not (Test-Cmd "nvm")) {
74+
Write-Host "正在安装 nvm-windows..." -ForegroundColor Green
75+
$nvmSetup = "$env:TEMP\nvm-setup.exe"
76+
Invoke-WebRequestWithProxy "https://github.com/coreybutler/nvm-windows/releases/latest/download/nvm-setup.exe" $nvmSetup
77+
Start-Process -Wait -FilePath $nvmSetup -ArgumentList "/silent"
78+
Remove-Item $nvmSetup
79+
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
80+
} else {
81+
Write-Host "已检测到 nvm,跳过安装" -ForegroundColor Green
82+
}
83+
84+
if ($needNode) {
6685
nvm install 20
6786
nvm use 20
6887
}
6988

70-
foreach ($tool in @("yarn","pm2","git","python")) {
71-
if (-not (Test-Cmd $tool)) {
72-
switch ($tool) {
73-
"yarn" { npm install -g yarn }
74-
"pm2" { npm install -g pm2 }
75-
"git" { winget install --id Git.Git -e --source winget }
76-
"python" { winget install --id Python.Python.3 -e --source winget }
77-
}
78-
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
89+
$tools = @{
90+
yarn = { npm install -g yarn }
91+
pm2 = { npm install -g pm2 }
92+
git = { winget install --id Git.Git -e --source winget }
93+
python = { winget install --id Python.Python.3 -e --source winget }
94+
}
95+
96+
foreach ($kv in $tools.GetEnumerator()) {
97+
$cmd = $kv.Key
98+
if (-not (Test-Cmd $cmd)) {
99+
Write-Host "正在安装 $cmd ..." -ForegroundColor Yellow
100+
& $kv.Value
101+
} else {
102+
Write-Host "已检测到 $cmd,跳过安装" -ForegroundColor Green
79103
}
80104
}
105+
106+
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
81107
}
82108

83109
# ---------- 工作目录 ----------
@@ -89,17 +115,21 @@ $remoteRepo = "https://github.com/hjdhnx/drpy-node.git"
89115
# ---------- 首次克隆 / 配置 ----------
90116
Use-ProxyIfNeeded -Script {
91117
if (-not (Test-Path $projectPath)) {
92-
Write-Host "正在克隆仓库..."
93-
git clone $remoteRepo $projectPath
118+
Write-Host "正在克隆仓库..." -ForegroundColor Yellow
119+
if ($UseProxy) {
120+
git -c http.proxy="http://$ProxyHost" clone $remoteRepo $projectPath
121+
} else {
122+
git clone $remoteRepo $projectPath
123+
}
94124
}
95125
Set-Location $projectPath
96126

97127
# 初始化配置
98128
$configJson = "config\env.json"
99129
if (-not (Test-Path $configJson)) {
100130
@{
101-
ali_token = ""; ali_refresh_token = ""; quark_cookie = "";
102-
uc_cookie = ""; bili_cookie = ""; thread = "10";
131+
ali_token = ""; ali_refresh_token = ""; quark_cookie = ""
132+
uc_cookie = ""; bili_cookie = ""; thread = "10"
103133
enable_dr2 = "1"; enable_py = "2"
104134
} | ConvertTo-Json | Set-Content $configJson -Encoding UTF8
105135
}
@@ -116,32 +146,32 @@ Use-ProxyIfNeeded -Script {
116146
-replace 'API_AUTH_NAME = .*', "API_AUTH_NAME = $(if([string]::IsNullOrWhiteSpace($apiUser)){'admin'}else{$apiUser})" `
117147
-replace 'API_AUTH_CODE = .*', "API_AUTH_CODE = $(if([string]::IsNullOrWhiteSpace($apiPass)){'drpys'}else{$apiPass})" `
118148
-replace 'API_PWD = .*', "API_PWD = $(if([string]::IsNullOrWhiteSpace($apiPwd)){'dzyyds'}else{$apiPwd})" |
119-
Set-Content $envFile -Encoding UTF8
149+
Set-Content $envFile -Encoding UTF8
120150
}
121151

122152
# 首次安装依赖
123153
if (-not (Test-Path "node_modules")) {
124-
Write-Host "首次安装 Node 依赖..."
154+
Write-Host "首次安装 Node 依赖..." -ForegroundColor Yellow
125155
yarn config set registry https://registry.npmmirror.com/
126156
yarn
127157
}
128158
if (-not (Test-Path ".venv\pyvenv.cfg")) {
129-
Write-Host "首次创建 Python 虚拟环境..."
159+
Write-Host "首次创建 Python 虚拟环境..." -ForegroundColor Yellow
130160
python -m venv .venv
131161
}
132162
& .\.venv\Scripts\Activate.ps1
133163
if ((git diff HEAD^ HEAD --name-only 2>$null) -match "requirements.txt") {
134-
Write-Host "检测到 requirements.txt 变动,更新 Python 依赖..."
164+
Write-Host "检测到 requirements.txt 变动,更新 Python 依赖..." -ForegroundColor Yellow
135165
pip install -r spider\py\base\requirements.txt -i https://mirrors.cloud.tencent.com/pypi/simple
136166
}
137167

138168
# 首次或 PM2 未启动时启动
139169
if (-not (pm2 list | Select-String "drpyS.*online")) {
140-
Write-Host "首次启动 PM2 进程..."
170+
Write-Host "首次启动 PM2 进程..." -ForegroundColor Yellow
141171
pm2 start index.js --name drpyS --update-env
142172
pm2 save
143173
} else {
144-
Write-Host "PM2 进程 drpyS 已在运行,跳过启动"
174+
Write-Host "PM2 进程 drpyS 已在运行,跳过启动" -ForegroundColor Green
145175
}
146176
}
147177

@@ -156,7 +186,7 @@ if (-not (Get-ScheduledTask -TaskName $taskStartup -ErrorAction SilentlyContinue
156186
$setting = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
157187
Register-ScheduledTask -TaskName $taskStartup -Action $action -Trigger $trigger `
158188
-Settings $setting -User "SYSTEM" -RunLevel Highest -Force | Out-Null
159-
Write-Host "已创建开机自启任务:$taskStartup"
189+
Write-Host "已创建开机自启任务:$taskStartup" -ForegroundColor Yellow
160190
}
161191

162192
if (-not (Get-ScheduledTask -TaskName $taskUpdate -ErrorAction SilentlyContinue)) {
@@ -166,14 +196,15 @@ if (-not (Get-ScheduledTask -TaskName $taskUpdate -ErrorAction SilentlyContinue)
166196
$setting = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
167197
Register-ScheduledTask -TaskName $taskUpdate -Action $action -Trigger $trigger `
168198
-Settings $setting -User "SYSTEM" -RunLevel Highest -Force | Out-Null
169-
Write-Host "已创建每 24 小时更新任务:$taskUpdate"
199+
Write-Host "已创建每 24 小时更新任务:$taskUpdate" -ForegroundColor Yellow
170200
}
171201

172202
# ---------- 完成 ----------
173203
$ip = (ipconfig | Select-String "IPv4 地址" | Select-Object -First 1).ToString().Split(":")[-1].Trim()
174204
$public = (Invoke-RestMethod "https://ipinfo.io/ip")
175-
Write-Host "内网地址:http://${ip}:5757"
176-
Write-Host "公网地址:http://${public}:5757"
177-
Write-Host "脚本执行完成!重启后 drpyS 自动启动并每 24 小时检查更新。"
178-
Write-Host "脚本只需要执行一次不需要重复执行。"
179-
Write-Host "如果弹出空白窗口可以直接关闭不影响使用。"
205+
Write-Host "内网地址:http://${ip}:5757" -ForegroundColor Yellow
206+
Write-Host "公网地址:http://${public}:5757" -ForegroundColor Yellow
207+
Write-Host "脚本执行完成!重启后 drpyS 自动启动并每 24 小时检查更新。" -ForegroundColor Yellow
208+
Write-Host "脚本只需要执行一次,无需重复执行。" -ForegroundColor Yellow
209+
Write-Host "按任意键退出..." -ForegroundColor Green
210+
Read-Host

0 commit comments

Comments
 (0)