File size: 4,060 Bytes
fed1528
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env node
/**
 * Opens the EVE ECC preview inside VS Code / Cursor / Insiders Simple Browser
 * when a compatible CLI is available; otherwise prints Ports-panel instructions.
 *
 * Env:
 *   EVE_PREVIEW_URL   default http://127.0.0.1:5173
 *   EVE_PREVIEW_PORT  default 5173
 */
import { spawnSync } from "node:child_process";
import { createConnection } from "node:net";

const url = process.env.EVE_PREVIEW_URL || "http://127.0.0.1:5173";
const port = Number(process.env.EVE_PREVIEW_PORT || 5173);
const isWin = process.platform === "win32";

function waitForPort(p, host = "127.0.0.1", timeoutMs = 60000) {
  const start = Date.now();
  return new Promise((resolve, reject) => {
    const tryOnce = () => {
      const socket = createConnection({ port: p, host }, () => {
        socket.end();
        resolve(true);
      });
      socket.on("error", () => {
        socket.destroy();
        if (Date.now() - start > timeoutMs) {
          reject(new Error(`Timed out waiting for ${host}:${p}`));
          return;
        }
        setTimeout(tryOnce, 400);
      });
    };
    tryOnce();
  });
}

function run(cmd, args) {
  return spawnSync(cmd, args, {
    stdio: "inherit",
    shell: isWin,
    env: process.env,
  });
}

function findEditorCli() {
  const candidates = [
    process.env.VSCODE_GIT_ASKPASS?.includes("code-insiders")
      ? "code-insiders"
      : null,
    "code-insiders",
    "code",
    "cursor",
  ].filter(Boolean);

  for (const cli of candidates) {
    const probe = spawnSync(cli, ["-v"], {
      shell: isWin,
      stdio: "ignore",
    });
    if (probe.status === 0) return cli;
  }
  return null;
}

console.log(`[eve-preview] waiting for port ${port}…`);
try {
  await waitForPort(port);
} catch (err) {
  console.error(`[eve-preview] ${err.message}`);
  console.error("[eve-preview] Start the dev server first: npm run dev");
  process.exit(1);
}

console.log(`[eve-preview] ${url} is up`);

// Prefer VS Code Insiders URI handler that routes into Simple Browser when possible.
// Note: external browser may still open depending on OS defaults β€” Ports panel
// "openPreview" is the reliable in-editor path (configured in .vscode/settings.json).
const cli = findEditorCli();
if (cli) {
  console.log(`[eve-preview] editor CLI: ${cli}`);
  console.log("[eve-preview] Tip: Ports β†’ 5173 β†’ Open in Browser uses Simple Browser (openPreview).");
  console.log("[eve-preview] Or Command Palette β†’ β€œSimple Browser: Show”");
} else {
  console.log("[eve-preview] No code/cursor CLI on PATH β€” use Ports panel / Simple Browser: Show");
}

console.log("");
console.log("β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”");
console.log("β”‚  In-editor preview (Cursor / Claude / VS Code style)    β”‚");
console.log("β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€");
console.log("β”‚  1. Ports panel β†’ port 5173 β†’ globe / Open in Browser   β”‚");
console.log("β”‚     (configured as Simple Browser / openPreview)        β”‚");
console.log("β”‚  2. Command Palette β†’ β€œSimple Browser: Show”            β”‚");
console.log(`β”‚     β†’ ${url.padEnd(43)}β”‚`);
console.log("β”‚  3. Task: β€œEVE: Start Preview in Editor”                β”‚");
console.log("β”‚                                                         β”‚");
console.log("β”‚  Reserved project ports                                 β”‚");
console.log("β”‚    5173  EVE ECC IDE (Vite)                             β”‚");
console.log("β”‚    4173  Production preview                             β”‚");
console.log("β”‚    8100  ACE controller                                 β”‚");
console.log("β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜");