Skip to content

Commit 324a940

Browse files
fix: Detect port from command input or use env variable
1 parent 26ef8c1 commit 324a940

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

‎bin/stackedge.js‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,25 @@ program.command("start <name>")
197197
const command = process.argv.slice(idx + 1).join(" ");
198198
const apps = await loadApps();
199199

200-
// Use custom ports if provided via env, otherwise allocate dynamically
201-
const httpPort = Number(process.env.PORT || await getFreePort());
200+
// Try to detect explicit port in command
201+
let detectedPort = null;
202+
203+
// Matches:
204+
// 127.0.0.1:8000
205+
// :8000
206+
// http.server 8000
207+
const match =
208+
command.match(/127\.0\.0\.1:(\d+)/) ||
209+
command.match(/:(\d{2,5})/) ||
210+
command.match(/\s(\d{2,5})(\s|$)/);
211+
212+
if (match) {
213+
detectedPort = Number(match[1]);
214+
}
215+
216+
const httpPort =
217+
detectedPort ||
218+
(process.env.PORT ? Number(process.env.PORT) : await getFreePort());
202219
const sslPort = process.env.SSL_PORT ? Number(process.env.SSL_PORT) : null;
203220

204221
const ports = [{ virtual: 80, target: httpPort }];

0 commit comments

Comments
 (0)