usersOf

tcmts.cli.libraries.usersOf · #j5h4y5kf · term · runs anywhere

Code outside a library that uses what only it names (of only, if given): what removing it (or those) would leave using code nothing names.

import { inLibrary, librariesIn, within } from "#codebase/tcmts/cli/libraries";
import type { Library } from "#codebase/tcmts/cli/libraries";
import type { Store } from "#codebase/tcmts/store";

export function usersOf(store: Store, lib: Library, only?: Set<string>): string[] {
  const libraries = librariesIn(store);
  const users = new Set<string>();
  for (const r of store.names(undefined, true).filter((n) => within(n.path, lib.path))) {
    if (only && !only.has(r.hash)) continue;
    // (named elsewhere too, another library's included, it stays named)
    if (store.pathsOf(r.hash).some((p) => !within(p, lib.path))) continue;
    for (const d of store.dependents(r.hash))
      for (const p of store.pathsOf(d)) if (!inLibrary(libraries, p)) users.add(p);
  }
  return [...users].sort();
}

uses

used by