vanspod 0.1.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,105 @@
1
+ param([string]$Plan = (Join-Path $PSScriptRoot 'update-plan.json'))
2
+ $ErrorActionPreference = 'Stop'
3
+ $utf8 = New-Object System.Text.UTF8Encoding($false)
4
+ $planData = [System.IO.File]::ReadAllText($Plan, $utf8) | ConvertFrom-Json
5
+ $localRoot = if ($env:LOCALAPPDATA) { $env:LOCALAPPDATA } else { $env:USERPROFILE }
6
+ $root = [System.IO.Path]::GetFullPath($planData.root)
7
+ $installation = $planData.installation
8
+ if ($planData.version -ne 1 -or $root -ne [System.IO.Path]::GetFullPath((Join-Path $localRoot 'VansPod')) -or [System.IO.Path]::GetFullPath($Plan) -ne (Join-Path $root 'update-plan.json') -or $planData.target -notmatch '^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$' -or $planData.from -notmatch '^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$') { throw 'Invalid update plan' }
9
+ if ($installation.kind -eq 'exe') {
10
+ $file = [System.IO.Path]::GetFullPath($installation.file)
11
+ if ([System.IO.Path]::GetExtension($file) -ne '.exe' -or [System.IO.Path]::GetFullPath($planData.download) -ne "$file.new") { throw 'Invalid update plan' }
12
+ } elseif ($installation.kind -ne 'npm') { throw 'Invalid update plan' }
13
+ [System.IO.File]::WriteAllText((Join-Path $root 'update-ready'), (@{pid = $PID} | ConvertTo-Json -Compress), $utf8)
14
+
15
+ function Write-Result([bool]$Ok, [string]$Message) {
16
+ $value = [ordered]@{ ok = $Ok; from = $planData.from; target = $planData.target }
17
+ if ($Message) { $value.message = $Message }
18
+ [System.IO.File]::WriteAllText((Join-Path $root 'update-result.json'), ($value | ConvertTo-Json -Compress), $utf8)
19
+ }
20
+
21
+ function Read-Instance {
22
+ try { return [System.IO.File]::ReadAllText((Join-Path $root 'runtime/instance.json'), $utf8) | ConvertFrom-Json } catch { return $null }
23
+ }
24
+
25
+ # 啟動指令等到 VansPod 就緒才結束;以結束碼與登記的版本判斷是否成功。
26
+ function Start-VansPod([string]$Expected) {
27
+ $env:VANSPOD_RESTART = '1'
28
+ # 工具是舊 worker 啟動的;帶著它的 token,launcher 會誤以為自己是 worker 而拒絕啟動。
29
+ Remove-Item Env:VANSPOD_WORKER_TOKEN -ErrorAction SilentlyContinue
30
+ $quoted = @($planData.start.args | ForEach-Object { '"' + ($_ -replace '"', '\"') + '"' }) -join ' '
31
+ $process = Start-Process -FilePath $planData.start.file -ArgumentList $quoted -WindowStyle Hidden -PassThru
32
+ $null = $process.Handle # 先取得 handle,結束後才讀得到 ExitCode。
33
+ if (-not $process.WaitForExit(120000)) { try { $process.Kill() } catch {}; return $false }
34
+ if ($process.ExitCode -ne 0) { return $false }
35
+ $instance = Read-Instance
36
+ return $instance -and $instance.version -eq $Expected
37
+ }
38
+
39
+ # 新版沒有就緒時,確保它的背景服務不會繼續佔用檔案。
40
+ function Stop-Target {
41
+ $instance = Read-Instance
42
+ if ($instance -and $instance.version -eq $planData.target) { Stop-Process -Id $instance.pid -Force -ErrorAction SilentlyContinue; Start-Sleep -Milliseconds 500 }
43
+ if ($installation.kind -eq 'exe') { Get-Process -ErrorAction SilentlyContinue | Where-Object { $_.Path -eq $file } | Stop-Process -Force -ErrorAction SilentlyContinue }
44
+ }
45
+
46
+ # 把 .old 換回原位;檔案可能還被剛結束的新版佔著,所以重試一陣子。學生不能因此失去 VansPod.exe。
47
+ function Restore-Exe {
48
+ for ($attempt = 0; $attempt -lt 20; $attempt++) {
49
+ try {
50
+ if (Test-Path -LiteralPath $file) { Remove-Item -LiteralPath $file -Force }
51
+ Move-Item -LiteralPath "$file.old" -Destination $file
52
+ return
53
+ } catch { Start-Sleep -Milliseconds 250 }
54
+ }
55
+ throw "無法換回 v$($planData.from),舊版保留在 $file.old。"
56
+ }
57
+
58
+ $restarted = $true
59
+ $stopped = $false
60
+ try {
61
+ for ($attempt = 0; $attempt -lt 600 -and (Get-Process -Id $planData.pid -ErrorAction SilentlyContinue); $attempt++) { Start-Sleep -Milliseconds 100 }
62
+ if (Get-Process -Id $planData.pid -ErrorAction SilentlyContinue) { throw '舊版 VansPod 沒有結束,更新已取消。' }
63
+ $stopped = $true
64
+ if ($installation.kind -eq 'exe') {
65
+ $old = "$file.old"
66
+ if (Test-Path -LiteralPath $old) { Remove-Item -LiteralPath $old -Force }
67
+ Move-Item -LiteralPath $file -Destination $old
68
+ try { Move-Item -LiteralPath "$file.new" -Destination $file }
69
+ catch { Restore-Exe; throw "新版 VansPod.exe 無法放到原位置($($_.Exception.Message))" }
70
+ if (Start-VansPod $planData.target) { Remove-Item -LiteralPath $old -Force; Write-Result $true }
71
+ else {
72
+ Stop-Target
73
+ Restore-Exe
74
+ $restarted = Start-VansPod $planData.from
75
+ Write-Result $false "v$($planData.target) 無法啟動,已換回 v$($planData.from)。"
76
+ }
77
+ } else {
78
+ $ErrorActionPreference = 'Continue'
79
+ $output = & $installation.node $installation.npmCli install --global --prefix $installation.prefix "vanspod@$($planData.target)" 2>&1
80
+ $installed = $LASTEXITCODE -eq 0
81
+ $ErrorActionPreference = 'Stop'
82
+ if (-not $installed) {
83
+ $restarted = Start-VansPod $planData.from
84
+ $reason = @($output | ForEach-Object { "$_" } | Where-Object { $_.Trim() }) | Select-Object -Last 1
85
+ Write-Result $false "npm 安裝失敗($reason)。已保留 v$($planData.from)。可改用:npm install -g vanspod@$($planData.target)"
86
+ } elseif (Start-VansPod $planData.target) { Write-Result $true }
87
+ else {
88
+ Stop-Target
89
+ $restarted = $false
90
+ Write-Result $false "已安裝 v$($planData.target),但無法啟動。請執行 vanspod start 查看錯誤,或用 npm install -g vanspod@$($planData.from) 換回舊版。"
91
+ }
92
+ }
93
+ } catch {
94
+ Write-Result $false "更新未完成:$($_.Exception.Message)"
95
+ # 舊版已經結束時,盡量讓學生回到原本的版本。
96
+ $restarted = $stopped -and (Start-VansPod $planData.from)
97
+ if ($installation.kind -eq 'exe') { Remove-Item -LiteralPath "$file.new" -Force -ErrorAction SilentlyContinue }
98
+ } finally {
99
+ foreach ($name in @('update-plan.json', 'update-ready')) { Remove-Item -LiteralPath (Join-Path $root $name) -Force -ErrorAction SilentlyContinue }
100
+ Remove-Item -LiteralPath $PSCommandPath -Force -ErrorAction SilentlyContinue
101
+ }
102
+ $result = [System.IO.File]::ReadAllText((Join-Path $root 'update-result.json'), $utf8) | ConvertFrom-Json
103
+ # VansPod 沒有重新啟動時,頁面看不到結果,改用 Windows 訊息告知。
104
+ if (-not $restarted -and -not $planData.headless) { Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show($result.message, 'VansPod') | Out-Null }
105
+ if (-not $result.ok) { exit 1 }