packageInfoOf

tcmts.share.packageInfoOf · #bxc6y5ro · function · runs anywhere

The package a manifest (a manifest definition) declares, as its source says it: read, never run; none if it has no name.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { absolute, nsOf } from "#codebase/tcmts/cli/manifests";
import { manifestFields } from "#codebase/tcmts/share";
import type { PackageInfo } from "#codebase/tcmts/share";
import type { NameRow } from "#codebase/tcmts/store";

export function packageInfoOf(cb: Codebase, r: NameRow): PackageInfo | undefined {
  const rec = cb.store.getDef(r.hash);
  const m = manifestFields(rec?.sourceText ?? "");
  const name = typeof m.name === "string" ? m.name : undefined;
  if (!name) return undefined;
  const ns = nsOf(r.path);
  const strings = (v: unknown) =>
    Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : [];
  const record = <T>(v: unknown, f: (x: unknown) => T | undefined) =>
    Object.fromEntries(
      Object.entries(v && typeof v === "object" && !Array.isArray(v) ? v : {}).flatMap(([k, x]) => {
        const y = f(x);
        return y === undefined ? [] : [[k, y]];
      }),
    ) as Record<string, T>;
  const whole = (x: unknown) => (typeof x === "string" ? absolute(ns, x) : undefined);
  const bin = record(m.bin, whole);
  const description =
    (typeof m.description === "string" ? m.description : undefined) ??
    rec?.doc
      ?.replace(/^\/\*\*|\*\/$/g, "")
      .replace(/^\s*\* ?/gm, "")
      .trim()
      .split("\n")[0];
  return {
    name,
    namespace: ns,
    manifestPath: r.path,
    bins: Object.keys(bin),
    bin,
    exports: record(m.exports, (x) => strings(x).map((t) => absolute(ns, t))),
    apps: record(m.apps, whole),
    suites: record(m.suites, (x) => strings(x).map((t) => absolute(ns, t))),
    files: strings(m.files),
    with: strings(m.with),
    libraries: strings(m.libraries),
    ...(typeof m.packageJson === "string" ? { packageJson: absolute(ns, m.packageJson) } : {}),
    ...(description ? { description } : {}),
  };
}

uses

used by