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