registryToAsk

tcmts.cli.libraries.registryToAsk · #fej5j2lt · term · runs anywhere

The registry tcm lib search asks: the one given (kept for next time, a codebase setting), else the one kept, else tcm's own (DEFAULT_REGISTRY, which isn't kept: a codebase that never chose one follows tcm's).

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { DEFAULT_REGISTRY, REGISTRY_SETTING } from "#codebase/tcmts/cli/libraries";
import { UsageError } from "#codebase/tcmts/cli/ui";

export function registryToAsk(
  store: Pick<Codebase["store"], "getSetting" | "setSetting">,
  registry: string | undefined,
): string {
  const given = registry?.trim().replace(/\/+$/, "");
  if (given) {
    if (!/^https?:\/\//.test(given))
      throw new UsageError(`--registry takes a registry's address (https://…), not '${registry}'`);
    if (store.getSetting(REGISTRY_SETTING) !== given) store.setSetting(REGISTRY_SETTING, given);
    return given;
  }
  return store.getSetting(REGISTRY_SETTING) || DEFAULT_REGISTRY;
}

uses

used by