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

feat: make flag deprecation warnings less noisy #527

Merged
merged 1 commit into from Oct 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/command.ts
Expand Up @@ -246,20 +246,23 @@ export default abstract class Command {
const g: any = global
g['http-call'] = g['http-call'] || {}
g['http-call']!.userAgent = this.config.userAgent
this.checkForDeprecations()
this.warnIfCommandDeprecated()
}

protected checkForDeprecations() {
protected warnIfFlagDeprecated(flags: Record<string, unknown>) {
for (const flag of Object.keys(flags)) {
const deprecated = this.ctor.flags[flag]?.deprecated
if (deprecated) {
this.warn(formatFlagDeprecationWarning(flag, deprecated))
}
}
}

protected warnIfCommandDeprecated(): void {
if (this.ctor.state === 'deprecated') {
const cmdName = toConfiguredId(this.ctor.id, this.config)
this.warn(formatCommandDeprecationWarning(cmdName, this.ctor.deprecationOptions))
}

for (const [flag, opts] of Object.entries(this.ctor.flags ?? {})) {
if (opts.deprecated) {
this.warn(formatFlagDeprecationWarning(flag, opts.deprecated))
}
}
}

protected async parse<F extends Interfaces.FlagOutput, G extends Interfaces.FlagOutput, A extends { [name: string]: any }>(options?: Interfaces.Input<F, G>, argv = this.argv): Promise<Interfaces.ParserOutput<F, G, A>> {
Expand All @@ -268,7 +271,10 @@ export default abstract class Command {
// the spread operator doesn't work with getters so we have to manually add it here
opts.flags = options?.flags
opts.args = options?.args
return Parser.parse(argv, opts)
const results = await Parser.parse<F, G, A>(argv, opts)
this.warnIfFlagDeprecated(results.flags ?? {})

return results
}

protected async catch(err: Interfaces.CommandError): Promise<any> {
Expand Down
35 changes: 32 additions & 3 deletions test/command/command.test.ts
Expand Up @@ -246,18 +246,47 @@ describe('command', () => {
version: '2.0.0',
},
}),
force: Flags.boolean(),
}

async run() {
await this.parse(CMD)
this.log('running command')
}
}
await CMD.run([])
await CMD.run(['--name', 'astro'])
})
.do(ctx => expect(ctx.stderr).to.include('Warning: The "name" flag has been deprecated'))
.it('shows warning for deprecated flags')
})

describe('deprecated flags that are not provided', () => {
fancy
.stdout()
.stderr()
.do(async () => {
class CMD extends Command {
static flags = {
name: Flags.string({
deprecated: {
to: '--full-name',
version: '2.0.0',
},
}),
force: Flags.boolean(),
}

async run() {
await this.parse(CMD)
this.log('running command')
}
}
await CMD.run(['--force'])
})
.do(ctx => expect(ctx.stderr).to.not.include('Warning: The "name" flag has been deprecated'))
.it('does not show warning for deprecated flags if they are not provided')
})

describe('deprecated state', () => {
fancy
.stdout()
Expand All @@ -273,7 +302,7 @@ describe('command', () => {
await CMD.run([])
})
.do(ctx => expect(ctx.stderr).to.include('Warning: The "my:command" command has been deprecated'))
.it('shows warning for deprecated flags')
.it('shows warning for deprecated command')
})

describe('deprecated state with options', () => {
Expand All @@ -300,7 +329,7 @@ describe('command', () => {
expect(ctx.stderr).to.include('in version 2.0.0')
expect(ctx.stderr).to.include('Use "my:other:command" instead')
})
.it('shows warning for deprecated flags')
.it('shows warning for deprecated command with custom options')
})

describe('stdout err', () => {
Expand Down