openmagic 0.31.7 → 0.31.9

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
@@ -1885,7 +1885,8 @@ var COMMON_DEV_PORTS = [
1885
1885
  ];
1886
1886
  function checkPortSingle(port, host) {
1887
1887
  return new Promise((resolve4) => {
1888
- const socket = createConnection({ port, host, timeout: 500 });
1888
+ const socket = createConnection({ port, host, timeout: 1e3 });
1889
+ socket.unref();
1889
1890
  socket.on("connect", () => {
1890
1891
  socket.destroy();
1891
1892
  resolve4(true);
@@ -2097,7 +2098,7 @@ function ask(question) {
2097
2098
  });
2098
2099
  });
2099
2100
  }
2100
- function waitForPort(port, timeoutMs = 3e4, shouldAbort) {
2101
+ function waitForPort(port, timeoutMs = 6e4, shouldAbort) {
2101
2102
  const start = Date.now();
2102
2103
  return new Promise((resolve4) => {
2103
2104
  const check = async () => {
@@ -2109,11 +2110,13 @@ function waitForPort(port, timeoutMs = 3e4, shouldAbort) {
2109
2110
  resolve4(true);
2110
2111
  return;
2111
2112
  }
2112
- if (Date.now() - start > timeoutMs) {
2113
+ const elapsed = Date.now() - start;
2114
+ if (elapsed > timeoutMs) {
2113
2115
  resolve4(false);
2114
2116
  return;
2115
2117
  }
2116
- setTimeout(check, 500);
2118
+ const interval = elapsed < 1e4 ? 300 : 1e3;
2119
+ setTimeout(check, interval);
2117
2120
  };
2118
2121
  check();
2119
2122
  });
@@ -2170,23 +2173,6 @@ async function healthCheck(proxyPort, _targetPort) {
2170
2173
  }
2171
2174
  console.log("");
2172
2175
  }
2173
- function formatDevServerLine(line) {
2174
- const trimmed = line.trim();
2175
- if (!trimmed) return "";
2176
- if (trimmed.startsWith("Error:") || trimmed.includes("ModuleNotFoundError") || trimmed.includes("Can't resolve")) {
2177
- return chalk.red(` \u2502 ${trimmed}`);
2178
- }
2179
- if (trimmed.includes("EADDRINUSE") || trimmed.includes("address already in use")) {
2180
- return chalk.red(` \u2502 ${trimmed}`) + "\n" + chalk.yellow(" \u2502 \u2192 Port is already in use. Stop the other process or use --port <different-port>");
2181
- }
2182
- if (trimmed.includes("EACCES") || trimmed.includes("permission denied")) {
2183
- return chalk.red(` \u2502 ${trimmed}`) + "\n" + chalk.yellow(" \u2502 \u2192 Permission denied. Try a different port or check file permissions.");
2184
- }
2185
- if (trimmed.includes("Cannot find module") || trimmed.includes("MODULE_NOT_FOUND")) {
2186
- return chalk.red(` \u2502 ${trimmed}`) + "\n" + chalk.yellow(" \u2502 \u2192 Missing dependency. Try running npm install.");
2187
- }
2188
- return chalk.dim(` \u2502 ${trimmed}`);
2189
- }
2190
2176
  var program = new Command();
2191
2177
  program.name("openmagic").description("AI-powered coding toolbar for any web application").version(VERSION2).option("-p, --port <port>", "Dev server port to proxy", "").option(
2192
2178
  "-l, --listen <port>",
@@ -2453,6 +2439,7 @@ async function offerToStartDevServer(expectedPort) {
2453
2439
  }
2454
2440
  }
2455
2441
  let port = expectedPort || chosen.defaultPort;
2442
+ let portChanged = false;
2456
2443
  if (await isPortOpen(port)) {
2457
2444
  const owned = verifyPortOwnership(port, process.cwd());
2458
2445
  if (owned === true) {
@@ -2469,21 +2456,27 @@ async function offerToStartDevServer(expectedPort) {
2469
2456
  chalk.dim(` Starting on port ${altPort} instead.`)
2470
2457
  );
2471
2458
  port = altPort;
2459
+ portChanged = true;
2472
2460
  }
2473
2461
  console.log("");
2474
2462
  console.log(
2475
- chalk.dim(` Starting `) + chalk.cyan(`npm run ${chosen.name}`) + chalk.dim("...")
2463
+ chalk.dim(` Starting `) + chalk.cyan(`npm run ${chosen.name}`) + (portChanged ? chalk.dim(` (port ${port})`) : "") + chalk.dim("...")
2476
2464
  );
2477
2465
  const depsInfo = checkDependenciesInstalled();
2478
2466
  const runCmd = depsInfo.packageManager === "yarn" ? "yarn" : depsInfo.packageManager === "pnpm" ? "pnpm" : depsInfo.packageManager === "bun" ? "bun" : "npm";
2479
2467
  const runArgs = runCmd === "npm" ? ["run", chosen.name] : [chosen.name];
2468
+ if (portChanged) {
2469
+ if (runCmd === "npm") {
2470
+ runArgs.push("--", "--port", String(port));
2471
+ } else {
2472
+ runArgs.push("--port", String(port));
2473
+ }
2474
+ }
2480
2475
  let child;
2481
2476
  try {
2482
2477
  child = spawn(runCmd, runArgs, {
2483
2478
  cwd: process.cwd(),
2484
- stdio: ["ignore", "pipe", "pipe"],
2485
- detached: false,
2486
- shell: true,
2479
+ stdio: "inherit",
2487
2480
  env: {
2488
2481
  ...process.env,
2489
2482
  PORT: String(port),
@@ -2497,49 +2490,16 @@ async function offerToStartDevServer(expectedPort) {
2497
2490
  }
2498
2491
  childProcesses.push(child);
2499
2492
  let childExited = false;
2500
- let detectedPort = null;
2501
- function parsePortFromOutput(line) {
2502
- const clean = line.replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g, "").replace(/\x1b[^a-zA-Z]*[a-zA-Z]/g, "").replace(/[\x00-\x1f\x7f]/g, "");
2503
- const portMatch = clean.match(/https?:\/\/(?:localhost|127\.0\.0\.1|0\.0\.0\.0):(\d+)/);
2504
- if (portMatch && !detectedPort) {
2505
- const p = parseInt(portMatch[1], 10);
2506
- if (p > 0 && p < 65536 && p !== port) {
2507
- detectedPort = p;
2508
- return;
2509
- }
2510
- }
2511
- if (!detectedPort) {
2512
- const fallback = clean.match(/(?:port|Port|PORT)\s+(\d{4,5})/);
2513
- if (fallback) {
2514
- const p = parseInt(fallback[1], 10);
2515
- if (p > 0 && p < 65536 && p !== port) {
2516
- detectedPort = p;
2517
- }
2518
- }
2519
- }
2520
- }
2521
- child.stdout?.on("data", (data) => {
2522
- for (const line of data.toString().trim().split("\n")) {
2523
- parsePortFromOutput(line);
2524
- const formatted = formatDevServerLine(line);
2525
- if (formatted) process.stdout.write(formatted + "\n");
2526
- }
2527
- });
2528
- child.stderr?.on("data", (data) => {
2529
- for (const line of data.toString().trim().split("\n")) {
2530
- parsePortFromOutput(line);
2531
- const formatted = formatDevServerLine(line);
2532
- if (formatted) process.stdout.write(formatted + "\n");
2533
- }
2534
- });
2535
2493
  child.on("error", (err) => {
2536
2494
  childExited = true;
2537
- console.log(chalk.red(` \u2717 Failed to start: ${err.message}`));
2495
+ console.log(chalk.red(`
2496
+ \u2717 Failed to start: ${err.message}`));
2538
2497
  });
2539
2498
  child.on("exit", (code) => {
2540
2499
  childExited = true;
2541
2500
  if (code !== null && code !== 0) {
2542
- console.log(chalk.red(` \u2717 Dev server exited with code ${code}`));
2501
+ console.log(chalk.red(`
2502
+ \u2717 Dev server exited with code ${code}`));
2543
2503
  }
2544
2504
  });
2545
2505
  const cleanup = () => {
@@ -2562,24 +2522,30 @@ async function offerToStartDevServer(expectedPort) {
2562
2522
  process.on("SIGINT", cleanup);
2563
2523
  process.on("SIGTERM", cleanup);
2564
2524
  console.log(
2565
- chalk.dim(` Waiting for dev server...`)
2525
+ chalk.dim(` Waiting for dev server on port ${port}...`)
2566
2526
  );
2567
- const isUp = await waitForPort(port, 3e4, () => {
2568
- if (childExited) return true;
2569
- if (detectedPort) return true;
2570
- return false;
2571
- });
2572
- if (!isUp && detectedPort) {
2573
- const altUp = await isPortOpen(detectedPort);
2574
- if (altUp) {
2575
- console.log(
2576
- chalk.green(` \u2713 Dev server is on port ${detectedPort} (configured in project, not default ${port})`)
2577
- );
2578
- lastDetectedPort = detectedPort;
2579
- return true;
2527
+ const isUp = await waitForPort(port, 6e4, () => childExited);
2528
+ if (isUp) {
2529
+ lastDetectedPort = port;
2530
+ console.log("");
2531
+ return true;
2532
+ }
2533
+ if (!childExited) {
2534
+ const scanPorts = [port, 3e3, 3001, 3002, 5173, 5174, 4200, 8080, 8e3, 4e3, 1234, 4321, 3333, 8081].filter((p, i, a) => a.indexOf(p) === i);
2535
+ for (const scanPort of scanPorts) {
2536
+ if (await isPortOpen(scanPort)) {
2537
+ const owned = verifyPortOwnership(scanPort, process.cwd());
2538
+ if (owned === false) continue;
2539
+ console.log(
2540
+ chalk.green(`
2541
+ \u2713 Dev server found on port ${scanPort}.`)
2542
+ );
2543
+ lastDetectedPort = scanPort;
2544
+ return true;
2545
+ }
2580
2546
  }
2581
2547
  }
2582
- if (childExited && !isUp) {
2548
+ if (childExited) {
2583
2549
  console.log(
2584
2550
  chalk.red(` \u2717 Dev server failed to start.`)
2585
2551
  );
@@ -2603,37 +2569,15 @@ async function offerToStartDevServer(expectedPort) {
2603
2569
  console.log("");
2604
2570
  return false;
2605
2571
  }
2606
- if (!isUp) {
2607
- for (const scanPort of [3e3, 3001, 5173, 5174, 4200, 8080, 8e3, 4e3, 1234, 4321, 3333, 8081]) {
2608
- if (scanPort === port) continue;
2609
- if (await isPortOpen(scanPort)) {
2610
- const owned = verifyPortOwnership(scanPort, process.cwd());
2611
- if (owned === false) continue;
2612
- console.log(
2613
- chalk.green(` \u2713 Dev server found on port ${scanPort}.`)
2614
- );
2615
- lastDetectedPort = scanPort;
2616
- return true;
2617
- }
2618
- }
2619
- console.log(
2620
- chalk.yellow(` \u26A0 Port ${port} didn't open after 30s.`)
2621
- );
2622
- console.log(
2623
- chalk.dim(` The server might use a different port. Check the output above.`)
2624
- );
2625
- console.log("");
2626
- const detected = await detectDevServer();
2627
- if (detected) {
2628
- console.log(
2629
- chalk.green(` \u2713 Found server on port ${detected.port} instead.`)
2630
- );
2631
- return true;
2632
- }
2633
- return false;
2634
- }
2572
+ console.log(
2573
+ chalk.yellow(`
2574
+ \u26A0 Could not find the dev server after 60s.`)
2575
+ );
2576
+ console.log(chalk.dim(` Check the output above for errors.`));
2577
+ console.log(chalk.dim(` Or start the server manually, then run:`));
2578
+ console.log(chalk.cyan(` npx openmagic --port <your-port>`));
2635
2579
  console.log("");
2636
- return true;
2580
+ return false;
2637
2581
  }
2638
2582
  program.parse();
2639
2583
  //# sourceMappingURL=cli.js.map