Skip to content

Commit

Permalink
feat: make flag deprecation warnings less noisy (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 17, 2022
1 parent f65f31a commit a5fb337
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
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

0 comments on commit a5fb337

Please sign in to comment.