deploy-npm.ps1 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # PowerShell Deployment Script using npm
  2. Write-Host "Starting PWA deployment with npm..." -ForegroundColor Green
  3. try {
  4. # Pull latest code
  5. Write-Host "Pulling latest code..." -ForegroundColor Yellow
  6. & "C:\Program Files\Git\bin\git.exe" pull origin main
  7. if ($LASTEXITCODE -ne 0) {
  8. throw "Git pull failed"
  9. }
  10. # Clean old build files
  11. Write-Host "Cleaning old build files..." -ForegroundColor Yellow
  12. if (Test-Path "dist") {
  13. Remove-Item -Recurse -Force "dist"
  14. }
  15. # Clean node_modules if exists
  16. if (Test-Path "node_modules") {
  17. Write-Host "Cleaning node_modules..." -ForegroundColor Yellow
  18. Remove-Item -Recurse -Force "node_modules" -ErrorAction SilentlyContinue
  19. }
  20. # Remove lock files
  21. if (Test-Path "package-lock.json") {
  22. Remove-Item "package-lock.json" -ErrorAction SilentlyContinue
  23. }
  24. if (Test-Path "yarn.lock") {
  25. Remove-Item "yarn.lock" -ErrorAction SilentlyContinue
  26. }
  27. # Install dependencies with npm
  28. Write-Host "Installing dependencies with npm..." -ForegroundColor Yellow
  29. npm install
  30. if ($LASTEXITCODE -ne 0) {
  31. throw "Dependency installation failed"
  32. }
  33. # Build production version
  34. Write-Host "Building production version..." -ForegroundColor Yellow
  35. npm run build
  36. if ($LASTEXITCODE -ne 0) {
  37. throw "Build failed"
  38. }
  39. # Check build results
  40. Write-Host "Checking build results..." -ForegroundColor Yellow
  41. if (-not (Test-Path "dist")) {
  42. throw "Build failed, dist directory does not exist"
  43. }
  44. # Deploy to server
  45. Write-Host "Deploying to server..." -ForegroundColor Yellow
  46. # Try different upload methods
  47. $uploadSuccess = $false
  48. # Try rsync first
  49. try {
  50. & "C:\Program Files\Git\bin\rsync.exe" --version | Out-Null
  51. Write-Host "Using rsync to upload..." -ForegroundColor Cyan
  52. & "C:\Program Files\Git\bin\rsync.exe" --exclude='node_modules/' -ravzh --delete -e "ssh -o StrictHostKeyChecking=no" ./dist/ junma-server:/var/www/junma-show/
  53. if ($LASTEXITCODE -eq 0) {
  54. $uploadSuccess = $true
  55. }
  56. }
  57. catch {
  58. Write-Host "rsync not available, trying scp..." -ForegroundColor Yellow
  59. }
  60. # Try scp if rsync failed
  61. if (-not $uploadSuccess) {
  62. try {
  63. # Try different scp paths
  64. $scpPaths = @(
  65. "C:\Program Files\Git\usr\bin\scp.exe",
  66. "C:\Program Files\Git\bin\scp.exe",
  67. "scp"
  68. )
  69. foreach ($scpPath in $scpPaths) {
  70. try {
  71. if ($scpPath -eq "scp") {
  72. & $scpPath -r -o StrictHostKeyChecking=no ./dist/* junma-server:/var/www/junma-show/
  73. } else {
  74. if (Test-Path $scpPath) {
  75. & $scpPath -r -o StrictHostKeyChecking=no ./dist/* junma-server:/var/www/junma-show/
  76. } else {
  77. continue
  78. }
  79. }
  80. if ($LASTEXITCODE -eq 0) {
  81. Write-Host "Upload successful with: $scpPath" -ForegroundColor Green
  82. $uploadSuccess = $true
  83. break
  84. }
  85. }
  86. catch {
  87. continue
  88. }
  89. }
  90. }
  91. catch {
  92. Write-Host "All upload methods failed" -ForegroundColor Red
  93. }
  94. }
  95. if (-not $uploadSuccess) {
  96. throw "File upload failed - no working upload method found"
  97. }
  98. if ($LASTEXITCODE -ne 0) {
  99. throw "File upload failed"
  100. }
  101. Write-Host "Deployment completed!" -ForegroundColor Green
  102. Write-Host "Application URL: https://your-domain.com" -ForegroundColor Cyan
  103. Write-Host "PWA features enabled" -ForegroundColor Cyan
  104. }
  105. catch {
  106. Write-Host "Deployment failed: $($_.Exception.Message)" -ForegroundColor Red
  107. exit 1
  108. }