removeLibrary

tcmts.cli.libraries.removeLibrary · #ufavqnmf · term · runs on the server

uses tcmts.cli.libraries.withRecord, which uses Buffer

Remove a library: its names and its entry in the record; refused while code outside it uses what only it names.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { libraryNamed, namesNow, recordAt, usersOf, within, withRecord } from "#codebase/tcmts/cli/libraries";
import type { Library } from "#codebase/tcmts/cli/libraries";
import { bold, dim } from "#codebase/tcmts/cli/style";
import { UsageError } from "#codebase/tcmts/cli/ui";

export function removeLibrary(cb: Codebase, which: string): Library {
  const lib = libraryNamed(cb.store, which);
  const users = usersOf(cb.store, lib);
  if (users.length)
    throw new UsageError(
      `${lib.path} is used by ${users.slice(0, 5).join(", ")}${users.length > 5 ? ` and ${users.length - 5} more` : ""}: change them not to use it first`,
    );
  const names = namesNow(cb.store);
  const entries = recordAt(cb.store, lib.record);
  delete entries[lib.path.slice(lib.path.lastIndexOf(".") + 1)];
  const kept = names.filter((n) => !within(n.path, lib.path));
  cb.store.setBranchNames(
    cb.store.branch().name,
    withRecord(cb, kept, lib.record, entries),
    `lib rm ${lib.path}`,
  );
  cb.refreshShim();
  console.log(`removed ${bold(lib.path)} ${dim(`(${names.length - kept.length} names)`)}`);
  return lib;
}

uses

used by