File size: 4,483 Bytes
5a81b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# WidgeTDC Development Startup Script
# Kills existing processes before starting new ones

$ErrorActionPreference = "SilentlyContinue"

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  WidgeTDC Development Startup" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Check for existing processes on our ports
$port3001 = Get-NetTCPConnection -LocalPort 3001 -State Listen 2>$null
$port8080 = Get-NetTCPConnection -LocalPort 8080 -State Listen 2>$null

if ($port3001 -or $port8080) {
    Write-Host "[WARNING] Existing processes detected!" -ForegroundColor Yellow

    if ($port3001) {
        $pid3001 = $port3001.OwningProcess
        Write-Host "  - Port 3001 (Backend): PID $pid3001" -ForegroundColor Yellow
    }
    if ($port8080) {
        $pid8080 = $port8080.OwningProcess
        Write-Host "  - Port 8080 (Frontend): PID $pid8080" -ForegroundColor Yellow
    }

    Write-Host ""
    Write-Host "Killing existing processes in 3 seconds..." -ForegroundColor Yellow
    Write-Host "(Press Ctrl+C to cancel)" -ForegroundColor DarkGray
    Start-Sleep -Seconds 3

    if ($port3001) {
        Write-Host "  Killing PID $pid3001 (port 3001)..." -ForegroundColor Red
        Stop-Process -Id $pid3001 -Force 2>$null
    }
    if ($port8080) {
        Write-Host "  Killing PID $pid8080 (port 8080)..." -ForegroundColor Red
        Stop-Process -Id $pid8080 -Force 2>$null
    }

    Start-Sleep -Seconds 1
    Write-Host "  Done!" -ForegroundColor Green
    Write-Host ""
}

# Also kill any node processes running our specific files
$nodeProcesses = Get-Process -Name "node" 2>$null | Where-Object {
    $_.CommandLine -like "*tsx*" -or $_.CommandLine -like "*vite*"
}

if ($nodeProcesses) {
    Write-Host "[INFO] Found orphan node processes, cleaning up..." -ForegroundColor Yellow
    $nodeProcesses | ForEach-Object {
        Write-Host "  Killing PID $($_.Id)..." -ForegroundColor DarkGray
        Stop-Process -Id $_.Id -Force 2>$null
    }
    Start-Sleep -Seconds 1
}

Write-Host ""
Write-Host "Starting services..." -ForegroundColor Green
Write-Host ""

# Start Backend
Write-Host "[1/2] Starting Backend (port 3001)..." -ForegroundColor Cyan
$backendJob = Start-Job -ScriptBlock {
    Set-Location $using:PWD
    Set-Location apps/backend
    npm run dev 2>&1
}

Start-Sleep -Seconds 2

# Start Frontend
Write-Host "[2/2] Starting Frontend (port 8080)..." -ForegroundColor Cyan
$frontendJob = Start-Job -ScriptBlock {
    Set-Location $using:PWD
    Set-Location apps/matrix-frontend
    npm run dev 2>&1
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "  Services Starting!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "  Frontend: http://localhost:8080" -ForegroundColor White
Write-Host "  Backend:  http://localhost:3001" -ForegroundColor White
Write-Host "  Health:   http://localhost:3001/health" -ForegroundColor DarkGray
Write-Host ""
Write-Host "Press Ctrl+C to stop all services" -ForegroundColor DarkGray
Write-Host ""

# Wait and show output
try {
    while ($true) {
        # Check if jobs are still running
        $backendState = (Get-Job -Id $backendJob.Id).State
        $frontendState = (Get-Job -Id $frontendJob.Id).State

        # Get and display any new output
        Receive-Job -Job $backendJob -ErrorAction SilentlyContinue | ForEach-Object {
            Write-Host "[BACKEND] $_" -ForegroundColor DarkCyan
        }
        Receive-Job -Job $frontendJob -ErrorAction SilentlyContinue | ForEach-Object {
            Write-Host "[FRONTEND] $_" -ForegroundColor DarkMagenta
        }

        Start-Sleep -Milliseconds 500
    }
}
finally {
    Write-Host ""
    Write-Host "Stopping services..." -ForegroundColor Yellow
    Stop-Job -Job $backendJob 2>$null
    Stop-Job -Job $frontendJob 2>$null
    Remove-Job -Job $backendJob 2>$null
    Remove-Job -Job $frontendJob 2>$null

    # Kill any remaining processes on our ports
    $port3001 = Get-NetTCPConnection -LocalPort 3001 -State Listen 2>$null
    $port8080 = Get-NetTCPConnection -LocalPort 8080 -State Listen 2>$null
    if ($port3001) { Stop-Process -Id $port3001.OwningProcess -Force 2>$null }
    if ($port8080) { Stop-Process -Id $port8080.OwningProcess -Force 2>$null }

    Write-Host "Goodbye!" -ForegroundColor Green
}