diff --git a/.changeset/three-pianos-nail.md b/.changeset/three-pianos-nail.md new file mode 100644 index 0000000..d9a9a70 --- /dev/null +++ b/.changeset/three-pianos-nail.md @@ -0,0 +1,5 @@ +--- +"@effect/cli": patch +--- + +add Command.provideSync diff --git a/.changeset/violet-balloons-film.md b/.changeset/violet-balloons-film.md new file mode 100644 index 0000000..3406b2d --- /dev/null +++ b/.changeset/violet-balloons-film.md @@ -0,0 +1,5 @@ +--- +"@effect/cli": patch +--- + +add Command.provideEffect diff --git a/.vscode/settings.json b/.vscode/settings.json index 6538f19..a11d681 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,13 +22,9 @@ "[typescriptreact]": { "editor.defaultFormatter": "dbaeumer.vscode-eslint" }, - "eslint.validate": [ - "markdown", - "javascript", - "typescript" - ], + "eslint.validate": ["markdown", "javascript", "typescript"], "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, "editor.quickSuggestions": { "other": true, @@ -42,7 +38,7 @@ "editor.tabCompletion": "off", "editor.suggest.localityBonus": true, "editor.suggestSelection": "recentlyUsed", - "editor.wordBasedSuggestions": true, + "editor.wordBasedSuggestions": "matchingDocuments", "editor.parameterHints.enabled": true, - "files.insertFinalNewline": true, + "files.insertFinalNewline": true } diff --git a/src/Command.ts b/src/Command.ts index f4b2d0a..8b1490d 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -267,6 +267,24 @@ export const provide: { ): Command, E | LE, A> } = Internal.provide +/** + * @since 1.0.0 + * @category combinators + */ +export const provideEffect: { + ( + tag: Tag, + effect: Effect | ((_: A) => Effect) + ): ( + self: Command + ) => Command, E2 | E, A> + ( + self: Command, + tag: Tag, + effect: Effect | ((_: A) => Effect) + ): Command, E | E2, A> +} = Internal.provideEffect + /** * @since 1.0.0 * @category combinators @@ -281,6 +299,22 @@ export const provideEffectDiscard: { ): Command } = Internal.provideEffectDiscard +/** + * @since 1.0.0 + * @category combinators + */ +export const provideSync: { + ( + tag: Tag, + service: S | ((_: A) => S) + ): (self: Command) => Command, E, A> + ( + self: Command, + tag: Tag, + service: S | ((_: A) => S) + ): Command, E, A> +} = Internal.provideSync + /** * @since 1.0.0 * @category combinators diff --git a/src/internal/command.ts b/src/internal/command.ts index 1a2f86e..6d2d0c8 100644 --- a/src/internal/command.ts +++ b/src/internal/command.ts @@ -350,6 +350,27 @@ export const provide = dual< Effect.provide(effect, typeof layer === "function" ? layer(config) : layer) })) +/** @internal */ +export const provideEffect = dual< + ( + tag: Context.Tag, + effect: Effect.Effect | ((_: A) => Effect.Effect) + ) => ( + self: Command.Command + ) => Command.Command | R2, E | E2, A>, + ( + self: Command.Command, + tag: Context.Tag, + effect: Effect.Effect | ((_: A) => Effect.Effect) + ) => Command.Command | R2, E | E2, A> +>(3, (self, tag, effect_) => + makeDerive(self, { + transform: (self, config) => { + const effect = typeof effect_ === "function" ? effect_(config) : effect_ + return Effect.provideServiceEffect(self, tag, effect) + } + })) + /** @internal */ export const provideEffectDiscard = dual< ( @@ -369,6 +390,27 @@ export const provideEffectDiscard = dual< } })) +/** @internal */ +export const provideSync = dual< + ( + tag: Context.Tag, + service: S | ((_: A) => S) + ) => ( + self: Command.Command + ) => Command.Command, E, A>, + ( + self: Command.Command, + tag: Context.Tag, + service: S | ((_: A) => S) + ) => Command.Command, E, A> +>(3, (self, tag, f) => + makeDerive(self, { + transform: (self, config) => { + const service = typeof f === "function" ? (f as any)(config) : f + return Effect.provideService(self, tag, service) + } + })) + /** @internal */ export const withDescription = dual< ( diff --git a/test/Command.test.ts b/test/Command.test.ts index e8b1dbd..75533db 100644 --- a/test/Command.test.ts +++ b/test/Command.test.ts @@ -33,11 +33,14 @@ const clone = Command.make("clone", { } })).pipe(Command.withDescription("Clone a repository into a new directory")) +const AddService = Context.Tag<"AddService">() + const add = Command.make("add", { pathspec: Args.text({ name: "pathspec" }) }).pipe( Command.withHandler(({ pathspec }) => Effect.gen(function*(_) { + yield* _(AddService) const { log } = yield* _(Messages) const { verbose } = yield* _(git) if (verbose) { @@ -47,7 +50,8 @@ const add = Command.make("add", { } }) ), - Command.withDescription("Add file contents to the index") + Command.withDescription("Add file contents to the index"), + Command.provideEffect(AddService, (_) => Effect.succeed("AddService" as const)) ) const run = git.pipe(