browserArgs

tcmts.cli.browser.browserArgs · #7azhefnr · function · runs on the server

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

agent-browser's arguments, with what open is given as tcm shot takes it (shotTarget): an app tcm dev or tcm start runs here (app.App) or a port or host:port on this machine, as its URL. Anything else is left to agent-browser (example.com included); a component's name that nothing runs is an error.

import { liveRunning } from "#codebase/tcmts/cli/running";
import { shotTarget } from "#codebase/tcmts/cli/shot";

export function browserArgs(args: string[], root: string | undefined): string[] {
  // (the command: the first word that isn't a flag or a flag's value)
  const i = args.findIndex(
    (a, at) =>
      (a === "open" || a === "goto" || a === "navigate") &&
      args.slice(0, at).every((b, j) => b.startsWith("-") || args[j - 1]?.startsWith("-")),
  );
  const given = i >= 0 ? args[i + 1] : undefined;
  if (given === undefined || given.startsWith("-")) return args;
  const local =
    /^:?\d+$/.test(given) ||
    /^(localhost|\d+\.\d+\.\d+\.\d+|\[[^\]]*\])(:\d+)?([/?#]|$)/.test(given);
  const running = !!root && liveRunning(root).some((r) => r.app === given && r.url);
  const component = /^[\w$]+(\.[\w$]+)*\.[A-Z][\w$]*$/.test(given);
  if (!local && !running && !component) return args;
  return [...args.slice(0, i + 1), shotTarget(given, root).url, ...args.slice(i + 2)];
}

uses

used by