Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
216 lines (184 loc) · 8.08 KB
/
Copy pathinstall.ps1
File metadata and controls
216 lines (184 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env pwsh
# Resend CLI installer for Windows
#
# Usage (PowerShell):
# irm https://resend.com/install.ps1 | iex
#
# Pin a version:
# $env:RESEND_VERSION = 'v0.1.0'; irm https://resend.com/install.ps1 | iex
#
# Environment variables:
# RESEND_INSTALL - Custom install directory (default: $HOME\.resend)
# RESEND_VERSION - Version to install (default: latest)
param(
[string]$Version = $env:RESEND_VERSION
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# --- Helpers -----------------------------------------------------------------
function Write-Info { param($msg) Write-Host " $msg" -ForegroundColor DarkGray }
function Write-Ok { param($msg) Write-Host " $msg" -ForegroundColor Green }
function Write-Fail {
param($msg)
Write-Host " error: $msg" -ForegroundColor Red
}
# --- Architecture detection --------------------------------------------------
if ($env:PROCESSOR_ARCHITECTURE -notin @('AMD64', 'EM64T')) {
Write-Fail "Unsupported architecture: $env:PROCESSOR_ARCHITECTURE`n`n Resend CLI currently supports Windows x64 only."
throw "Installation failed."
}
# --- Version + Download URL --------------------------------------------------
$repo = 'https://github.com/resend/resend-cli'
$target = 'windows-x64'
if ($Version) {
$Version = $Version.TrimStart('v')
if ($Version -notmatch '^\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?$') {
Write-Fail "Invalid version format: $Version`n`n Expected: semantic version like 0.1.0 or 1.2.3-beta.1`n Usage: `$env:RESEND_VERSION = 'v0.1.0'; irm https://resend.com/install.ps1 | iex"
throw "Installation failed."
}
} else {
# Resolve "latest" to a concrete tag so the archive and its checksums come
# from the same release even while a new one is being published.
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$latestResponse = Invoke-WebRequest -Uri "$repo/releases/latest" -Method Head -UseBasicParsing
} catch {
Write-Fail "Failed to resolve the latest release.`n`n Possible causes:`n - No internet connection`n - GitHub is unreachable`n`n URL: $repo/releases/latest"
throw "Installation failed."
}
$base = $latestResponse.BaseResponse
if ($base -is [System.Net.HttpWebResponse]) {
$finalUrl = $base.ResponseUri.AbsoluteUri # Windows PowerShell 5.1
} else {
$finalUrl = $base.RequestMessage.RequestUri.AbsoluteUri # PowerShell 7+
}
if ($finalUrl -notmatch '/releases/tag/v(.+)$') {
Write-Fail "Could not determine the latest version from: $finalUrl"
throw "Installation failed."
}
$Version = $Matches[1]
}
$url = "$repo/releases/download/v$Version/resend-$target.zip"
# --- Install directory -------------------------------------------------------
if ($env:RESEND_INSTALL) { $installDir = $env:RESEND_INSTALL } else { $installDir = Join-Path $HOME '.resend' }
$binDir = Join-Path $installDir 'bin'
$exe = Join-Path $binDir 'resend.exe'
if (-not (Test-Path $binDir)) {
New-Item -ItemType Directory -Path $binDir -Force | Out-Null
}
# --- Download + Extract ------------------------------------------------------
Write-Host ""
Write-Host " Installing Resend CLI..." -ForegroundColor White
Write-Host ""
Write-Info "Downloading from $url"
Write-Host ""
$tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) "resend-$([System.Guid]::NewGuid())"
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
$tmpZip = Join-Path $tmpDir 'resend.zip'
try {
try {
# Force TLS 1.2 for Windows PowerShell 5.1 (no-op on PowerShell 7+ where it is the default)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ProgressPreference = 'SilentlyContinue' # Invoke-WebRequest is ~10x faster without progress bar
Invoke-WebRequest -Uri $url -OutFile $tmpZip -UseBasicParsing
} catch {
Write-Fail "Download failed.`n`n Possible causes:`n - No internet connection`n - The version does not exist: $Version`n - GitHub is unreachable`n`n URL: $url"
throw "Installation failed."
}
# --- Checksum verification ---------------------------------------------
$archiveName = "resend-$target.zip"
$checksumsUrl = ($url.Substring(0, $url.LastIndexOf('/'))) + '/checksums.txt'
$tmpChecksums = Join-Path $tmpDir 'checksums.txt'
$expected = $null
$checksumsAvailable = $true
# Releases up to v2.10.0 predate checksums.txt.
$versionHasChecksums = [version](($Version -split '-')[0]) -gt [version]'2.10.0'
try {
Invoke-WebRequest -Uri $checksumsUrl -OutFile $tmpChecksums -UseBasicParsing
} catch {
$status = $null
if ($_.Exception.PSObject.Properties['Response'] -and $_.Exception.Response) {
$status = [int]$_.Exception.Response.StatusCode
}
if ($status -eq 404 -and -not $versionHasChecksums) {
# Old versions stay installable, but only they may skip verification.
Write-Info "This release predates published checksums -- skipping verification"
$checksumsAvailable = $false
} elseif ($status -eq 404) {
Write-Fail "checksums.txt not found for v$Version (HTTP 404).`n`n Every release after v2.10.0 publishes checksums. The release may be incomplete or tampered with.`n Report it: https://github.com/resend/resend-cli/issues"
throw "Installation failed."
} else {
if (-not $status) { $status = '000' }
Write-Fail "Failed to download checksums.txt (HTTP $status).`n`n Refusing to install without verification -- try again.`n`n URL: $checksumsUrl"
throw "Installation failed."
}
}
if ($checksumsAvailable) {
foreach ($line in Get-Content $tmpChecksums) {
$parts = $line.Trim() -split '\s+'
if ($parts.Length -eq 2 -and $parts[1] -eq $archiveName) {
$expected = $parts[0].ToLower()
break
}
}
if (-not $expected) {
Write-Fail "checksums.txt does not list $archiveName.`n`n The release may be incomplete or tampered with. Please, try again.`n Report it: https://github.com/resend/resend-cli/issues"
throw "Installation failed."
}
$actual = (Get-FileHash -Path $tmpZip -Algorithm SHA256).Hash.ToLower()
if ($actual -ne $expected) {
Write-Fail "Checksum verification failed for $archiveName.`n`n Expected: $expected`n Actual: $actual`n`n If the problem persists, report it: https://github.com/resend/resend-cli/issues"
throw "Installation failed."
}
Write-Info "Checksum verified"
}
try {
Expand-Archive -Path $tmpZip -DestinationPath $binDir -Force
} catch {
Write-Fail "Failed to extract archive: $_"
throw "Installation failed."
}
} finally {
Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
}
if (-not (Test-Path $exe)) {
Write-Fail "Binary not found after extraction. The download may be corrupted -- try again."
throw "Installation failed."
}
# --- Verify installation -----------------------------------------------------
try {
$installedVersion = (& $exe --version 2>$null).Trim()
} catch {
$installedVersion = 'unknown'
}
Write-Host ""
Write-Ok "Resend CLI $installedVersion installed successfully!"
Write-Host ""
Write-Info "Binary: $exe"
# --- PATH setup --------------------------------------------------------------
$userPath = [Environment]::GetEnvironmentVariable('PATH', 'User')
if (-not $userPath) { $userPath = '' }
$pathEntries = $userPath -split ';' | Where-Object { $_ -ne '' }
if ($pathEntries -contains $binDir) {
# Already on PATH -- just print the getting-started line
Write-Host ""
Write-Host " Run " -NoNewline
Write-Host "resend --help" -ForegroundColor Cyan -NoNewline
Write-Host " to get started"
Write-Host ""
return
}
# Add to user PATH (persists across sessions -- no admin rights needed)
$newPath = ($pathEntries + $binDir) -join ';'
[Environment]::SetEnvironmentVariable('PATH', $newPath, 'User')
$env:PATH = "$env:PATH;$binDir" # Also update the current session
Write-Info "Added $binDir to PATH (User scope)"
Write-Host ""
Write-Info "Restart your terminal, then:"
Write-Host ""
Write-Info "Next steps:"
Write-Host ""
Write-Host " `$env:RESEND_API_KEY = 're_...'" -ForegroundColor Cyan
Write-Host " resend --help" -ForegroundColor Cyan
Write-Host ""
return