1- # deploy.ps1
1+ # deploy-master .ps1
22# Requires -Version 5.1
33Set-StrictMode - Version Latest
44$ErrorActionPreference = ' Stop'
@@ -26,78 +26,127 @@ function Write-Status {
2626 Write-Host " $prefix $Message " - ForegroundColor $colors [$Type ]
2727}
2828
29- function Deploy-Project {
30- try {
31- # Start logging
32- $timestamp = Get-Date - Format " yyyyMMdd_HHmmss"
33- if (-not (Test-Path " logs" )) { New-Item - ItemType Directory - Path " logs" }
34- $logFile = " logs/deploy_$timestamp .log"
35- Start-Transcript - Path $logFile
36-
37- # Check environment variables
38- Write-Status " Checking environment variables..." " Info"
39- $requiredVars = @ (
40- " SPOTIFY_CLIENT_ID" ,
41- " SPOTIFY_CLIENT_SECRET" ,
42- " SPOTIFY_REDIRECT_URI"
43- )
44-
45- $missingVars = @ ()
46- foreach ($var in $requiredVars ) {
47- if (-not (Get-Item env:$var - ErrorAction SilentlyContinue)) {
48- $missingVars += $var
29+ function Test-Environment {
30+ $requiredVars = @ {
31+ " SPOTIFY_CLIENT_ID" = " "
32+ " SPOTIFY_CLIENT_SECRET" = " "
33+ " SPOTIFY_REDIRECT_URI" = " "
34+ " CLOUDFLARE_API_TOKEN" = " For Cloudflare API access"
35+ " CLOUDFLARE_ACCOUNT_ID" = " For Cloudflare account identification"
36+ }
37+
38+ $missingVars = @ ()
39+ foreach ($var in $requiredVars.GetEnumerator ()) {
40+ if (-not (Get-Item env:$ ($var.Key ) - ErrorAction SilentlyContinue)) {
41+ $message = " $ ( $var.Key ) is missing"
42+ if ($var.Value ) {
43+ $message += " : $ ( $var.Value ) "
4944 }
45+ Write-Status $message " Warning"
46+ $missingVars += $var.Key
5047 }
48+ }
49+
50+ # Check for deprecated variables
51+ if (Get-Item env:CF_API_TOKEN - ErrorAction SilentlyContinue) {
52+ Write-Status " CF_API_TOKEN is deprecated. Please use CLOUDFLARE_API_TOKEN instead" " Warning"
53+ # Automatically migrate the value
54+ $env: CLOUDFLARE_API_TOKEN = $env: CF_API_TOKEN
55+ Remove-Item env:CF_API_TOKEN
56+ }
57+
58+ if ($missingVars.Count -gt 0 ) {
59+ throw " Missing required environment variables: $ ( $missingVars -join ' , ' ) "
60+ }
61+ }
5162
52- if ($missingVars.Count -gt 0 ) {
53- Write-Status " Missing environment variables: $ ( $missingVars -join ' , ' ) " " Error"
54- throw " Missing required environment variables"
63+ function Clear-BuildArtifacts {
64+ $paths = @ (" dist" , " .wrangler" , " node_modules/.cache" )
65+ foreach ($path in $paths ) {
66+ if (Test-Path $path ) {
67+ Remove-Item - Recurse - Force $path
68+ Write-Status " Cleaned $path " " Success"
5569 }
70+ }
71+ }
5672
57- # Clean and build
58- Write-Status " Cleaning previous builds..." " Info"
59- if (Test-Path " dist" ) { Remove-Item - Recurse - Force " dist" }
60-
61- Write-Status " Installing dependencies..." " Info"
73+ function Install-Dependencies {
74+ Write-Status " Installing dependencies..." " Info"
75+ npm ci -- prefer- offline -- no- audit
76+ if ($LASTEXITCODE -ne 0 ) {
6277 npm install
78+ if ($LASTEXITCODE -ne 0 ) { throw " Failed to install dependencies" }
79+ }
80+ Write-Status " Dependencies installed successfully" " Success"
81+ }
6382
64- Write-Status " Building project... " " Info "
65- npm run build
83+ function Start-Build {
84+ Write-Status " Building project... " " Info "
6685
67- if ( -not ( Test-Path " dist " )) {
68- throw " Build failed - dist directory not created "
69- }
86+ Write-Status " Building worker... " " Info "
87+ npm run build:worker
88+ if ( $LASTEXITCODE -ne 0 ) { throw " Worker build failed " }
7089
71- # Deploy Worker
72- Write-Status " Deploying Cloudflare Worker..." " Info"
73- $workerDeploy = npx wrangler deploy spotify- worker.ts 2>&1
74- if ($LASTEXITCODE -ne 0 ) {
75- Write-Status " Worker deployment failed: $workerDeploy " " Error"
76- throw " Worker deployment failed"
77- }
78- Write-Status " Worker deployed successfully" " Success"
90+ Write-Status " Building frontend..." " Info"
91+ npm run build
92+ if ($LASTEXITCODE -ne 0 ) { throw " Frontend build failed" }
93+
94+ if (-not (Test-Path " dist" )) { throw " Build failed - dist directory not created" }
95+ Write-Status " Build completed successfully" " Success"
96+ }
7997
80- # Deploy Pages
81- Write-Status " Deploying to Cloudflare Pages..." " Info"
82- $pagesDeploy = npx wrangler pages deploy dist/ 2>&1
83- if ($LASTEXITCODE -ne 0 ) {
84- Write-Status " Pages deployment failed: $pagesDeploy " " Error"
85- throw " Pages deployment failed"
86- }
87- Write-Status " Pages deployed successfully" " Success"
98+ function Deploy-Worker {
99+ Write-Status " Deploying Cloudflare Worker..." " Info"
100+
101+ # Set wrangler environment variables
102+ $env: WRANGLER_AUTH_TOKEN = $env: CLOUDFLARE_API_TOKEN
103+
104+ $deployEnv = if ($env: CI ) { " production" } else { " development" }
105+
106+ npx wrangler deploy src/ worker/ index.ts -- env $deployEnv
107+ if ($LASTEXITCODE -ne 0 ) { throw " Worker deployment failed" }
108+
109+ Write-Status " Worker deployed successfully" " Success"
110+ }
111+
112+ function Deploy-Frontend {
113+ Write-Status " Deploying to Cloudflare Pages..." " Info"
114+
115+ npx wrangler pages deploy dist/
116+ if ($LASTEXITCODE -ne 0 ) { throw " Pages deployment failed" }
117+
118+ Write-Status " Pages deployed successfully" " Success"
119+ }
88120
121+ function Start-Deployment {
122+ try {
123+ # Create log directory if it doesn't exist
124+ if (-not (Test-Path " logs" )) { New-Item - ItemType Directory - Path " logs" }
125+
126+ # Start logging
127+ $timestamp = Get-Date - Format " yyyyMMdd_HHmmss"
128+ $script :logFile = " logs/deploy_$timestamp .log"
129+ Start-Transcript - Path $script :logFile
130+
131+ Test-Environment
132+ Clear-BuildArtifacts
133+ Install-Dependencies
134+ Start-Build
135+ Deploy-Worker
136+ Deploy-Frontend
137+
89138 Write-Status " Deployment completed successfully!" " Success"
90- Write-Status " Log file: $logFile " " Info"
139+ Write-Status " Log file: $script : logFile " " Info"
91140 }
92141 catch {
93142 Write-Status " Deployment failed: $_ " " Error"
94- Write-Status " Check the log file for details: $logFile " " Info"
143+ Write-Status " Check the log file for details: $script : logFile " " Info"
95144 exit 1
96145 }
97146 finally {
98147 Stop-Transcript
99148 }
100149}
101150
102- # Execute deployment
103- Deploy-Project
151+ # Start deployment
152+ Start-Deployment
0 commit comments