baa-conductor


baa-conductor / apps / status-api / src
im_wower  ·  2026-03-22

index.ts

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