openmagic 0.31.6 → 0.31.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -1968,6 +1968,16 @@ async function detectDevServer(cwd = process.cwd()) {
1968
1968
  async function isPortOpen(port) {
1969
1969
  return checkPort(port);
1970
1970
  }
1971
+ async function findAvailablePort(startPort) {
1972
+ let port = startPort;
1973
+ while (await isPortOpen(port)) {
1974
+ port++;
1975
+ if (port > startPort + 100) {
1976
+ throw new Error(`Could not find an available port near ${startPort}`);
1977
+ }
1978
+ }
1979
+ return port;
1980
+ }
1971
1981
  var FRAMEWORK_PATTERNS = [
1972
1982
  { match: /\bnext\b/, framework: "Next.js", defaultPort: 3e3 },
1973
1983
  { match: /\bvite\b/, framework: "Vite", defaultPort: 5173 },
@@ -2442,14 +2452,40 @@ async function offerToStartDevServer(expectedPort) {
2442
2452
  chosen = scripts[idx];
2443
2453
  }
2444
2454
  }
2445
- const port = expectedPort || chosen.defaultPort;
2455
+ let port = expectedPort || chosen.defaultPort;
2456
+ let portChanged = false;
2457
+ if (await isPortOpen(port)) {
2458
+ const owned = verifyPortOwnership(port, process.cwd());
2459
+ if (owned === true) {
2460
+ console.log(chalk.green(` \u2713 Dev server already running on port ${port}`));
2461
+ lastDetectedPort = port;
2462
+ return true;
2463
+ }
2464
+ const altPort = await findAvailablePort(port + 1);
2465
+ console.log("");
2466
+ console.log(
2467
+ chalk.yellow(` \u26A0 Port ${port} is already in use by another process.`)
2468
+ );
2469
+ console.log(
2470
+ chalk.dim(` Starting on port ${altPort} instead.`)
2471
+ );
2472
+ port = altPort;
2473
+ portChanged = true;
2474
+ }
2446
2475
  console.log("");
2447
2476
  console.log(
2448
- chalk.dim(` Starting `) + chalk.cyan(`npm run ${chosen.name}`) + chalk.dim("...")
2477
+ chalk.dim(` Starting `) + chalk.cyan(`npm run ${chosen.name}`) + (portChanged ? chalk.dim(` (port ${port})`) : "") + chalk.dim("...")
2449
2478
  );
2450
2479
  const depsInfo = checkDependenciesInstalled();
2451
2480
  const runCmd = depsInfo.packageManager === "yarn" ? "yarn" : depsInfo.packageManager === "pnpm" ? "pnpm" : depsInfo.packageManager === "bun" ? "bun" : "npm";
2452
2481
  const runArgs = runCmd === "npm" ? ["run", chosen.name] : [chosen.name];
2482
+ if (portChanged) {
2483
+ if (runCmd === "npm") {
2484
+ runArgs.push("--", "--port", String(port));
2485
+ } else {
2486
+ runArgs.push("--port", String(port));
2487
+ }
2488
+ }
2453
2489
  let child;
2454
2490
  try {
2455
2491
  child = spawn(runCmd, runArgs, {
@@ -2473,10 +2509,14 @@ async function offerToStartDevServer(expectedPort) {
2473
2509
  let detectedPort = null;
2474
2510
  function parsePortFromOutput(line) {
2475
2511
  const clean = line.replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g, "").replace(/\x1b[^a-zA-Z]*[a-zA-Z]/g, "").replace(/[\x00-\x1f\x7f]/g, "");
2476
- const portMatch = clean.match(/https?:\/\/(?:localhost|127\.0\.0\.1|0\.0\.0\.0):(\d+)/);
2512
+ const portMatch = clean.match(/https?:\/\/(?:localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1?\]):(\d+)/);
2477
2513
  if (portMatch && !detectedPort) {
2478
2514
  const p = parseInt(portMatch[1], 10);
2479
- if (p > 0 && p < 65536 && p !== port) {
2515
+ if (p > 0 && p < 65536) {
2516
+ if (p === port) {
2517
+ detectedPort = p;
2518
+ return;
2519
+ }
2480
2520
  detectedPort = p;
2481
2521
  return;
2482
2522
  }
@@ -2537,17 +2577,19 @@ async function offerToStartDevServer(expectedPort) {
2537
2577
  console.log(
2538
2578
  chalk.dim(` Waiting for dev server...`)
2539
2579
  );
2540
- const isUp = await waitForPort(port, 3e4, () => {
2580
+ const isUp = await waitForPort(port, 6e4, () => {
2541
2581
  if (childExited) return true;
2542
2582
  if (detectedPort) return true;
2543
2583
  return false;
2544
2584
  });
2545
- if (!isUp && detectedPort) {
2546
- const altUp = await isPortOpen(detectedPort);
2585
+ if (detectedPort) {
2586
+ const altUp = detectedPort === port ? isUp : await isPortOpen(detectedPort);
2547
2587
  if (altUp) {
2548
- console.log(
2549
- chalk.green(` \u2713 Dev server is on port ${detectedPort} (configured in project, not default ${port})`)
2550
- );
2588
+ if (detectedPort !== port) {
2589
+ console.log(
2590
+ chalk.green(` \u2713 Dev server is on port ${detectedPort} (configured in project, not default ${port})`)
2591
+ );
2592
+ }
2551
2593
  lastDetectedPort = detectedPort;
2552
2594
  return true;
2553
2595
  }
@@ -2580,6 +2622,8 @@ async function offerToStartDevServer(expectedPort) {
2580
2622
  for (const scanPort of [3e3, 3001, 5173, 5174, 4200, 8080, 8e3, 4e3, 1234, 4321, 3333, 8081]) {
2581
2623
  if (scanPort === port) continue;
2582
2624
  if (await isPortOpen(scanPort)) {
2625
+ const owned = verifyPortOwnership(scanPort, process.cwd());
2626
+ if (owned === false) continue;
2583
2627
  console.log(
2584
2628
  chalk.green(` \u2713 Dev server found on port ${scanPort}.`)
2585
2629
  );
@@ -2588,7 +2632,7 @@ async function offerToStartDevServer(expectedPort) {
2588
2632
  }
2589
2633
  }
2590
2634
  console.log(
2591
- chalk.yellow(` \u26A0 Port ${port} didn't open after 30s.`)
2635
+ chalk.yellow(` \u26A0 Port ${port} didn't open after 60s.`)
2592
2636
  );
2593
2637
  console.log(
2594
2638
  chalk.dim(` The server might use a different port. Check the output above.`)