Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ async function cli(args: ParsedArgs, argv: string[]) {
isLocal: true,
})

await printCompletion(shell, { update })
const cachedBlueprint = await getApiBlueprint({ update })
printCompletion(shell, buildRegistry(cachedBlueprint).spec)
return
}

Expand Down
33 changes: 17 additions & 16 deletions src/lib/commands/local/completion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getApiBlueprint } from 'lib/blueprint/index.js'
import type { ApiBlueprint } from 'lib/blueprint/index.js'
import type { Command } from 'lib/commands/registry.js'
import type { CommandSpec } from 'lib/commands/spec.js'
import { getOutput } from 'lib/output/get-output.js'
import {
type CompletionShell,
Expand All @@ -16,19 +17,19 @@ import {
* Called by the entry before any auth or blueprint context exists, and by
* the registered command's executor — one implementation for both.
*/
export const printCompletion = async (
export const printCompletion = (
shell: CompletionShell,
{ update = false }: { update?: boolean } = {},
): Promise<void> => {
// Deferred import: the registry lists this module's commands, so a static
// import back into it would be a cycle.
const { buildRegistry } = await import('lib/commands/registry.js')
const blueprint = await getApiBlueprint({ update })
const { spec } = buildRegistry(blueprint)
spec: CommandSpec,
): void => {
getOutput().text(renderCompletion(shell, spec))
}

const completionCommand = (shell: CompletionShell): Command => ({
type BuildSpec = (blueprint: ApiBlueprint) => CommandSpec

const completionCommand = (
shell: CompletionShell,
buildSpec: BuildSpec,
): Command => ({
definition: {
path: ['completion', shell],
kind: 'cli',
Expand All @@ -37,14 +38,14 @@ const completionCommand = (shell: CompletionShell): Command => ({
flags: [],
},
requiresAuth: false,
execute: async ({ args }) => {
await printCompletion(shell, { update: args['update'] === true })
execute: async (_invocation, ctx) => {
printCompletion(shell, buildSpec(ctx.blueprint))
return { kind: 'done' }
},
})

export const completionCommands: Command[] = [
completionCommand('bash'),
completionCommand('fish'),
completionCommand('zsh'),
export const createCompletionCommands = (buildSpec: BuildSpec): Command[] => [
completionCommand('bash', buildSpec),
completionCommand('fish', buildSpec),
completionCommand('zsh', buildSpec),
]
6 changes: 5 additions & 1 deletion src/lib/commands/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ApiBlueprint } from 'lib/blueprint/index.js'
import type { CliContext } from 'lib/context.js'

import { executeApiCommand } from './api-command.js'
import { completionCommands } from './local/completion.js'
import { createCompletionCommands } from './local/completion.js'
import { configRevealLocationCommand } from './local/config-reveal-location.js'
import { configSetFakeServerCommand } from './local/config-set-fake-server.js'
import { configUseRemoteApiDefsCommand } from './local/config-use-remote-api-defs.js'
Expand Down Expand Up @@ -65,6 +65,10 @@ export interface CommandRegistry {
* blueprint. The single source of truth: the spec, the picker, and the
* dispatcher all consume this list.
*/
const completionCommands = createCompletionCommands(
(blueprint) => buildRegistry(blueprint).spec,
)

export const localCommands: Command[] = [
...completionCommands,
configRevealLocationCommand,
Expand Down
Loading