sparkecoder 0.1.7 → 0.1.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/index.js CHANGED
@@ -4403,6 +4403,33 @@ async function findWebPort(preferredPort) {
4403
4403
  }
4404
4404
  return { port: preferredPort, alreadyRunning: false };
4405
4405
  }
4406
+ function hasProductionBuild(webDir) {
4407
+ const buildIdPath = join3(webDir, ".next", "BUILD_ID");
4408
+ return existsSync7(buildIdPath);
4409
+ }
4410
+ function runCommand(command, args, cwd, env) {
4411
+ return new Promise((resolve7) => {
4412
+ const child = spawn(command, args, {
4413
+ cwd,
4414
+ stdio: ["ignore", "pipe", "pipe"],
4415
+ env,
4416
+ shell: true
4417
+ });
4418
+ let output = "";
4419
+ child.stdout?.on("data", (data) => {
4420
+ output += data.toString();
4421
+ });
4422
+ child.stderr?.on("data", (data) => {
4423
+ output += data.toString();
4424
+ });
4425
+ child.on("close", (code) => {
4426
+ resolve7({ success: code === 0, output });
4427
+ });
4428
+ child.on("error", (err) => {
4429
+ resolve7({ success: false, output: err.message });
4430
+ });
4431
+ });
4432
+ }
4406
4433
  async function startWebUI(apiPort, webPort = DEFAULT_WEB_PORT, quiet = false) {
4407
4434
  const webDir = getWebDirectory();
4408
4435
  if (!webDir) {
@@ -4414,39 +4441,90 @@ async function startWebUI(apiPort, webPort = DEFAULT_WEB_PORT, quiet = false) {
4414
4441
  if (!quiet) console.log(` \u2713 Web UI already running at http://localhost:${actualPort}`);
4415
4442
  return { process: null, port: actualPort };
4416
4443
  }
4417
- const useNpm = existsSync7(join3(webDir, "package-lock.json"));
4418
- const command = useNpm ? "npm" : "npx";
4419
- const args = useNpm ? ["run", "dev", "--", "-p", String(actualPort)] : ["next", "dev", "-p", String(actualPort)];
4444
+ const usePnpm = existsSync7(join3(webDir, "pnpm-lock.yaml"));
4445
+ const useNpm = !usePnpm && existsSync7(join3(webDir, "package-lock.json"));
4446
+ const pkgManager = usePnpm ? "pnpm" : useNpm ? "npm" : "npx";
4447
+ const { NODE_OPTIONS, TSX_TSCONFIG_PATH, ...cleanEnv } = process.env;
4448
+ const webEnv = {
4449
+ ...cleanEnv,
4450
+ NEXT_PUBLIC_API_URL: `http://127.0.0.1:${apiPort}`
4451
+ };
4452
+ const isProduction = process.env.NODE_ENV === "production";
4453
+ let command;
4454
+ let args;
4455
+ if (isProduction) {
4456
+ if (!hasProductionBuild(webDir)) {
4457
+ if (!quiet) console.log(" \u{1F4E6} Building Web UI for production...");
4458
+ const buildArgs = pkgManager === "npx" ? ["next", "build"] : ["run", "build"];
4459
+ const buildResult = await runCommand(pkgManager, buildArgs, webDir, webEnv);
4460
+ if (!buildResult.success) {
4461
+ if (!quiet) console.error(" \u274C Web UI build failed");
4462
+ return { process: null, port: actualPort };
4463
+ }
4464
+ if (!quiet) console.log(" \u2713 Web UI build complete");
4465
+ }
4466
+ command = pkgManager;
4467
+ args = pkgManager === "npx" ? ["next", "start", "-p", String(actualPort)] : ["run", "start", "-p", String(actualPort)];
4468
+ } else {
4469
+ command = pkgManager;
4470
+ args = pkgManager === "npx" ? ["next", "dev", "-p", String(actualPort)] : ["run", "dev", "-p", String(actualPort)];
4471
+ }
4420
4472
  const child = spawn(command, args, {
4421
4473
  cwd: webDir,
4422
4474
  stdio: ["ignore", "pipe", "pipe"],
4423
- env: {
4424
- ...process.env,
4425
- NEXT_PUBLIC_API_URL: `http://127.0.0.1:${apiPort}`
4426
- },
4427
- detached: false
4475
+ env: webEnv,
4476
+ detached: false,
4477
+ shell: true
4428
4478
  });
4479
+ const startupTimeout = 3e4;
4429
4480
  let started = false;
4430
- child.stdout?.on("data", (data) => {
4431
- const output = data.toString();
4432
- if (!started && (output.includes("Ready") || output.includes("started") || output.includes("localhost"))) {
4433
- started = true;
4434
- if (!quiet) console.log(` \u2713 Web UI running at http://localhost:${actualPort}`);
4435
- }
4436
- });
4437
- if (!quiet) {
4481
+ let exited = false;
4482
+ let exitCode = null;
4483
+ const startedPromise = new Promise((resolve7) => {
4484
+ const timeout = setTimeout(() => {
4485
+ if (!started && !exited) {
4486
+ resolve7(false);
4487
+ }
4488
+ }, startupTimeout);
4489
+ child.stdout?.on("data", (data) => {
4490
+ const output = data.toString();
4491
+ if (!started && (output.includes("Ready") || output.includes("started") || output.includes("localhost"))) {
4492
+ started = true;
4493
+ clearTimeout(timeout);
4494
+ resolve7(true);
4495
+ }
4496
+ });
4438
4497
  child.stderr?.on("data", (data) => {
4439
4498
  const output = data.toString();
4440
4499
  if (output.toLowerCase().includes("error")) {
4441
- console.error(` Web UI error: ${output.trim()}`);
4500
+ if (!quiet) console.error(` Web UI error: ${output.trim().slice(0, 200)}`);
4442
4501
  }
4443
4502
  });
4444
- }
4445
- child.on("exit", () => {
4446
- webUIProcess = null;
4503
+ child.on("error", (err) => {
4504
+ if (!quiet) console.error(` \u274C Web UI spawn error: ${err.message}`);
4505
+ clearTimeout(timeout);
4506
+ resolve7(false);
4507
+ });
4508
+ child.on("exit", (code) => {
4509
+ exited = true;
4510
+ exitCode = code;
4511
+ if (!started) {
4512
+ clearTimeout(timeout);
4513
+ resolve7(false);
4514
+ }
4515
+ webUIProcess = null;
4516
+ });
4447
4517
  });
4448
4518
  webUIProcess = child;
4449
- return { process: child, port: actualPort };
4519
+ const didStart = await startedPromise;
4520
+ if (!didStart) {
4521
+ if (exited && exitCode !== 0) {
4522
+ if (!quiet) console.error(` \u274C Web UI failed to start (exit code: ${exitCode})`);
4523
+ } else if (!exited) {
4524
+ if (!quiet) console.log(` \u26A0 Web UI startup timed out, continuing anyway...`);
4525
+ }
4526
+ }
4527
+ return { process: child, port: actualPort, started: didStart };
4450
4528
  }
4451
4529
  function stopWebUI() {
4452
4530
  if (webUIProcess) {
@@ -4540,11 +4618,13 @@ async function startServer(options = {}) {
4540
4618
  hostname: host
4541
4619
  });
4542
4620
  let webPort;
4621
+ let webStarted;
4543
4622
  if (options.webUI !== false) {
4544
4623
  const result = await startWebUI(port, options.webPort || DEFAULT_WEB_PORT, options.quiet);
4545
4624
  webPort = result.port;
4625
+ webStarted = result.started;
4546
4626
  }
4547
- return { app, port, host, webPort };
4627
+ return { app, port, host, webPort, webStarted };
4548
4628
  }
4549
4629
  function stopServer() {
4550
4630
  stopWebUI();