From 9f07cc39dd618b7f760cc301eee7240ffdca65e8 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Mon, 30 Sep 2024 10:04:24 -0600 Subject: [PATCH] fix: deprecated aliases bug --- src/command.ts | 5 +++-- test/command/command.test.ts | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/command.ts b/src/command.ts index e7fab154..dbf1652a 100644 --- a/src/command.ts +++ b/src/command.ts @@ -283,7 +283,8 @@ export abstract class Command { // Since config.runHook will only run the hook for the root plugin, hookResult.successes will always have a length of 0 or 1 // But to be extra safe, we find the result that matches the root plugin. const argvToParse = hookResult.successes?.length - ? hookResult.successes.find((s) => s.plugin.root === Cache.getInstance().get('rootPlugin')?.root)?.result ?? argv + ? (hookResult.successes.find((s) => s.plugin.root === Cache.getInstance().get('rootPlugin')?.root)?.result ?? + argv) : argv this.argv = [...argvToParse] const results = await Parser.parse(argvToParse, opts) @@ -336,7 +337,7 @@ export abstract class Command { ) if (aliases.length === 0) return - const foundAliases = aliases.filter((alias) => this.argv.some((a) => a.startsWith(alias))) + const foundAliases = aliases.filter((alias) => this.argv.includes(alias)) for (const alias of foundAliases) { let preferredUsage = `--${flagDef?.name}` if (flagDef?.char) { diff --git a/test/command/command.test.ts b/test/command/command.test.ts index 3fd03cce..4b1357e0 100644 --- a/test/command/command.test.ts +++ b/test/command/command.test.ts @@ -95,7 +95,7 @@ describe('command', () => { class CMD extends Command { static flags = { name: Flags.string({ - aliases: ['username', 'target-user', 'u'], + aliases: ['username', 'target-user', 'u', 'nam'], deprecateAliases: true, char: 'o', }), @@ -137,6 +137,11 @@ describe('command', () => { const {stderr} = await captureOutput(async () => CMD.run(['--name', 'u'])) expect(stderr).to.be.empty }) + + it('shows no warning when using flag with deprecated alias that starts with the actual flag', async () => { + const {stderr} = await captureOutput(async () => CMD.run(['--name', 'astro'])) + expect(stderr).to.be.empty + }) }) describe('deprecated flags', () => {