File size: 3,114 Bytes
2c38fa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
$ErrorActionPreference = 'Stop'

# Paths (relative to this script location)
$Root = Split-Path -Parent $MyInvocation.MyCommand.Path
$Backend = Join-Path $Root 'backend'
$Frontend = Join-Path $Root 'frontend'
$VenvDir = Join-Path $Backend '.venv'
$VenvPy = Join-Path $VenvDir 'Scripts/python.exe'

function Resolve-PythonLauncher {
  # First, try the py launcher without version specifier
  $pyCmd = Get-Command py -ErrorAction SilentlyContinue
  if ($pyCmd) {
    try {
      & py -c "import sys; exit(0 if sys.version_info >= (3, 10) else 1)" 2>$null
      if ($LASTEXITCODE -eq 0) {
        return "py"
      }
    } catch {}
  }

  # Try python command directly
  $pythonCmd = Get-Command python -ErrorAction SilentlyContinue
  if ($pythonCmd) {
    try {
      & python -c "import sys; exit(0 if sys.version_info >= (3, 10) else 1)" 2>$null
      if ($LASTEXITCODE -eq 0) {
        return "python"
      }
    } catch {}
  }

  throw "Python tidak ditemukan di PATH. Install Python 3.10+ dan centang 'Install launcher for all users / Add to PATH'."
}

$PyLauncher = Resolve-PythonLauncher
Write-Host "[DEV] Python launcher yang digunakan: $PyLauncher" -ForegroundColor Cyan

Write-Host "[DEV] Root: $Root" -ForegroundColor Cyan

# 1) Ensure Python venv exists (Python 3.12 recommended)
if (-not (Test-Path $VenvPy)) {
  Write-Host "[DEV] Membuat virtualenv backend (.venv)" -ForegroundColor Yellow
  pushd $Backend
  Invoke-Expression "$PyLauncher -m venv .venv"
  popd
}

# 2) Sinkronisasi dependency backend setiap menjalankan dev
Write-Host "[DEV] Memastikan dependency backend terpasang (pip install -r requirements.txt)" -ForegroundColor Yellow
& $VenvPy -m pip install --upgrade pip | Out-Null
& $VenvPy -m pip install -r (Join-Path $Backend 'requirements.txt')

# 3) Start backend in a new PowerShell window (blok terpisah)
Write-Host "[DEV] Menjalankan backend (uvicorn)..." -ForegroundColor Green
$BackendProc = Start-Process -FilePath $VenvPy `
  -ArgumentList @('-m','uvicorn','backend.main:app','--reload','--port','8000') `
  -WorkingDirectory $Root `
  -WindowStyle Normal `
  -PassThru

# 3b) Wait for backend health 
$healthy = $false
for ($i = 0; $i -lt 20; $i++) {
  try {
    $res = Invoke-WebRequest -Uri 'http://localhost:8000/health' -UseBasicParsing -TimeoutSec 2
    if ($res.StatusCode -eq 200) { $healthy = $true; break }
  } catch { }
  Start-Sleep -Milliseconds 800
}
if (-not $healthy) {
  Write-Host "[DEV][WARN] Backend belum merespons di http://localhost:8000/health" -ForegroundColor Yellow
  Write-Host "             Jika jendela backend menutup sendiri, coba jalankan manual:" -ForegroundColor Yellow
  Write-Host "             cd `"$Backend`"; .\\.venv\\Scripts\\Activate.ps1; python -m uvicorn backend.main:app --reload --port 8000" -ForegroundColor Yellow
}

# 4) Start frontend dev server in current window
Write-Host "[DEV] Menjalankan frontend (Vite 5175)" -ForegroundColor Green
pushd $Frontend
# Install deps frontend setiap kali
Write-Host "[DEV] Menginstal / menyelaraskan dependency frontend (npm install)" -ForegroundColor Yellow
npm install
npm run dev:5175
popd