t3mode 0.0.0-stage → 0.1.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,111 @@
1
+ # Installs a T3 Code update after T3 Code quits, then puts the t3mode shim back.
2
+ # Started detached by the t3mode loader (packages/main/src/Updater.ts) from Electron's will-quit.
3
+ # 1. Wait for every T3 Code process to exit.
4
+ # 2. Run the downloaded installer silently, the way electron-updater does (--updated /S),
5
+ # and wait for that process.
6
+ # 3. Re-apply the shim with repair.cjs, run by T3 Code's own exe in Node mode.
7
+ # 4. After "Restart now", start T3 Code again with the arguments it had.
8
+ # It must not be T3 Code's exe itself: the installer replaces that exe while this script runs.
9
+ param(
10
+ [Parameter(Mandatory)] [int] $ParentPid,
11
+ [Parameter(Mandatory)] [string] $Installer,
12
+ [Parameter(Mandatory)] [string] $InstallDir,
13
+ [Parameter(Mandatory)] [string] $RepairJs,
14
+ [Parameter(Mandatory)] [string] $ShimJs,
15
+ [Parameter(Mandatory)] [string] $HomeDir,
16
+ [Parameter(Mandatory)] [string] $Log,
17
+ [string] $Version = "",
18
+ [string] $Relaunch = "0",
19
+ [string] $RelaunchArgs = "",
20
+ [string] $KeepUpdaterEnv = "0"
21
+ )
22
+
23
+ $ErrorActionPreference = "Stop"
24
+
25
+ function Write-Log([string] $Message) {
26
+ try {
27
+ New-Item -ItemType Directory -Force -Path (Split-Path -Parent $Log) | Out-Null
28
+ Add-Content -LiteralPath $Log -Value ("{0} {1}" -f (Get-Date).ToString("o"), $Message)
29
+ } catch {}
30
+ }
31
+
32
+ # Windows command-line quoting: Start-Process joins arguments with spaces and quotes nothing.
33
+ function Format-Argument([string] $Value) {
34
+ if ($Value -ne "" -and $Value -notmatch '[\s"]') { return $Value }
35
+ $escaped = $Value -replace '(\\*)"', '$1$1\"' -replace '(\\+)$', '$1$1'
36
+ return '"' + $escaped + '"'
37
+ }
38
+
39
+ function Get-T3Processes {
40
+ $prefix = $InstallDir.TrimEnd("\") + "\"
41
+ @(Get-Process -ErrorAction SilentlyContinue | Where-Object {
42
+ $_.Path -and $_.Path.StartsWith($prefix, [StringComparison]::OrdinalIgnoreCase)
43
+ })
44
+ }
45
+
46
+ function Get-T3Exe {
47
+ Get-ChildItem -LiteralPath $InstallDir -Filter "*.exe" |
48
+ Where-Object { $_.Name -notlike "Uninstall*" } |
49
+ Select-Object -First 1
50
+ }
51
+
52
+ $mutex = New-Object System.Threading.Mutex($false, "Local\t3mode-update-helper")
53
+ if (-not $mutex.WaitOne(0)) {
54
+ Write-Log "Another update helper is running; this one stops."
55
+ exit 0
56
+ }
57
+
58
+ try {
59
+ Write-Log "Update to $Version`: waiting for T3 Code (pid $ParentPid) to exit"
60
+ try { Wait-Process -Id $ParentPid -Timeout 120 -ErrorAction SilentlyContinue } catch {}
61
+ $deadline = (Get-Date).AddSeconds(120)
62
+ while ((Get-T3Processes).Count -gt 0) {
63
+ if ((Get-Date) -gt $deadline) {
64
+ Write-Log "T3 Code is still running after 2 minutes; the update is skipped until the next quit."
65
+ exit 1
66
+ }
67
+ Start-Sleep -Milliseconds 500
68
+ }
69
+
70
+ Write-Log "Running $Installer"
71
+ $install = Start-Process -FilePath $Installer -ArgumentList "--updated", "/S" -Wait -PassThru -WindowStyle Hidden
72
+ Write-Log "Installer exited with code $($install.ExitCode)"
73
+
74
+ # Re-apply the shim whatever the installer did: a failed install may still have replaced app.asar.
75
+ $exe = Get-T3Exe
76
+ if (-not $exe) { throw "No T3 Code executable in $InstallDir after the install." }
77
+ $out = Join-Path $env:TEMP "t3mode-repair.out.txt"
78
+ $err = Join-Path $env:TEMP "t3mode-repair.err.txt"
79
+ $env:ELECTRON_RUN_AS_NODE = "1"
80
+ $repairArgs = @($RepairJs, $InstallDir, $ShimJs, $HomeDir) | ForEach-Object { Format-Argument $_ }
81
+ $repair = Start-Process -FilePath $exe.FullName -ArgumentList $repairArgs -Wait -PassThru -NoNewWindow `
82
+ -RedirectStandardOutput $out -RedirectStandardError $err
83
+ Remove-Item Env:ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue
84
+ $output = ((Get-Content -LiteralPath $out, $err -ErrorAction SilentlyContinue) -join " ").Trim()
85
+ Write-Log "Shim repair exited with code $($repair.ExitCode): $output"
86
+ Remove-Item -LiteralPath $out, $err -ErrorAction SilentlyContinue
87
+
88
+ if ($Relaunch -eq "1") {
89
+ # The new launch decides for itself whether T3 Code's updater stays off.
90
+ if ($KeepUpdaterEnv -ne "1") { Remove-Item Env:T3CODE_DISABLE_AUTO_UPDATE -ErrorAction SilentlyContinue }
91
+ $arguments = @()
92
+ if ($RelaunchArgs) {
93
+ $json = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($RelaunchArgs))
94
+ # Windows PowerShell emits a JSON array as one object; foreach over it gets the items.
95
+ $items = ConvertFrom-Json $json
96
+ foreach ($item in $items) { $arguments += Format-Argument ([string] $item) }
97
+ }
98
+ Write-Log "Starting $($exe.FullName)"
99
+ if ($arguments.Count -gt 0) {
100
+ Start-Process -FilePath $exe.FullName -ArgumentList $arguments | Out-Null
101
+ } else {
102
+ Start-Process -FilePath $exe.FullName | Out-Null
103
+ }
104
+ }
105
+ Write-Log "Update helper done"
106
+ } catch {
107
+ Write-Log "Update helper failed: $_"
108
+ exit 1
109
+ } finally {
110
+ $mutex.ReleaseMutex()
111
+ }