cli-api.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import * as Command from "effect/unstable/cli/Command"
  2. type Options<Config extends Command.Command.Config, Commands extends ReadonlyArray<Any>> = {
  3. readonly description?: string
  4. readonly params?: Config
  5. readonly commands?: Commands
  6. }
  7. export interface Node<
  8. Name extends string,
  9. Spec extends Command.Command<Name, any, any, any, any>,
  10. Commands extends Children,
  11. > {
  12. readonly name: Name
  13. readonly spec: Spec
  14. readonly commands: Commands
  15. }
  16. export type Any = Node<string, Command.Command<any, any, any, any, any>, Children>
  17. export type Children = Readonly<Record<string, Any>>
  18. export function make<
  19. const Name extends string,
  20. const Config extends Command.Command.Config = {},
  21. const Commands extends ReadonlyArray<Any> = [],
  22. >(name: Name, options: Options<Config, Commands> = {}) {
  23. const command = Command.make(name, options.params ?? ({} as Config))
  24. const spec = options.description ? command.pipe(Command.withDescription(options.description)) : command
  25. return {
  26. name,
  27. spec,
  28. commands: Object.fromEntries(
  29. (options.commands ?? []).map((command) => [command.name, command]),
  30. ) as ChildrenOf<Commands>,
  31. }
  32. }
  33. type ChildrenOf<Commands extends ReadonlyArray<Any>> = {
  34. readonly [Node in Commands[number] as Node["name"]]: Node
  35. }
  36. export * as CliApi from "./cli-api"