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

Commit

Permalink
fix generation of completion script
Browse files Browse the repository at this point in the history
  • Loading branch information
IMax153 committed Nov 17, 2023
1 parent 755616c commit 3428902
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 120 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-bees-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/cli": patch
---

fix completion script generation
9 changes: 3 additions & 6 deletions examples/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import * as HelpDoc from "@effect/cli/HelpDoc"
import * as Span from "@effect/cli/HelpDoc/Span"
import * as Options from "@effect/cli/Options"
import * as NodeContext from "@effect/platform-node/NodeContext"
import * as Terminal from "@effect/platform-node/Terminal"
import * as Runtime from "@effect/platform-node/Runtime"
import * as Data from "effect/Data"
import * as Effect from "effect/Effect"
import * as Layer from "effect/Layer"
import * as Option from "effect/Option"

export interface Git extends Data.Case {
Expand Down Expand Up @@ -134,8 +133,6 @@ const cli = CliApp.make({
footer: HelpDoc.p("Copyright 2023")
})

const MainLive = Layer.merge(NodeContext.layer, Terminal.layer)

Effect.sync(() => process.argv.slice(2)).pipe(
Effect.flatMap((args) =>
CliApp.run(cli, args, (command) =>
Expand All @@ -147,6 +144,6 @@ Effect.sync(() => process.argv.slice(2)).pipe(
onSome: handleGitSubcommand
}))
),
Effect.provide(MainLive),
Effect.runFork
Effect.provide(NodeContext.layer),
Runtime.runMain
)
9 changes: 3 additions & 6 deletions examples/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import * as CliApp from "@effect/cli/CliApp"
import * as Command from "@effect/cli/Command"
import * as Prompt from "@effect/cli/Prompt"
import * as NodeContext from "@effect/platform-node/NodeContext"
import * as Terminal from "@effect/platform-node/Terminal"
import * as Runtime from "@effect/platform-node/Runtime"
import * as Effect from "effect/Effect"
import * as Layer from "effect/Layer"

const colorPrompt = Prompt.select({
message: "Pick your favorite color",
Expand Down Expand Up @@ -64,10 +63,8 @@ const cli = CliApp.make({
command: Command.prompt("favorites", prompt)
})

const MainLive = Layer.merge(NodeContext.layer, Terminal.layer)

Effect.sync(() => process.argv.slice(2)).pipe(
Effect.flatMap((args) => CliApp.run(cli, args, (input) => Effect.log(input))),
Effect.provide(MainLive),
Effect.runFork
Effect.provide(NodeContext.layer),
Runtime.runMain
)
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
"coverage": "vitest run --coverage"
},
"peerDependencies": {
"@effect/platform": "^0.30.0",
"@effect/printer": "^0.23.0",
"@effect/printer-ansi": "^0.23.0",
"@effect/schema": "^0.48.0",
"@effect/platform": "^0.30.5",
"@effect/printer": "^0.23.1",
"@effect/printer-ansi": "^0.23.1",
"@effect/schema": "^0.48.3",
"effect": "2.0.0-next.55"
},
"devDependencies": {
Expand All @@ -74,12 +74,12 @@
"@effect/docgen": "^0.3.4",
"@effect/eslint-plugin": "^0.1.2",
"@effect/language-service": "^0.0.21",
"@effect/platform": "^0.30.2",
"@effect/platform-node": "^0.31.2",
"@effect/platform": "^0.30.5",
"@effect/platform-node": "^0.31.6",
"@effect/printer": "^0.23.1",
"@effect/printer-ansi": "^0.23.1",
"@effect/schema": "^0.48.0",
"@types/node": "^20.9.0",
"@effect/schema": "^0.48.3",
"@types/node": "^20.9.1",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
"@vitest/coverage-v8": "^0.34.6",
Expand All @@ -97,7 +97,7 @@
"madge": "^6.1.0",
"rimraf": "^5.0.5",
"stackframe": "^1.3.4",
"tsx": "^4.1.2",
"tsx": "^4.1.3",
"typescript": "^5.2.2",
"vite": "^5.0.0",
"vitest": "^0.34.6"
Expand Down
68 changes: 34 additions & 34 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions src/internal/cliApp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as Path from "@effect/platform/Path"
import type * as Terminal from "@effect/platform/Terminal"
import * as Console from "effect/Console"
import * as Context from "effect/Context"
Expand Down Expand Up @@ -100,6 +99,11 @@ export const run = dual<
// Internals
// =============================================================================

const getEnv = () =>
typeof process !== "undefined" && "env" in process && typeof process.env === "object"
? process.env
: {}

const printDocs = (error: HelpDoc.HelpDoc): Effect.Effect<never, never, void> =>
Console.log(InternalHelpDoc.toAnsiText(error))

Expand Down Expand Up @@ -142,33 +146,29 @@ const handleBuiltInOption = <A>(
return Console.log(InternalHelpDoc.toAnsiText(helpDoc))
}
case "ShowCompletionScript": {
return Effect.flatMap(Path.Path, (path) => {
const commandNames = ReadonlyArray.fromIterable(InternalCommand.getNames(self.command))
const programNames = ReadonlyArray.isNonEmptyReadonlyArray(commandNames)
? commandNames
: ReadonlyArray.of(self.name)
const script = InternalCompletion.getCompletionScript(
builtIn.pathToExecutable,
programNames,
builtIn.shellType,
path
)
return Console.log(script)
})
const commandNames = ReadonlyArray.fromIterable(InternalCommand.getNames(self.command))
const programNames = ReadonlyArray.isNonEmptyReadonlyArray(commandNames)
? commandNames
: ReadonlyArray.of(self.name)
const script = InternalCompletion.getCompletionScript(
builtIn.pathToExecutable,
programNames,
builtIn.shellType
)
return Console.log(script)
}
case "ShowCompletions": {
return Effect.all([
InternalCompgen.Tag,
Effect.sync(() => globalThis.process.env)
Effect.sync(getEnv)
]).pipe(Effect.flatMap(([compgen, env]) => {
const tupleOrder = Order.mapInput(Order.number, (tuple: [number, string]) => tuple[0])
const compWords = pipe(
ReadonlyRecord.collect(
env,
(key, value) =>
key.startsWith("COMP_WORD_") && value !== undefined
? Option.some<[number, string]>([key.replace("COMP_WORD_", "").length, value])
: Option.none()
env,
ReadonlyRecord.collect((key, value) =>
key.startsWith("COMP_WORD_") && value !== undefined
? Option.some<[number, string]>([key.replace("COMP_WORD_", "").length, value])
: Option.none()
),
ReadonlyArray.compact,
ReadonlyArray.sortBy(tupleOrder),
Expand Down
Loading

0 comments on commit 3428902

Please sign in to comment.