@@ -15,40 +15,34 @@ if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdenti
1515 exit
1616}
1717
18- # ----------- 切回脚本目录 -----------
1918Set-Location - LiteralPath $PSScriptRoot
2019
21- # ---------- 代理开关 ----------
20+ # ---------- 代理 ----------
2221function Use-ProxyIfNeeded {
2322 param ([scriptblock ]$Script )
2423 if ($UseProxy ) {
2524 $oldHttp = [Environment ]::GetEnvironmentVariable(" HTTP_PROXY" )
2625 $oldHttps = [Environment ]::GetEnvironmentVariable(" HTTPS_PROXY" )
2726 [Environment ]::SetEnvironmentVariable(" HTTP_PROXY" , " http://$ProxyHost " , " Process" )
2827 [Environment ]::SetEnvironmentVariable(" HTTPS_PROXY" , " http://$ProxyHost " , " Process" )
29- try { & $Script }
30- finally {
28+ try { & $Script } finally {
3129 [Environment ]::SetEnvironmentVariable(" HTTP_PROXY" , $oldHttp , " Process" )
3230 [Environment ]::SetEnvironmentVariable(" HTTPS_PROXY" , $oldHttps , " Process" )
3331 }
3432 } else { & $Script }
3533}
36-
3734function Test-Cmd { param ($cmd ); $null -ne (Get-Command $cmd - ErrorAction SilentlyContinue) }
3835function 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- }
36+ if ($UseProxy ) { Invoke-WebRequest $Url - OutFile $OutFile - Proxy " http://$ProxyHost " }
37+ else { Invoke-WebRequest $Url - OutFile $OutFile }
4438}
4539
4640# ---------- 用户确认 ----------
4741if (-not $SkipConfirm ) {
4842 Write-Host " 警告:此脚本仅适用于 Windows 10/11 64 位" - ForegroundColor Green
4943 Write-Host " 建议使用 Windows Terminal 终端管理员方式运行" - ForegroundColor Green
5044 Write-Host " 如果 nvm、git、python 安装失败,建议手动安装" - ForegroundColor Green
51- Write-Host " 下载失败可指定旁路由代理:.\autorun .ps1 -UseProxy -ProxyHost `" 192.168.1.21:7890`" " - ForegroundColor Green
45+ Write-Host " 下载失败可指定旁路由代理:.\drpys-final .ps1 -UseProxy -ProxyHost `" 192.168.1.21:7890`" " - ForegroundColor Green
5246 Write-Host " 如果旁路由也下载失败那就换成道长那个白嫖地址" - ForegroundColor Green
5347 $confirm = Read-Host " 您是否理解并同意继续?(y/n) 默认(y)"
5448 if ($confirm -eq " n" ) { exit 1 }
@@ -92,7 +86,6 @@ Use-ProxyIfNeeded -Script {
9286 git = { winget install -- id Git.Git - e -- source winget }
9387 python = { winget install -- id Python.Python.3 - e -- source winget }
9488 }
95-
9689 foreach ($kv in $tools.GetEnumerator ()) {
9790 $cmd = $kv.Key
9891 if (-not (Test-Cmd $cmd )) {
@@ -102,7 +95,6 @@ Use-ProxyIfNeeded -Script {
10295 Write-Host " 已检测到 $cmd ,跳过安装" - ForegroundColor Green
10396 }
10497 }
105-
10698 $env: Path = [System.Environment ]::GetEnvironmentVariable(" Path" , " Machine" ) + " ;" + [System.Environment ]::GetEnvironmentVariable(" Path" , " User" )
10799}
108100
@@ -124,7 +116,6 @@ Use-ProxyIfNeeded -Script {
124116 }
125117 Set-Location $projectPath
126118
127- # 初始化配置
128119 $configJson = " config\env.json"
129120 if (-not (Test-Path $configJson )) {
130121 @ {
@@ -149,7 +140,6 @@ Use-ProxyIfNeeded -Script {
149140 Set-Content $envFile - Encoding UTF8
150141 }
151142
152- # 首次安装依赖
153143 if (-not (Test-Path " node_modules" )) {
154144 Write-Host " 首次安装 Node 依赖..." - ForegroundColor Yellow
155145 yarn config set registry https:// registry.npmmirror.com /
@@ -165,7 +155,6 @@ Use-ProxyIfNeeded -Script {
165155 pip install - r spider\py\base\requirements.txt - i https:// mirrors.cloud.tencent.com / pypi/ simple
166156 }
167157
168- # 首次或 PM2 未启动时启动
169158 if (-not (pm2 list | Select-String " drpyS.*online" )) {
170159 Write-Host " 首次启动 PM2 进程..." - ForegroundColor Yellow
171160 pm2 start index.js -- name drpyS -- update-env
@@ -179,24 +168,44 @@ Use-ProxyIfNeeded -Script {
179168$taskStartup = " drpyS_PM2_Startup"
180169$taskUpdate = " drpyS_Update"
181170
182- if (-not (Get-ScheduledTask - TaskName $taskStartup - ErrorAction SilentlyContinue)) {
183- $action = New-ScheduledTaskAction - Execute " powershell.exe" `
184- - Argument " -NoProfile -WindowStyle Hidden -Command pm2 resurrect"
185- $trigger = New-ScheduledTaskTrigger - AtStartup - RandomDelay (New-TimeSpan - Seconds 30 )
186- $setting = New-ScheduledTaskSettingsSet - AllowStartIfOnBatteries - DontStopIfGoingOnBatteries - StartWhenAvailable
187- Register-ScheduledTask - TaskName $taskStartup - Action $action - Trigger $trigger `
188- - Settings $setting - User " SYSTEM" - RunLevel Highest - Force | Out-Null
189- Write-Host " 已创建开机自启任务:$taskStartup " - ForegroundColor Yellow
190- }
171+ # 获取绝对路径
172+ $pm2 = (Get-Command pm2.cmd - ErrorAction SilentlyContinue).Source
173+ $nodeExe = (Get-Command node.exe - ErrorAction SilentlyContinue).Source
174+
175+ if (-not $pm2 -or -not $nodeExe ) {
176+ Write-Warning " 找不到 pm2.cmd 或 node.exe,跳过计划任务注册"
177+ } else {
178+ # 删除旧任务
179+ $taskStartup , $taskUpdate | ForEach-Object {
180+ if (Get-ScheduledTask - TaskName $_ - ErrorAction SilentlyContinue) {
181+ Unregister-ScheduledTask - TaskName $_ - Confirm:$false
182+ }
183+ }
184+
185+ $commonSettings = New-ScheduledTaskSettingsSet `
186+ - AllowStartIfOnBatteries - DontStopIfGoingOnBatteries - StartWhenAvailable
191187
192- if (-not (Get-ScheduledTask - TaskName $taskUpdate - ErrorAction SilentlyContinue)) {
193- $action = New-ScheduledTaskAction - Execute " powershell.exe" `
194- - Argument " -NoProfile -WindowStyle Hidden -Command `" & { cd '$projectPath '; git fetch origin; if (git status -uno | Select-String 'Your branch is behind') { git reset --hard origin/main; yarn --prod --silent; if (git diff HEAD^ HEAD --name-only | Select-String 'spider/py/base/requirements.txt') { python -m venv .venv; & .\.venv\Scripts\Activate.ps1; pip install -r spider\py\base\requirements.txt -q } pm2 restart drpyS } }`" "
188+ # 1) 开机自启(直接启动 drpyS,不依赖 dump)
189+ $action = New-ScheduledTaskAction `
190+ - Execute " powershell.exe" `
191+ - Argument " -NoProfile -ExecutionPolicy Bypass -Command `" & { Set-Location '$projectPath '; & '$nodeExe ' '$projectPath \index.js' | & '$pm2 ' start '$projectPath \index.js' --name drpyS --update-env }`" " `
192+ - WorkingDirectory $projectPath
193+ $trigger = New-ScheduledTaskTrigger - AtStartup - RandomDelay (New-TimeSpan - Seconds 30 )
194+ Register-ScheduledTask - TaskName $taskStartup `
195+ - Action $action - Trigger $trigger - Settings $commonSettings `
196+ - User " SYSTEM" - RunLevel Highest - Force | Out-Null
197+ Write-Host " 已创建/更新开机自启任务:$taskStartup " - ForegroundColor Yellow
198+
199+ # 2) 每 24 h 更新
200+ $action = New-ScheduledTaskAction `
201+ - Execute " powershell.exe" `
202+ - Argument " -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -Command `" & { Set-Location '$projectPath '; git fetch origin; if (git status -uno | Select-String 'Your branch is behind') { git reset --hard origin/main; yarn --prod --silent; if (git diff HEAD^ HEAD --name-only | Select-String 'spider/py/base/requirements.txt') { python -m venv .venv; & .\.venv\Scripts\Activate.ps1; pip install -r spider\py\base\requirements.txt -q } & '$pm2 ' restart drpyS } }`" " `
203+ - WorkingDirectory $projectPath
195204 $trigger = New-ScheduledTaskTrigger - Once - At (Get-Date ) - RepetitionInterval (New-TimeSpan - Hours 24 )
196- $setting = New-ScheduledTaskSettingsSet - AllowStartIfOnBatteries - DontStopIfGoingOnBatteries - StartWhenAvailable
197- Register-ScheduledTask - TaskName $taskUpdate - Action $action - Trigger $trigger `
198- - Settings $setting - User " SYSTEM" - RunLevel Highest - Force | Out-Null
199- Write-Host " 已创建每 24 小时更新任务:$taskUpdate " - ForegroundColor Yellow
205+ Register-ScheduledTask - TaskName $taskUpdate `
206+ - Action $action - Trigger $trigger - Settings $commonSettings `
207+ - User " SYSTEM" - RunLevel Highest - Force | Out-Null
208+ Write-Host " 已创建/更新每 24 小时更新任务:$taskUpdate " - ForegroundColor Yellow
200209}
201210
202211# ---------- 完成 ----------
@@ -206,5 +215,5 @@ Write-Host "内网地址:http://${ip}:5757" -ForegroundColor Yellow
206215Write-Host " 公网地址:http://${public} :5757" - ForegroundColor Yellow
207216Write-Host " 脚本执行完成!重启后 drpyS 自动启动并每 24 小时检查更新。" - ForegroundColor Yellow
208217Write-Host " 脚本只需要执行一次,无需重复执行。" - ForegroundColor Yellow
209- Write-Host " 按任意键退出... " - ForegroundColor Green
218+ Write-Host " 按任意键退出!!! " - ForegroundColor Yellow
210219Read-Host
0 commit comments