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: warn about alias deprecations #540
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ import {PrettyPrintableError} from './errors' | |
import * as Parser from './parser' | ||
import * as Flags from './flags' | ||
import {Deprecation} from './interfaces/parser' | ||
import {formatCommandDeprecationWarning, formatFlagDeprecationWarning} from './help/util' | ||
import {formatCommandDeprecationWarning, formatFlagDeprecationWarning, normalizeArgv} from './help/util' | ||
|
||
const pjson = require('../package.json') | ||
|
||
|
@@ -62,6 +62,11 @@ export default abstract class Command { | |
|
||
static deprecationOptions?: Deprecation; | ||
|
||
/** | ||
* Emit deprecation warning when a command alias is used | ||
*/ | ||
static deprecateAliases?: boolean | ||
|
||
/** | ||
* An override string (or strings) for the default usage documentation. | ||
*/ | ||
|
@@ -255,10 +260,27 @@ export default abstract class Command { | |
if (deprecated) { | ||
this.warn(formatFlagDeprecationWarning(flag, deprecated)) | ||
} | ||
|
||
const deprecateAliases = this.ctor.flags[flag]?.deprecateAliases | ||
const aliases = this.ctor.flags[flag]?.aliases ?? [] | ||
if (deprecateAliases && aliases) { | ||
const foundAliases = this.argv.filter(a => aliases.includes(a.replace(/-/g, ''))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's happening with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. argv has the But the flag aliases don't have the prefix, e.g. So in order to find the intersection, we have to remove all the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens for |
||
for (const alias of foundAliases) { | ||
this.warn(formatFlagDeprecationWarning(alias, {to: this.ctor.flags[flag]?.name})) | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected warnIfCommandDeprecated(): void { | ||
const [id] = normalizeArgv(this.config) | ||
|
||
if (this.ctor.deprecateAliases && this.ctor.aliases.includes(id)) { | ||
const cmdName = toConfiguredId(this.ctor.id, this.config) | ||
const aliasName = toConfiguredId(id, this.config) | ||
this.warn(formatCommandDeprecationWarning(aliasName, {to: cmdName})) | ||
} | ||
|
||
if (this.ctor.state === 'deprecated') { | ||
const cmdName = toConfiguredId(this.ctor.id, this.config) | ||
this.warn(formatCommandDeprecationWarning(cmdName, this.ctor.deprecationOptions)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ export const help = (opts: Partial<BooleanFlag<boolean>> = {}) => { | |
description: 'Show CLI help.', | ||
...opts, | ||
parse: async (_: any, cmd: Command) => { | ||
new Help(cmd.config).showHelp(cmd.argv) | ||
new Help(cmd.config).showHelp([cmd.id!, ...cmd.argv]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if it matters or not, but I have seen scenarios where a command doesn't have an ID, particularly when we do weird things with them in UT. One of those "I'm super cautious about asserting things to TS" |
||
cmd.exit(0) | ||
}, | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aliases
will always be true since it's?? []
(empty arrays are truthy).Did you maybe want aliases.length?