validNames

tcmts.cli.sync.validNames · #4ox7ayzf · term · runs anywhere

Names from another codebase: paths must be valid (a file's as files are named: docs.getting-started) and every hash must already be stored here; kinds come from the local store.

import { IDENT_PATH } from "#codebase/tcmts/cli/codebase";
import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { FILE_PATH } from "#codebase/tcmts/cli/files";
import { shortHash } from "#codebase/tcmts/core/hash";
import type { NameRow } from "#codebase/tcmts/store";

export function validNames(cb: Codebase, names: NameRow[]): NameRow[] {
  return names.map((n) => {
    const rec = cb.store.getDef(n.hash);
    if (!rec) throw new Error(`${n.path} refers to ${shortHash(n.hash)}, which was not sent`);
    if (!(rec.kind === "file" ? FILE_PATH : IDENT_PATH).test(n.path))
      throw new Error(`invalid name '${n.path}'`);
    return { path: n.path, hash: n.hash, kind: rec.kind };
  });
}

uses

used by