sourceFor

tcmts.cli.complete.sourceFor · #aodrhviv · term · runs anywhere

Where each argument or option completes from: by command, then by parameter name.

import type { Complete, Sources } from "#codebase/tcmts/cli/complete";

export function sourceFor(command: string, param: string, s: Sources): ((complete: Complete) => void) | undefined {
  const definitions = (c: Complete) => s.definitions(c);
  const values = (c: Complete) => s.definitions(c, "value");
  const namespaces = (c: Complete) => s.namespaces(c);
  const both = (c: Complete) => (s.namespaces(c), s.definitions(c));
  const dirs = (c: Complete) => s.directories(c);
  const byCommand: Record<string, (c: Complete) => void> = {
    "mv from": both,
    "pack entries": both,
    "upgrade namespace": namespaces,
    "upgrade package": (c) => s.packages(c),
    "import dir": dirs,
    "pull branch": () => {},
    "branch branch": () => {},
    "contribute to": (c) => s.branches(c),
    "complete shell": (c) => ["zsh", "bash", "fish", "powershell"].forEach((sh) => c(sh, "")),
  };
  const byName: Record<string, (c: Complete) => void> = {
    name: definitions,
    names: definitions,
    app: values,
    namespace: namespaces,
    into: namespaces,
    branch: (c) => s.branches(c),
    remote: (c) => s.remotes(c),
    on: (c) => s.remotes(c),
    table: (c) => s.tables(c),
    file: (c) => s.scratchFiles(c),
    files: (c) => s.scratchFiles(c),
    place: (c) => s.scratchFiles(c),
    about: definitions,
  };
  return byCommand[`${command} ${param}`] ?? byName[param];
}

uses

used by