libraryEffects

tcmts.cli.libraries.libraryEffects · #usjtmcy7 · term · runs on the server

uses tcmts.loader.packages.ensureExternals, which uses node:fs

Where a library's code reaches outside itself, worked out from the code, not from what anyone says of it: by effect, the definitions whose own code does, or which use a package's export that does (fetchTiles (node-fetch)). unknown: packages whose code couldn't all be followed (a computed import, eval), so may reach more. Its tests aren't counted; rows are named as in the library.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { npmFacts } from "#codebase/tcmts/cli/npm";
import { isTestPath } from "#codebase/tcmts/cli/testing";
import { ownEffects } from "#codebase/tcmts/core/effects";
import type { Effect } from "#codebase/tcmts/core/effects";
import { externalsOf } from "#codebase/tcmts/loader";
import { ensureExternals, parseModule } from "#codebase/tcmts/loader/packages";
import type { NameRow } from "#codebase/tcmts/store";

export function libraryEffects(
  cb: Codebase,
  rows: NameRow[],
): { reaches: Map<Effect, string[]>; unknown: string[] } {
  const reaches = new Map<Effect, string[]>();
  const unknown = new Set<string>();
  const add = (e: Effect, where: string) => {
    const at = reaches.get(e) ?? [];
    if (!at.includes(where)) reaches.set(e, [...at, where]);
  };
  for (const r of rows) {
    if (r.kind === "type" || r.kind === "file" || isTestPath(r.path)) continue;
    const rec = cb.store.getDef(r.hash);
    if (!rec) continue;
    for (const e of ownEffects(rec.ir)) add(e, r.path);
    const npm = externalsOf([rec]).filter((x) => x.module.startsWith("npm:"));
    if (npm.length) ensureExternals(cb.lay, npm);
    for (const x of npm) {
      const pkg = parseModule(x.module).name;
      const f = npmFacts(cb.lay.cacheDir, x, "server");
      if (!f || f.opaque.length) unknown.add(pkg);
      for (const e of f?.effects ?? []) add(e, `${r.path} (${pkg})`);
    }
  }
  return { reaches, unknown: [...unknown].sort() };
}

uses

used by