diff --git a/skills/dev-browser/scripts/start-server.ts b/skills/dev-browser/scripts/start-server.ts index e130a27..07a31b5 100644 --- a/skills/dev-browser/scripts/start-server.ts +++ b/skills/dev-browser/scripts/start-server.ts @@ -72,14 +72,19 @@ try { console.log("You may need to run: npx playwright install chromium"); } +// Parse port configuration from environment +const port = parseInt(process.env.PORT || "9222", 10); +const cdpPort = process.env.CDP_PORT ? parseInt(process.env.CDP_PORT, 10) : undefined; +const effectiveCdpPort = cdpPort || port + 1; + // Check if server is already running console.log("Checking for existing servers..."); try { - const res = await fetch("http://localhost:9222", { + const res = await fetch(`http://localhost:${port}`, { signal: AbortSignal.timeout(1000), }); if (res.ok) { - console.log("Server already running on port 9222"); + console.log(`Server already running on port ${port}`); process.exit(0); } } catch { @@ -87,11 +92,11 @@ try { } // Clean up stale CDP port if HTTP server isn't running (crash recovery) -// This handles the case where Node crashed but Chrome is still running on 9223 +// This handles the case where Node crashed but Chrome is still running try { - const pid = execSync("lsof -ti:9223", { encoding: "utf-8" }).trim(); + const pid = execSync(`lsof -ti:${effectiveCdpPort}`, { encoding: "utf-8" }).trim(); if (pid) { - console.log(`Cleaning up stale Chrome process on CDP port 9223 (PID: ${pid})`); + console.log(`Cleaning up stale Chrome process on CDP port ${effectiveCdpPort} (PID: ${pid})`); execSync(`kill -9 ${pid}`); } } catch { @@ -101,7 +106,8 @@ try { console.log("Starting dev browser server..."); const headless = process.env.HEADLESS === "true"; const server = await serve({ - port: 9222, + port, + cdpPort, headless, profileDir, }); diff --git a/skills/dev-browser/server.sh b/skills/dev-browser/server.sh index 50369a4..ef63ebe 100755 --- a/skills/dev-browser/server.sh +++ b/skills/dev-browser/server.sh @@ -8,9 +8,14 @@ cd "$SCRIPT_DIR" # Parse command line arguments HEADLESS=false +PORT=9222 +CDP_PORT="" + while [[ "$#" -gt 0 ]]; do case $1 in --headless) HEADLESS=true ;; + --port) PORT="$2"; shift ;; + --cdp-port) CDP_PORT="$2"; shift ;; *) echo "Unknown parameter: $1"; exit 1 ;; esac shift @@ -19,6 +24,8 @@ done echo "Installing dependencies..." npm install -echo "Starting dev-browser server..." +echo "Starting dev-browser server on port $PORT..." export HEADLESS=$HEADLESS +export PORT=$PORT +export CDP_PORT=$CDP_PORT npx tsx scripts/start-server.ts