File size: 1,885 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { execFile } from "node:child_process";
import { t } from "../i18n.mjs";

export function registerDashboard(program) {
  program
    .command("dashboard")
    .description(t("dashboard.description"))
    .option("--url", t("dashboard.urlOnly"))
    .option("--port <port>", "Port the server is running on", "20128")
    .option("--tui", t("dashboard.tui") || "Open interactive TUI dashboard (terminal UI)")
    .action(async (opts, cmd) => {
      if (opts.tui) {
        const globalOpts = cmd.optsWithGlobals();
        const port = opts.port ? parseInt(String(opts.port), 10) : 20128;
        const baseUrl = globalOpts.baseUrl ?? `http://localhost:${port}`;
        const apiKey = globalOpts.apiKey ?? null;
        const { startInteractiveTui } = await import("../tui/Dashboard.jsx");
        await startInteractiveTui({ port, baseUrl, apiKey });
        return;
      }
      const exitCode = await runDashboardCommand(opts);
      if (exitCode !== 0) process.exit(exitCode);
    });
}

export async function runDashboardCommand(opts = {}) {
  const port = opts.port ? parseInt(String(opts.port), 10) : 20128;
  const dashboardUrl = `http://localhost:${port}`;

  if (opts.url) {
    console.log(dashboardUrl);
    return 0;
  }

  console.log(t("dashboard.opening", { url: dashboardUrl }));

  try {
    const open = await import("open");
    await open.default(dashboardUrl);
  } catch {
    await openFallback(dashboardUrl);
  }

  return 0;
}

function openFallback(url) {
  return new Promise((resolve) => {
    const { platform } = process;
    let cmd, args;

    if (platform === "darwin") {
      cmd = "open";
      args = [url];
    } else if (platform === "win32") {
      cmd = "cmd";
      args = ["/c", "start", "", url];
    } else {
      cmd = "xdg-open";
      args = [url];
    }

    execFile(cmd, args, { stdio: "ignore" }, () => resolve());
  });
}