-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenv.local.ps1
More file actions
87 lines (71 loc) · 2.85 KB
/
Copy pathenv.local.ps1
File metadata and controls
87 lines (71 loc) · 2.85 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
$repoRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$envFile = Join-Path $repoRoot '.env'
$supabaseConfigPath = Join-Path $repoRoot 'supabase\config.toml'
if (-not (Test-Path $envFile)) {
throw "Missing local environment file at '$envFile'."
}
$localProjectId = $null
if (Test-Path $supabaseConfigPath) {
$projectIdLine = Select-String -Path $supabaseConfigPath -Pattern '^\s*project_id\s*=\s*"([^"]+)"' | Select-Object -First 1
if ($projectIdLine -and $projectIdLine.Matches.Count -gt 0) {
$localProjectId = $projectIdLine.Matches[0].Groups[1].Value
}
}
$forbiddenRemoteKeys = @(
'SUPABASE_ACCESS_TOKEN',
'SUPABASE_DB_PASSWORD'
)
$localUrlPrefixes = @(
'http://127.0.0.1',
'https://127.0.0.1',
'http://localhost',
'https://localhost'
)
$localOnlyRedirectVariables = @(
'SUPABASE_AUTH_EXTERNAL_GOOGLE_REDIRECT_URI'
)
Get-Content $envFile | ForEach-Object {
if ($_ -match '^\s*#' -or $_ -match '^\s*$') { return }
$name, $value = $_ -split '=', 2
$name = $name.Trim()
$value = $value.Trim()
if ([string]::IsNullOrWhiteSpace($name)) {
return
}
if ($forbiddenRemoteKeys -contains $name) {
throw "env.local.ps1 refuses to load '$name' from .env. Keep remote deploy credentials in GitHub secrets only."
}
if ($name -eq 'SUPABASE_PROJECT_ID' -and -not [string]::IsNullOrWhiteSpace($value)) {
if ([string]::IsNullOrWhiteSpace($localProjectId)) {
throw "env.local.ps1 cannot validate SUPABASE_PROJECT_ID because supabase/config.toml does not declare a local project_id."
}
if ($value -ne $localProjectId) {
throw "env.local.ps1 refuses hosted SUPABASE_PROJECT_ID '$value'. Expected local project_id '$localProjectId' from supabase/config.toml."
}
}
if ($name -eq 'SUPABASE_URL' -and -not [string]::IsNullOrWhiteSpace($value)) {
$isLocalUrl = $false
foreach ($prefix in $localUrlPrefixes) {
if ($value.StartsWith($prefix, [System.StringComparison]::OrdinalIgnoreCase)) {
$isLocalUrl = $true
break
}
}
if (-not $isLocalUrl) {
throw "env.local.ps1 refuses hosted SUPABASE_URL '$value'. Local shells may only target localhost or 127.0.0.1."
}
}
if ($localOnlyRedirectVariables -contains $name -and -not [string]::IsNullOrWhiteSpace($value)) {
$isLocalRedirect = $false
foreach ($prefix in $localUrlPrefixes) {
if ($value.StartsWith($prefix, [System.StringComparison]::OrdinalIgnoreCase)) {
$isLocalRedirect = $true
break
}
}
if (-not $isLocalRedirect) {
throw "env.local.ps1 refuses non-local redirect URI '$value' for '$name'."
}
}
[System.Environment]::SetEnvironmentVariable($name, $value, 'Process')
}