All files / src/utils buildInfo.ts

100% Statements 20/20
96.15% Branches 25/26
100% Functions 6/6
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                          4x 4x   1x         15x       14x 14x             14x 14x 8x   6x             9x 9x 9x 2x   7x 5x   2x                 6x 3x   3x    
/**
 * Git commit / ref baked in at Vite build time.
 *
 * Visibility is env-based, never host-based:
 * - local `npm run dev` uses VITE_NODE_ENV=development
 * - deployed images read ZMS_ENV from the same runtime-config.json
 *   as SHOW_CITIZEN_LOGIN
 *
 * Magnolia embeds (e.g. lhm-i) run the WC on another origin. Resolve the
 * JSON from the script URL (`…/buergeransicht/src/entry-*.js`), not `./`
 * on the host page — that would hit Magnolia and miss ZMS_ENV.
 */
export function runtimeConfigUrl(moduleUrl: string): string {
  try {
    return new URL("../runtime-config.json", moduleUrl).href;
  } catch {
    return "./runtime-config.json";
  }
}
 
export function isProductionBuild(nodeEnv: string | undefined): boolean {
  return (nodeEnv ?? "").trim().toLowerCase() === "production";
}
 
export function isProductionZmsEnv(zmsEnv: string | undefined): boolean {
  const env = (zmsEnv ?? "").trim().toLowerCase();
  return env === "prod" || env === "production";
}
 
export function shouldShowBuildInfo(
  viteNodeEnv: string | undefined,
  zmsEnv: string | undefined
): boolean {
  const runtimeEnv = (zmsEnv ?? "").trim();
  if (runtimeEnv) {
    return !isProductionZmsEnv(runtimeEnv);
  }
  return !isProductionBuild(viteNodeEnv);
}
 
export function formatBuildInfoLabel(
  commit: string | undefined,
  ref: string | undefined
): string | null {
  const sha = (commit ?? "").trim();
  const name = (ref ?? "").trim();
  if (!sha && !name) {
    return null;
  }
  if (sha && name) {
    return `${sha} · ${name}`;
  }
  return sha || name;
}
 
export function resolveBuildInfoLabel(
  commit: string | undefined,
  ref: string | undefined,
  viteNodeEnv: string | undefined,
  zmsEnv: string | undefined
): string | null {
  if (!shouldShowBuildInfo(viteNodeEnv, zmsEnv)) {
    return null;
  }
  return formatBuildInfoLabel(commit, ref);
}