-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStart-DevServer.ps1
More file actions
181 lines (156 loc) · 5.45 KB
/
Copy pathStart-DevServer.ps1
File metadata and controls
181 lines (156 loc) · 5.45 KB
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
<#
.SYNOPSIS
Quick dev server launcher based on project type.
.DESCRIPTION
Auto-detects project type and starts the appropriate development server.
Supports Node.js (npm), PHP built-in server, Python, and more.
.PARAMETER Type
Force a specific server type: node, php, python, laravel.
.PARAMETER Port
Override the default port.
.PARAMETER Host
Host to bind to (default: localhost).
.EXAMPLE
serve # Auto-detect and start
serve -Port 8080 # Override port
serve php # Force PHP built-in server
serve node # Force npm/node
#>
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[ValidateSet('node', 'php', 'python', 'laravel', 'auto', '')]
[string]$Type = 'auto',
[int]$Port,
[string]$BindHost = 'localhost'
)
# Detect project type
function Get-ProjectType {
# Laravel (check first, before generic PHP)
if (Test-Path '.\artisan') {
return 'laravel'
}
# Node.js
if (Test-Path '.\package.json') {
return 'node'
}
# PHP
if ((Test-Path '.\index.php') -or (Test-Path '.\public\index.php') -or (Test-Path '.\composer.json')) {
return 'php'
}
# Python
if ((Test-Path '.\manage.py') -or (Test-Path '.\app.py') -or (Test-Path '.\main.py')) {
return 'python'
}
return $null
}
# Auto-detect if needed
if ($Type -eq 'auto' -or [string]::IsNullOrEmpty($Type)) {
$Type = Get-ProjectType
if (-not $Type) {
Write-Host ""
Write-Host "Could not detect project type." -ForegroundColor Red
Write-Host "Specify type: serve node | serve php | serve python | serve laravel" -ForegroundColor Yellow
Write-Host ""
exit 1
}
Write-Host ""
Write-Host "Detected: " -NoNewline -ForegroundColor Cyan
Write-Host $Type -ForegroundColor Green
}
# Set default ports
$defaultPorts = @{
'node' = 3000
'php' = 8000
'python' = 8000
'laravel' = 8000
}
if (-not $Port) {
$Port = $defaultPorts[$Type]
}
# Check if port is in use
$portInUse = Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue
if ($portInUse) {
Write-Host ""
Write-Host "Port $Port is already in use!" -ForegroundColor Red
Write-Host "Use " -NoNewline -ForegroundColor Gray
Write-Host "port $Port" -NoNewline -ForegroundColor Yellow
Write-Host " to see what's using it, or " -NoNewline -ForegroundColor Gray
Write-Host "serve -Port <number>" -NoNewline -ForegroundColor Yellow
Write-Host " to use a different port." -ForegroundColor Gray
Write-Host ""
exit 1
}
Write-Host ""
Write-Host "Starting $Type dev server on " -NoNewline -ForegroundColor Cyan
Write-Host "http://${BindHost}:${Port}" -ForegroundColor Yellow
Write-Host "Press Ctrl+C to stop" -ForegroundColor DarkGray
Write-Host ""
switch ($Type) {
'node' {
# Check for package.json scripts
if (Test-Path '.\package.json') {
$pkg = Get-Content '.\package.json' -Raw | ConvertFrom-Json -ErrorAction SilentlyContinue
# Try common dev script names
$devScripts = @('dev', 'start', 'serve', 'develop')
$script = $devScripts | Where-Object { $pkg.scripts.$_ } | Select-Object -First 1
if ($script) {
Write-Host "Running: npm run $script" -ForegroundColor DarkGray
Write-Host ""
npm run $script
} else {
Write-Host "No dev script found in package.json" -ForegroundColor Yellow
Write-Host "Available scripts: $($pkg.scripts.PSObject.Properties.Name -join ', ')" -ForegroundColor DarkGray
}
} else {
Write-Host "No package.json found" -ForegroundColor Red
}
}
'php' {
# Determine document root
$docRoot = '.'
if (Test-Path '.\public') {
$docRoot = '.\public'
} elseif (Test-Path '.\web') {
$docRoot = '.\web'
} elseif (Test-Path '.\htdocs') {
$docRoot = '.\htdocs'
}
Write-Host "Document root: $docRoot" -ForegroundColor DarkGray
Write-Host ""
php -S "${BindHost}:${Port}" -t $docRoot
}
'laravel' {
Write-Host "Running: php artisan serve --port=$Port" -ForegroundColor DarkGray
Write-Host ""
php artisan serve --host=$BindHost --port=$Port
}
'python' {
# Django
if (Test-Path '.\manage.py') {
Write-Host "Running: python manage.py runserver ${Port}" -ForegroundColor DarkGray
Write-Host ""
python manage.py runserver "${BindHost}:${Port}"
}
# Flask
elseif (Test-Path '.\app.py') {
Write-Host "Running: flask run --port $Port" -ForegroundColor DarkGray
Write-Host ""
$env:FLASK_APP = 'app.py'
$env:FLASK_ENV = 'development'
flask run --host=$BindHost --port=$Port
}
# FastAPI / Uvicorn
elseif (Test-Path '.\main.py') {
Write-Host "Running: uvicorn main:app --port $Port" -ForegroundColor DarkGray
Write-Host ""
uvicorn main:app --host $BindHost --port $Port --reload
}
else {
# Simple Python HTTP server
Write-Host "Running: python -m http.server $Port" -ForegroundColor DarkGray
Write-Host ""
python -m http.server $Port --bind $BindHost
}
}
}