openmagic 0.25.4 → 0.26.0

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
@@ -1447,7 +1447,7 @@ async function handleLlmChat(params, onChunk, onDone, onError) {
1447
1447
  }
1448
1448
 
1449
1449
  // src/server.ts
1450
- var VERSION = "0.25.4";
1450
+ var VERSION = "0.26.0";
1451
1451
  var __dirname = dirname2(fileURLToPath(import.meta.url));
1452
1452
  function attachOpenMagic(httpServer, roots) {
1453
1453
  function handleRequest(req, res) {
@@ -1980,7 +1980,8 @@ process.on("uncaughtException", (err) => {
1980
1980
  process.exit(1);
1981
1981
  });
1982
1982
  var childProcesses = [];
1983
- var VERSION2 = "0.25.4";
1983
+ var lastDetectedPort = null;
1984
+ var VERSION2 = "0.26.0";
1984
1985
  function ask(question) {
1985
1986
  const rl = createInterface({ input: process.stdin, output: process.stdout });
1986
1987
  return new Promise((resolve3) => {
@@ -2105,10 +2106,14 @@ program.name("openmagic").description("AI-powered coding toolbar for any web app
2105
2106
  if (!started) {
2106
2107
  process.exit(1);
2107
2108
  }
2108
- const recheck = await detectDevServer();
2109
- if (recheck) {
2110
- targetPort = recheck.port;
2111
- targetHost = recheck.host;
2109
+ if (lastDetectedPort) {
2110
+ targetPort = lastDetectedPort;
2111
+ } else {
2112
+ const recheck = await detectDevServer();
2113
+ if (recheck) {
2114
+ targetPort = recheck.port;
2115
+ targetHost = recheck.host;
2116
+ }
2112
2117
  }
2113
2118
  }
2114
2119
  } else {
@@ -2122,15 +2127,19 @@ program.name("openmagic").description("AI-powered coding toolbar for any web app
2122
2127
  if (!started) {
2123
2128
  process.exit(1);
2124
2129
  }
2125
- const redetected = await detectDevServer();
2126
- if (!redetected) {
2127
- console.log(chalk.red(" \u2717 Could not detect the dev server after starting."));
2128
- console.log(chalk.dim(" Try specifying the port: npx openmagic --port 3000"));
2129
- console.log("");
2130
- process.exit(1);
2130
+ if (lastDetectedPort) {
2131
+ targetPort = lastDetectedPort;
2132
+ } else {
2133
+ const redetected = await detectDevServer();
2134
+ if (!redetected) {
2135
+ console.log(chalk.red(" \u2717 Could not detect the dev server after starting."));
2136
+ console.log(chalk.dim(" Try specifying the port: npx openmagic --port 3000"));
2137
+ console.log("");
2138
+ process.exit(1);
2139
+ }
2140
+ targetPort = redetected.port;
2141
+ targetHost = redetected.host;
2131
2142
  }
2132
- targetPort = redetected.port;
2133
- targetHost = redetected.host;
2134
2143
  }
2135
2144
  }
2136
2145
  console.log(
@@ -2152,15 +2161,15 @@ program.name("openmagic").description("AI-powered coding toolbar for any web app
2152
2161
  }
2153
2162
  const proxyServer = createProxyServer(targetHost, targetPort, roots);
2154
2163
  proxyServer.listen(proxyPort, "127.0.0.1", async () => {
2164
+ const proxyUrl = `http://localhost:${proxyPort}`;
2165
+ console.log("");
2166
+ console.log(chalk.bold.green(" Ready!"));
2155
2167
  console.log("");
2156
2168
  console.log(
2157
- chalk.bold.green(` Proxy running at \u2192 `) + chalk.bold.underline.cyan(`http://localhost:${proxyPort}`)
2169
+ chalk.bold(" \u2192 ") + chalk.bold.underline.cyan(proxyUrl)
2158
2170
  );
2159
2171
  console.log("");
2160
2172
  await healthCheck(proxyPort, targetPort);
2161
- console.log(
2162
- chalk.dim(" Open the URL above in your browser to start.")
2163
- );
2164
2173
  console.log(chalk.dim(" Press Ctrl+C to stop."));
2165
2174
  console.log(
2166
2175
  chalk.dim(" Errors below are from your dev server, not OpenMagic.")
@@ -2301,14 +2310,26 @@ async function offerToStartDevServer(expectedPort) {
2301
2310
  }
2302
2311
  childProcesses.push(child);
2303
2312
  let childExited = false;
2313
+ let detectedPort = null;
2314
+ function parsePortFromOutput(line) {
2315
+ const portMatch = line.match(/https?:\/\/(?:localhost|127\.0\.0\.1|0\.0\.0\.0):(\d+)/);
2316
+ if (portMatch && !detectedPort) {
2317
+ const p = parseInt(portMatch[1], 10);
2318
+ if (p > 0 && p < 65536 && p !== port) {
2319
+ detectedPort = p;
2320
+ }
2321
+ }
2322
+ }
2304
2323
  child.stdout?.on("data", (data) => {
2305
2324
  for (const line of data.toString().trim().split("\n")) {
2325
+ parsePortFromOutput(line);
2306
2326
  const formatted = formatDevServerLine(line);
2307
2327
  if (formatted) process.stdout.write(formatted + "\n");
2308
2328
  }
2309
2329
  });
2310
2330
  child.stderr?.on("data", (data) => {
2311
2331
  for (const line of data.toString().trim().split("\n")) {
2332
+ parsePortFromOutput(line);
2312
2333
  const formatted = formatDevServerLine(line);
2313
2334
  if (formatted) process.stdout.write(formatted + "\n");
2314
2335
  }
@@ -2343,9 +2364,23 @@ async function offerToStartDevServer(expectedPort) {
2343
2364
  process.on("SIGINT", cleanup);
2344
2365
  process.on("SIGTERM", cleanup);
2345
2366
  console.log(
2346
- chalk.dim(` Waiting for port ${port}...`)
2367
+ chalk.dim(` Waiting for dev server...`)
2347
2368
  );
2348
- const isUp = await waitForPort(port, 3e4, () => childExited);
2369
+ const isUp = await waitForPort(port, 3e4, () => {
2370
+ if (childExited) return true;
2371
+ if (detectedPort) return true;
2372
+ return false;
2373
+ });
2374
+ if (!isUp && detectedPort) {
2375
+ const altUp = await isPortOpen(detectedPort);
2376
+ if (altUp) {
2377
+ console.log(
2378
+ chalk.green(` \u2713 Dev server is on port ${detectedPort} (configured in project, not default ${port})`)
2379
+ );
2380
+ lastDetectedPort = detectedPort;
2381
+ return true;
2382
+ }
2383
+ }
2349
2384
  if (childExited && !isUp) {
2350
2385
  console.log(
2351
2386
  chalk.red(` \u2717 Dev server exited before it was ready.`)