-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst-deploy.ps1
211 lines (175 loc) · 6.78 KB
/
first-deploy.ps1
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
# first-time-setup.ps1
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Write-Status {
param(
[string]$Message,
[string]$Type = "Info"
)
$colors = @{
Info = "Cyan"
Success = "Green"
Warning = "Yellow"
Error = "Red"
Input = "Magenta"
}
$prefix = switch ($Type) {
"Success" { "[+]" }
"Error" { "[-]" }
"Warning" { "[!]" }
"Info" { "[*]" }
"Input" { "[?]" }
}
Write-Host "$prefix $Message" -ForegroundColor $colors[$Type]
}
function Get-UserInput {
param(
[string]$Prompt,
[switch]$IsPassword
)
Write-Status $Prompt "Input"
if ($IsPassword) {
$secureString = Read-Host -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString)
$string = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
return $string
} else {
return Read-Host
}
}
function Open-BrowserIfConfirmed {
param([string]$Url, [string]$Message)
Write-Status $Message "Info"
$response = Get-UserInput "Would you like to open the browser now? (y/n)"
if ($response -eq 'y') {
Start-Process $Url
Write-Status "Browser opened. Press Enter once you've completed this step." "Input"
Read-Host | Out-Null
}
}
function Install-RequiredTools {
Write-Status "Checking required tools..." "Info"
# Check Node.js
if (-not (Get-Command "node" -ErrorAction SilentlyContinue)) {
Write-Status "Node.js is not installed. Please install it from: https://nodejs.org/" "Error"
Open-BrowserIfConfirmed "https://nodejs.org/" "Opening Node.js download page..."
throw "Node.js is required to continue"
}
# Check Git
if (-not (Get-Command "git" -ErrorAction SilentlyContinue)) {
Write-Status "Git is not installed. Please install it from: https://git-scm.com/" "Error"
Open-BrowserIfConfirmed "https://git-scm.com/downloads" "Opening Git download page..."
throw "Git is required to continue"
}
# Install wrangler globally
Write-Status "Installing Wrangler CLI..." "Info"
npm install -g wrangler
if ($LASTEXITCODE -ne 0) { throw "Failed to install Wrangler" }
}
function Setup-CloudflareAccount {
Write-Status "Setting up Cloudflare account..." "Info"
Write-Status "If you don't have a Cloudflare account, you'll need to create one." "Info"
Open-BrowserIfConfirmed "https://dash.cloudflare.com/sign-up" "Opening Cloudflare signup page..."
# Login to Cloudflare using Wrangler
Write-Status "Logging in to Cloudflare..." "Info"
npx wrangler login
if ($LASTEXITCODE -ne 0) { throw "Failed to login to Cloudflare" }
# Get Account ID
Write-Status "Please get your Cloudflare Account ID from the Cloudflare Dashboard." "Info"
Write-Status "You can find it at https://dash.cloudflare.com/ in the right sidebar." "Info"
Open-BrowserIfConfirmed "https://dash.cloudflare.com/" "Opening Cloudflare Dashboard..."
$accountId = Get-UserInput "Enter your Cloudflare Account ID"
$apiToken = Get-UserInput "Enter your Cloudflare API Token (from https://dash.cloudflare.com/profile/api-tokens)" -IsPassword
# Create .env.local file
Write-Status "Creating .env.local file..." "Info"
$envContent = @"
CLOUDFLARE_API_TOKEN=$apiToken
CLOUDFLARE_ACCOUNT_ID=$accountId
SPOTIFY_CLIENT_ID=
SPOTIFY_CLIENT_SECRET=
SPOTIFY_REDIRECT_URI=
"@
Set-Content -Path ".env.local" -Value $envContent
Write-Status "Created .env.local file with Cloudflare credentials" "Success"
}
function Setup-SpotifyApp {
Write-Status "Setting up Spotify Developer App..." "Info"
Open-BrowserIfConfirmed "https://developer.spotify.com/dashboard" "Opening Spotify Developer Dashboard..."
Write-Status "Create a new app in the Spotify Developer Dashboard" "Info"
Write-Status "Once created, get the Client ID and Client Secret" "Info"
$clientId = Get-UserInput "Enter your Spotify Client ID"
$clientSecret = Get-UserInput "Enter your Spotify Client Secret" -IsPassword
$redirectUri = "https://personal-site.pages.dev/callback"
# Update .env.local file
$envContent = Get-Content ".env.local" -Raw
$envContent = $envContent.Replace("SPOTIFY_CLIENT_ID=", "SPOTIFY_CLIENT_ID=$clientId")
$envContent = $envContent.Replace("SPOTIFY_CLIENT_SECRET=", "SPOTIFY_CLIENT_SECRET=$clientSecret")
$envContent = $envContent.Replace("SPOTIFY_REDIRECT_URI=", "SPOTIFY_REDIRECT_URI=$redirectUri")
Set-Content -Path ".env.local" -Value $envContent
Write-Status "Updated .env.local file with Spotify credentials" "Success"
}
function Setup-Project {
Write-Status "Setting up project..." "Info"
# Install dependencies
Write-Status "Installing project dependencies..." "Info"
npm install
if ($LASTEXITCODE -ne 0) { throw "Failed to install dependencies" }
# Initialize git if not already initialized
if (-not (Test-Path ".git")) {
Write-Status "Initializing git repository..." "Info"
git init
git add .
git commit -m "Initial commit"
}
# Create initial wrangler.toml if it doesn't exist
if (-not (Test-Path "wrangler.toml")) {
Write-Status "Creating wrangler.toml..." "Info"
$wranglerContent = @"
name = "personal-site"
main = "src/worker/index.ts"
compatibility_date = "2024-02-01"
[build]
command = "npm run build:worker"
[env.production]
name = "personal-site"
vars = { ENVIRONMENT = "production" }
[env.development]
name = "personal-site-dev"
vars = { ENVIRONMENT = "development" }
[dev]
port = 8787
"@
Set-Content -Path "wrangler.toml" -Value $wranglerContent
}
}
function Start-FirstTimeSetup {
try {
Write-Status "Starting first-time setup..." "Info"
# Create log directory if it doesn't exist
if (-not (Test-Path "logs")) {
New-Item -ItemType Directory -Path "logs" | Out-Null
}
# Start logging
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$logFile = "logs/setup_$timestamp.log"
Start-Transcript -Path $logFile
# Run setup steps
Install-RequiredTools
Setup-CloudflareAccount
Setup-SpotifyApp
Setup-Project
Write-Status "First-time setup completed successfully!" "Success"
Write-Status "You can now run './deploy-master.ps1' to deploy your site" "Info"
Write-Status "Log file: $logFile" "Info"
}
catch {
Write-Status "Setup failed: $_" "Error"
Write-Status "Check the log file for details: $logFile" "Info"
exit 1
}
finally {
Stop-Transcript
}
}
# Start setup
Start-FirstTimeSetup