Skip to content

Commit

Permalink
chore: expose methods for getting defined commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Feb 25, 2022
1 parent 539060c commit 305bf10
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {Options, Plugin as IPlugin} from '../interfaces/plugin'
import {Config as IConfig, ArchTypes, PlatformTypes, LoadOptions} from '../interfaces/config'
import {Command, Hook, Hooks, PJSON, Topic} from '../interfaces'
import * as Plugin from './plugin'
import {Debug, compact, loadJSON, uniq, collectUsableParts, getCommandIdPermutations} from './util'
import {Debug, compact, loadJSON, collectUsableParts, getCommandIdPermutations} from './util'
import {isProd} from '../util'
import ModuleLoader from '../module-loader'
import {getHelpFlagAdditions} from '../help/util'
Expand Down Expand Up @@ -391,7 +391,7 @@ export class Config implements IConfig {
* @param argv string[]
* @returns string[]
*/
findMatches(partialCmdId: string, argv: string[]): string[] {
findMatches(partialCmdId: string, argv: string[]): Command.Plugin[] {
const flags = argv.filter(arg => !getHelpFlagAdditions(this).includes(arg) && arg.startsWith('-')).map(a => a.replace(/-/g, ''))
const possibleMatches = [...this.permutationIndex.get(partialCmdId)].map(k => this.commandIndex.get(k)!)

Expand All @@ -403,9 +403,8 @@ export class Config implements IConfig {
// A command is a match if the provided flags belong to the full command
return flags.every(f => cmdFlags.includes(f))
})
.map(command => command.id)

return uniq(matches)
return matches
}

/**
Expand All @@ -432,6 +431,14 @@ export class Config implements IConfig {
return collectUsableParts(this.commandIDs)
}

getDefinedCommands(): Command.Plugin[] {
return [...this.commandIndex.values()]
}

getDefinedCommandIDs(): string[] {
return this.getDefinedCommands().map(c => c.id)
}

get commands(): Command.Plugin[] {
const commands = [...this.commandIndex.values()]
const validPermutations = [...this.permutationIndex.getValid()]
Expand Down Expand Up @@ -627,6 +634,7 @@ export class Config implements IConfig {
}

private loadTopics(plugin: IPlugin) {
if (this.flexibleTaxonomy) return
for (const topic of compact(plugin.topics)) {
const existing = this.topicIndex.get(topic.name)
if (existing) {
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export interface Config {
findCommand(id: string, opts?: { must: boolean }): Command.Plugin | undefined;
findTopic(id: string, opts: { must: true }): Topic;
findTopic(id: string, opts?: { must: boolean }): Topic | undefined;
findMatches(id: string, argv: string[]): string[];
findMatches(id: string, argv: string[]): Command.Plugin[];
collectUsableIds(): string[];
scopedEnvVar(key: string): string | undefined;
scopedEnvVarKey(key: string): string;
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface Hooks {
return: unknown;
};
'command_incomplete': {
options: {id: string; argv: string[], matches: string[]};
options: {id: string; argv: string[], matches: Command.Plugin[]};
return: unknown;
};
'plugins:preinstall': {
Expand Down

0 comments on commit 305bf10

Please sign in to comment.