MCPcopy Create free account
hub / github.com/Effect-TS/effect / normalizeSubcommandEntries

Function normalizeSubcommandEntries

packages/effect/src/unstable/cli/Command.ts:719–763  ·  view source on GitHub ↗
(
  entries: ReadonlyArray<Command.SubcommandEntry>
)

Source from the content-addressed store, hash-verified

717}
718
719const normalizeSubcommandEntries = (
720 entries: ReadonlyArray<Command.SubcommandEntry>
721): {
722 readonly flat: ReadonlyArray<Command.Any>
723 readonly groups: ReadonlyArray<SubcommandGroupInternal>
724} => {
725 const flat: Array<Command.Any> = []
726 const grouped = new Map<string | undefined, NonEmptyArray<Command.Any>>()
727
728 const addToGroup = (group: string | undefined, command: Command.Any): void => {
729 flat.push(command)
730 const existing = grouped.get(group)
731 if (existing) {
732 existing.push(command)
733 } else {
734 grouped.set(group, [command])
735 }
736 }
737
738 for (const entry of entries) {
739 if (isCommand(entry)) {
740 addToGroup(undefined, entry)
741 continue
742 }
743 for (const command of entry.commands) {
744 addToGroup(entry.group, command)
745 }
746 }
747
748 const groups: Array<SubcommandGroupInternal> = []
749 const ungroupedCommands = grouped.get(undefined)
750
751 if (ungroupedCommands && ungroupedCommands.length > 0) {
752 groups.push({ group: undefined, commands: ungroupedCommands })
753 }
754
755 for (const [group, commands] of grouped) {
756 if (group === undefined) {
757 continue
758 }
759 groups.push({ group, commands })
760 }
761
762 return { flat, groups }
763}
764
765/**
766 * Adds subcommands to a command, creating a hierarchical command structure.

Callers 1

Command.tsFile · 0.85

Calls 4

addToGroupFunction · 0.85
pushMethod · 0.80
isCommandFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected