Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
register descriptors at point of consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Nov 29, 2023
1 parent b0d7e9f commit 2b5fceb
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/internal/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ const Prototype = {
}
}

const modifiedCommands = globalValue(
"@effect/cli/Command/modifiedCommands",
const registeredDescriptors = globalValue(
"@effect/cli/Command/registeredDescriptors",
() => new WeakMap<Context.Tag<any, any>, Descriptor.Command<any>>()
)

const getDescriptor = <Name extends string, R, E, A>(self: Command.Command<Name, R, E, A>) =>
modifiedCommands.get(self.tag) ?? self.descriptor
registeredDescriptors.get(self.tag) ?? self.descriptor

const makeProto = <Name extends string, R, E, A>(
descriptor: Descriptor.Command<A>,
Expand All @@ -141,7 +141,6 @@ const makeProto = <Name extends string, R, E, A>(
self.descriptor = descriptor
self.handler = handler
self.tag = tag ?? Context.Tag()
modifiedCommands.set(self.tag, self.descriptor)
return self
}

Expand Down Expand Up @@ -359,9 +358,10 @@ export const withSubcommands = dual<
)
const handlers = ReadonlyArray.reduce(
subcommands,
new Map<Context.Tag<any, any>, (_: any) => Effect.Effect<any, any, void>>(),
new Map<Context.Tag<any, any>, Command.Command<any, any, any, any>>(),
(handlers, subcommand) => {
handlers.set(subcommand.tag, subcommand.handler)
handlers.set(subcommand.tag, subcommand)
registeredDescriptors.set(subcommand.tag, subcommand.descriptor)
return handlers
}
)
Expand All @@ -373,8 +373,9 @@ export const withSubcommands = dual<
) {
if (args.subcommand._tag === "Some") {
const [tag, value] = args.subcommand.value
const subcommand = handlers.get(tag)!
return Effect.provideService(
handlers.get(tag)!(value),
subcommand.handler(value),
self.tag,
args as any
)
Expand Down Expand Up @@ -432,5 +433,6 @@ export const run = dual<
...config,
command: self.descriptor
})
registeredDescriptors.set(self.tag, self.descriptor)
return (args) => InternalCliApp.run(app, args, self.handler)
})

0 comments on commit 2b5fceb

Please sign in to comment.