shotTarget

tcmts.cli.shot.shotTarget · #mp2dskbv · function · runs on the server

uses tcmts.cli.running.liveRunning, which uses node:fs

The page tcm shot is given, as a URL: one as it is; host:port or a port alone, over http on this machine; otherwise an app's name, at the address the tcm dev or tcm start running it on this codebase listens on (the newest). A file name for it, too.

import { liveRunning } from "#codebase/tcmts/cli/running";
import { UsageError } from "#codebase/tcmts/cli/ui";

export function shotTarget(given: string, root: string | undefined): { url: string; slug: string } {
  const slugOf = (s: string) =>
    s
      .replace(/^[a-z][a-z0-9+.-]*:\/\//i, "")
      .replace(/[^a-z0-9.]+/gi, "-")
      .replace(/^-+|-+$/g, "") || "page";
  if (/^[a-z][a-z0-9+.-]*:\/\//i.test(given)) return { url: given, slug: slugOf(given) };
  if (/^:?\d+$/.test(given)) {
    const url = `http://127.0.0.1:${given.replace(":", "")}/`;
    return { url, slug: slugOf(url) };
  }
  if (/^(localhost|\d+\.\d+\.\d+\.\d+|\[[^\]]*\])(:\d+)?([/?#]|$)/.test(given))
    return { url: `http://${given}`, slug: slugOf(given) };
  const running = root
    ? liveRunning(root)
        .filter((r) => r.app === given && r.url)
        .at(-1)
    : undefined;
  if (!running)
    throw new UsageError(
      `${given}: not a URL, and no tcm dev or start is running it on this codebase (tcm ps lists them)`,
    );
  return { url: running.url!, slug: slugOf(given) };
}

uses

used by