upgradeMap

tcmts.cli.libraries.upgradeMap · #c4umsuoh · term · runs anywhere

What an upgrade from one version of a library to another does to its names: each definition of the old one that the new one has another at the same name (and in the same space) is replaced by it; the rest of the old one that the new one doesn't have at all is gone.

import { sameSpace } from "#codebase/tcmts/core/types";
import type { NameRow } from "#codebase/tcmts/store";

export function upgradeMap(
  before: Pick<NameRow, "path" | "hash" | "kind">[],
  after: Pick<NameRow, "path" | "hash" | "kind">[],
): { replaced: Map<string, string>; added: number; gone: Set<string> } {
  const replaced = new Map<string, string>();
  for (const r of before) {
    const n = after.find((a) => a.path === r.path && sameSpace(a.kind, r.kind));
    if (n && n.hash !== r.hash) replaced.set(r.hash, n.hash);
  }
  const had = new Set(before.map((r) => r.path));
  const hashes = new Set(after.map((a) => a.hash));
  return {
    replaced,
    added: after.filter((a) => !had.has(a.path)).length,
    gone: new Set(
      before.filter((r) => !hashes.has(r.hash) && !replaced.has(r.hash)).map((r) => r.hash),
    ),
  };
}

uses

used by