packLibraries

tcmts.cli.pack.packLibraries · #tmglw52f · term · runs on the server

uses tcmts.cli.pack.shipCodebase, which uses node:fs

Ship tcm libraries of the codebase (libraries, packages with npm: false) in a package: a codebase holding each one's newest release, its namespace named as it was there and the release recorded at it, so its namespace has the same hash, written to out/codebase.sqlite. tcm lib add tcm <name> mounts one from there. Says how many definitions it holds.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { findPackage, packagesIn } from "#codebase/tcmts/cli/manifests";
import { shipCodebase } from "#codebase/tcmts/cli/pack";
import { ReleaseError, rowsAt } from "#codebase/tcmts/cli/releases";
import type { NameRow } from "#codebase/tcmts/store";

export function packLibraries(cb: Codebase, libraries: string[], out: string): number {
  const shipped = libraries.map((name) => {
    const release = cb.store.releases(name)[0];
    if (!release)
      throw new ReleaseError(`${name}, a library the package ships, has no release yet`);
    const rows = rowsAt(cb, release.causal);
    const pkg = findPackage(packagesIn(cb, rows), name);
    if (pkg.manifest.npm !== false)
      throw new ReleaseError(
        `${name}, a library the package ships, isn't a tcm library (its manifest says \`npm: false\`)`,
      );
    const under = release.namespace ? `${release.namespace}.` : "";
    return { release, rows: rows.filter((r) => r.path.startsWith(under)) };
  });
  const rows: NameRow[] = shipped.flatMap((s) => s.rows);
  return shipCodebase(
    cb,
    rows.map((r) => r.hash),
    out,
    (target) => {
      target.store.setBranchNames(
        target.store.branch().name,
        rows,
        `ship ${shipped.map((s) => `${s.release.package}@${s.release.version}`).join(", ")}`,
      );
      const head = target.store.head();
      for (const { release } of shipped) target.store.putRelease({ ...release, causal: head });
    },
  );
}

uses

used by