agentBrowserBin

tcmts.cli.browser.agentBrowserBin · #cz6lyqvg · function · runs on the server

uses node:path

agent-browser's own launcher (its package's bin, which runs the binary built for this platform), installed into userCache the first time, as a codebase's packages are installed into its cache (ensureExternals), saying so. Its install script isn't run (tcm runs none): it only marks the binary runnable, which the launcher does too.

import { AGENT_BROWSER, userCache } from "#codebase/tcmts/cli/browser";
import { dim } from "#codebase/tcmts/cli/style";
import { UsageError } from "#codebase/tcmts/cli/ui";
import { compatRange } from "#codebase/tcmts/core/semver";
import type { Layout } from "#codebase/tcmts/loader";
import { ensureExternals, installDir, installedVersion, lockOf, npmAlias } from "#codebase/tcmts/loader/packages";
import fs from "node:fs";
import path from "node:path";

export function agentBrowserBin(): string {
  const dir = userCache();
  const lay: Layout = {
    root: dir,
    tcmDir: dir,
    cacheDir: dir,
    shimDir: dir,
    dbFile: path.join(dir, "codebase.sqlite"),
  };
  const module = `npm:agent-browser@${AGENT_BROWSER}`;
  const range = compatRange(AGENT_BROWSER);
  const bin = path.join(installDir(lay, "agent-browser", range), "bin", "agent-browser.js");
  if (installedVersion(lay, npmAlias(module)) === AGENT_BROWSER && fs.existsSync(bin)) return bin;
  console.error(
    dim(`tcm browser runs agent-browser ${AGENT_BROWSER}: installing it into ${dir} (once)…`),
  );
  fs.mkdirSync(dir, { recursive: true });
  // (this version, not the newest in its range)
  lockOf(lay).set("agent-browser", range, AGENT_BROWSER);
  try {
    ensureExternals(lay, [{ module, subpath: "", export: "*" }]);
  } catch (e) {
    throw new UsageError(
      `couldn't install agent-browser ${AGENT_BROWSER} into ${dir}: ${(e as Error).message}`,
    );
  }
  if (!fs.existsSync(bin))
    throw new UsageError(`agent-browser ${AGENT_BROWSER} installed into ${dir} has no ${bin}`);
  return bin;
}

uses

used by