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
5 changes: 5 additions & 0 deletions .changeset/fix-root-cli-command-aliases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"incur": patch
---

Fixed Root CLIs created with `Cli.create` and `aliases` not registering those aliases as command aliases when mounted via `cli.command()`.
11 changes: 11 additions & 0 deletions src/Cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4617,4 +4617,15 @@ describe('command aliases', () => {
const { output } = await serve(makeAliasedCli(), ['exten'])
expect(output).toMatch(/did you mean.*extension/i)
})

test('root CLI aliases register as command aliases', async () => {
const update = Cli.create('update', {
aliases: ['upgrade'],
description: 'Update packages',
run: () => ({ result: 'updated' }),
})
const cli = Cli.create('pkg').command(update)
const { output } = await serve(cli, ['upgrade'])
expect(output).toContain('updated')
})
})
7 changes: 7 additions & 0 deletions src/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ export function create(
const mountedRootDef = toRootDefinition.get(nameOrCli)
if (mountedRootDef) {
commands.set(nameOrCli.name, mountedRootDef)
const rootAliases = toRootAliases.get(nameOrCli)
if (rootAliases)
for (const a of rootAliases) commands.set(a, { _alias: true, target: nameOrCli.name })
return cli
}
const sub = nameOrCli as Cli
Expand Down Expand Up @@ -308,6 +311,7 @@ export function create(
}

if (rootDef) toRootDefinition.set(cli as unknown as Root, rootDef)
if (rootDef && def.aliases) toRootAliases.set(cli as unknown as Root, def.aliases)
if (def.options) toRootOptions.set(cli, def.options)
if (def.config !== undefined) toConfigEnabled.set(cli, true)
if (def.outputPolicy) toOutputPolicy.set(cli, def.outputPolicy)
Expand Down Expand Up @@ -2414,6 +2418,9 @@ export const toConfigEnabled = new WeakMap<Cli, boolean>()
/** @internal Maps CLI instances to their output policy. */
const toOutputPolicy = new WeakMap<Cli, OutputPolicy>()

/** @internal Maps root CLI instances to their command aliases. */
const toRootAliases = new WeakMap<Root, string[]>()

/** @internal Sentinel symbol for `ok()` and `error()` return values. */
const sentinel = Symbol.for('incur.sentinel')

Expand Down
Loading