im_wower
·
2026-03-22
index.ts
1export * from "./contracts.js";
2export * from "./config.js";
3export * from "./state-store.js";
4export * from "./daemon.js";
5export * from "./app-server-transport.js";
6export * from "./websocket.js";
7export * from "./local-service.js";
8export * from "./cli.js";
9
10import { runCodexdCli } from "./cli.js";
11
12if (shouldRunCodexdCli(import.meta.url)) {
13 try {
14 const exitCode = await runCodexdCli();
15
16 if (exitCode !== 0 && typeof process !== "undefined") {
17 process.exitCode = exitCode;
18 }
19 } catch (error) {
20 console.error(formatCodexdCliError(error));
21
22 if (typeof process !== "undefined") {
23 process.exitCode = 1;
24 }
25 }
26}
27
28function shouldRunCodexdCli(metaUrl: string): boolean {
29 if (typeof process === "undefined") {
30 return false;
31 }
32
33 const executedPath = normalizeCliEntryPath(process.argv[1]);
34
35 if (executedPath == null) {
36 return false;
37 }
38
39 const sourceEntryPath = normalizeCliEntryPath(toFsPath(metaUrl));
40 const distShimPath = normalizeCliEntryPath(toFsPath(new URL("../../../index.js", metaUrl).href));
41
42 return executedPath === sourceEntryPath || executedPath === distShimPath;
43}
44
45function normalizeCliEntryPath(value: string | undefined): string | null {
46 if (value == null || value === "") {
47 return null;
48 }
49
50 return value.endsWith("/") ? value.slice(0, -1) : value;
51}
52
53function toFsPath(value: string): string {
54 return decodeURIComponent(new URL(value).pathname);
55}
56
57function formatCodexdCliError(error: unknown): string {
58 if (error instanceof Error) {
59 return error.stack ?? `${error.name}: ${error.message}`;
60 }
61
62 return `codexd startup failed: ${String(error)}`;
63}