exportsOf

tcmts.cli.pack.exportsOf · #wrhbtd47 · term · runs anywhere

What a subpath exports: definitions under their last names, namespaces' definitions under theirs. A type and a value can share a name; two of either can't.

import type { Exported } from "#codebase/tcmts/cli/pack";
import type { NameRow } from "#codebase/tcmts/store";

export function exportsOf(all: NameRow[], targets: string[]): Exported[] {
  const out: Exported[] = [];
  const taken = new Map<string, string>();
  for (const target of targets) {
    const names = all.filter((n) => n.path === target || n.path.startsWith(`${target}.`));
    if (!names.length) throw new Error(`'${target}' isn't a definition or namespace`);
    for (const n of names) {
      const name = n.path.slice(n.path.lastIndexOf(".") + 1);
      const space = n.kind === "type" ? "type" : "value";
      const prior = taken.get(`${space} ${name}`);
      if (prior === n.path) continue;
      if (prior) throw new Error(`two ${space}s would be exported as ${name}: ${prior} and ${n.path}; pack them under separate subpaths`);
      taken.set(`${space} ${name}`, n.path);
      out.push({ name, hash: n.hash, kind: n.kind });
    }
  }
  return out;
}

uses

used by