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: show deprecation warning in help when using deprecated alias #801

Merged
merged 2 commits into from
Sep 29, 2023
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
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