Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deprecated aliases bug #1208

Merged
merged 1 commit into from
Oct 1, 2024
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: 3 additions & 2 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<F, B, A>(argvToParse, opts)
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion test/command/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
Expand Down Expand Up @@ -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', () => {
Expand Down
Loading