Skip to content

Commit

Permalink
feat: add hiddenAliases property to Command
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 11, 2023
1 parent f1925a7 commit 5bf0a2e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export abstract class Command {
/** Hide the command from help */
public static hidden: boolean

/** An array of aliases for this command that are hidden from help. */
public static hiddenAliases: string[] = []

/** A command ID, used mostly in error or verbose reporting. */
public static id: string

Expand Down Expand Up @@ -414,6 +417,7 @@ export namespace Command {
flags: {[name: string]: Flag.Cached}
hasDynamicHelp?: boolean
hidden: boolean
hiddenAliases: string[]
id: string
isESM?: boolean
permutations?: string[]
Expand Down
16 changes: 12 additions & 4 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,17 +784,15 @@ export class Config implements IConfig {
this.commandPermutations.add(permutation, command.id)
}

// set command aliases
for (const alias of command.aliases ?? []) {
const handleAlias = (alias: string, hidden = false) => {
if (this._commands.has(alias)) {
const prioritizedCommand = this.determinePriority([this._commands.get(alias)!, command])
this._commands.set(alias, {...prioritizedCommand, id: alias})
} else {
this._commands.set(alias, {...command, id: alias})
this._commands.set(alias, {...command, hidden, id: alias})
}

// set every permutation of the aliases

// v3 moved command alias permutations to the manifest, but some plugins may not have
// the new manifest yet. For those, we need to calculate the permutations here.
const aliasPermutations =
Expand All @@ -806,6 +804,16 @@ export class Config implements IConfig {
this.commandPermutations.add(permutation, command.id)
}
}

// set command aliases
for (const alias of command.aliases ?? []) {
handleAlias(alias)
}

// set hidden command aliases
for (const alias of command.hiddenAliases ?? []) {
handleAlias(alias, true)
}
}

marker?.addDetails({commandCount: plugin.commands.length})
Expand Down
2 changes: 1 addition & 1 deletion src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const help = (opts: Partial<BooleanFlag<boolean>> = {}): BooleanFlag<void
...opts,
async parse(_, cmd) {
const Help = await loadHelpClass(cmd.config)
await new Help(cmd.config, cmd.config.pjson.helpOptions ?? cmd.config.pjson.oclif.helpOptions).showHelp(
await new Help(cmd.config, cmd.config.pjson.oclif.helpOptions ?? cmd.config.pjson.helpOptions).showHelp(
cmd.id ? [cmd.id, ...cmd.argv] : cmd.argv,
)
cmd.exit(0)
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function run(argv?: string[], options?: Interfaces.LoadOptions): Pr
// display help version if applicable
if (helpAddition(argv, config)) {
const Help = await loadHelpClass(config)
const help = new Help(config, config.pjson.helpOptions ?? config.pjson.oclif.helpOptions)
const help = new Help(config, config.pjson.oclif.helpOptions ?? config.pjson.helpOptions)
await help.showHelp(argv)
await collectPerf()
return
Expand Down
1 change: 1 addition & 0 deletions src/util/cache-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export async function cacheCommand(
flags,
hasDynamicHelp: Object.values(flags).some((f) => f.hasDynamicHelp),
hidden: cmd.hidden,
hiddenAliases: cmd.hiddenAliases || [],
id: cmd.id,
pluginAlias: plugin && plugin.alias,
pluginName: plugin && plugin.name,
Expand Down

0 comments on commit 5bf0a2e

Please sign in to comment.