shippedLibraries

tcmts.cli.sync.shippedLibraries · #hp4iyg7x · term · runs on the server

uses node:fs

The tcm libraries the running tcm's package ships (libraries in its manifest), as a codebase directory cb can fetch them from: a copy, in its cache, of the codebase beside the package's code (<package>/libraries, or, for a tcm run from its codebase as suites run it, where they packed them: TCM_SHIPPED_LIBRARIES). Undefined if it ships none.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { createHash } from "node:crypto";
import fs from "node:fs";
import path from "node:path";

export function shippedLibraries(cb: Codebase): string | undefined {
  const bin = process.argv[1];
  const beside =
    bin && fs.existsSync(bin)
      ? path.join(path.dirname(path.dirname(fs.realpathSync(bin))), "libraries")
      : undefined;
  const from = [beside, process.env.TCM_SHIPPED_LIBRARIES]
    .map((d) => d && path.join(d, "codebase.sqlite"))
    .find((f) => f && fs.existsSync(f));
  if (!from) return undefined;
  // (a copy: opening a codebase writes beside it, and the package's own files stay as installed)
  const bytes = fs.readFileSync(from);
  const dir = path.join(
    cb.lay.cacheDir,
    "shipped",
    createHash("sha256").update(bytes).digest("hex").slice(0, 16),
  );
  const to = path.join(dir, ".tcm", "codebase.sqlite");
  if (!fs.existsSync(to)) {
    fs.mkdirSync(path.dirname(to), { recursive: true });
    fs.writeFileSync(`${to}.tmp`, bytes);
    fs.renameSync(`${to}.tmp`, to);
  }
  return dir;
}

uses

used by