Skip to content

Commit

Permalink
fix: show deprecation warning in help when using deprecated alias (#801)
Browse files Browse the repository at this point in the history
* fix: show deprecation warning in help when using deprecated alias

* chore: clean up
  • Loading branch information
mdonnalley authored Sep 29, 2023
1 parent 6c78fae commit 584a008
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/help/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,17 @@ export class Help extends HelpBase {
if (state) {
this.log(
state === 'deprecated'
? `${formatCommandDeprecationWarning(toConfiguredId(name, this.config), command.deprecationOptions)}`
? `${formatCommandDeprecationWarning(toConfiguredId(name, this.config), command.deprecationOptions)}\n`
: `This command is in ${state}.\n`,
)
}

if (command.deprecateAliases && command.aliases.includes(name)) {
const actualCmd = this.config.commands.find((c) => c.aliases.includes(name))
const opts = {...command.deprecationOptions, ...(actualCmd ? {to: actualCmd.id} : {})}
this.log(`${formatCommandDeprecationWarning(toConfiguredId(name, this.config), opts)}\n`)
}

const summary = this.summary(command)
if (summary) {
this.log(summary + '\n')
Expand Down
17 changes: 17 additions & 0 deletions test/help/fixtures/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,20 @@ export const DbTopic: Topic = {
name: 'db',
description: 'This topic is for the db topic',
}

// deprecateAliases
export class DeprecateAliases extends Command {
static id = 'foo:bar'

static aliases = ['foo:bar:alias']

static deprecateAliases = true

static flags = {}

static args = {}

async run(): Promise<void> {
'run'
}
}
27 changes: 27 additions & 0 deletions test/help/show-help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
AppsTopic,
DbCreate,
DbTopic,
DeprecateAliases,
} from './fixtures/fixtures'
import {Config, Interfaces} from '../../src'
import {monkeyPatchCommands} from './help-test-utils'
Expand Down Expand Up @@ -257,6 +258,32 @@ COMMANDS
apps:create Create an app
apps:destroy Destroy an app`)
})

test
.loadConfig()
.stdout()
.do(async (ctx) => {
const {config} = ctx
monkeyPatchCommands(config, [
{
name: 'plugin-1',
commands: [DeprecateAliases],
topics: [],
},
])

const help = new TestHelp(config as any)
await help.showHelp(['foo:bar:alias'])
})
.it('show deprecation warning when using alias', ({stdout}) => {
expect(stdout.trim()).to.equal(`The "foo:bar:alias" command has been deprecated. Use "foo:bar" instead.
USAGE
$ oclif foo:bar:alias
ALIASES
$ oclif foo:bar:alias`)
})
})

describe('showHelp for a command', () => {
Expand Down

0 comments on commit 584a008

Please sign in to comment.