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

Function traverseCommand

packages/cli/src/internal/commandDescriptor.ts:999–1049  ·  view source on GitHub ↗
(
  self: Instruction,
  initialState: S,
  f: (state: S, info: CommandInfo) => Effect.Effect<S>
)

Source from the content-addressed store, hash-verified

997 * based on information acquired from the command.
998 */
999const traverseCommand = <S>(
1000 self: Instruction,
1001 initialState: S,
1002 f: (state: S, info: CommandInfo) => Effect.Effect<S>
1003): Effect.Effect<S> =>
1004 SynchronizedRef.make(initialState).pipe(Effect.flatMap((ref) => {
1005 const loop = (
1006 self: Instruction,
1007 parentCommands: ReadonlyArray<string>,
1008 subcommands: ReadonlyArray<[string, Standard | GetUserInput]>,
1009 level: number
1010 ): Effect.Effect<void, never, never> => {
1011 switch (self._tag) {
1012 case "Standard": {
1013 const info: CommandInfo = {
1014 command: self,
1015 parentCommands,
1016 subcommands,
1017 level
1018 }
1019 return SynchronizedRef.updateEffect(ref, (state) => f(state, info))
1020 }
1021 case "GetUserInput": {
1022 const info: CommandInfo = {
1023 command: self,
1024 parentCommands,
1025 subcommands,
1026 level
1027 }
1028 return SynchronizedRef.updateEffect(ref, (state) => f(state, info))
1029 }
1030 case "Map": {
1031 return loop(self.command, parentCommands, subcommands, level)
1032 }
1033 case "Subcommands": {
1034 const parentNames = getNamesInternal(self.parent)
1035 const nextSubcommands = getSubcommandsInternal(self)
1036 const nextParentCommands = Arr.appendAll(parentCommands, parentNames)
1037 // Traverse the parent command using old parent names and next subcommands
1038 return loop(self.parent, parentCommands, nextSubcommands, level).pipe(
1039 Effect.zipRight(Effect.forEach(self.children, (child) =>
1040 // Traverse the child command using next parent names and old subcommands
1041 loop(child, nextParentCommands, subcommands, level + 1)))
1042 )
1043 }
1044 }
1045 }
1046 return Effect.suspend(() => loop(self, Arr.empty(), Arr.empty(), 0)).pipe(
1047 Effect.zipRight(SynchronizedRef.get(ref))
1048 )
1049 }))
1050
1051const indentAll = dual<
1052 (indent: number) => (self: ReadonlyArray<string>) => Array<string>,

Callers 3

Calls 4

loopFunction · 0.70
pipeMethod · 0.65
makeMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected