(commandName: string)
| 12 | subCommands: SubCommand[] |
| 13 | } |
| 14 | export const findTopicsAndSubcommands = (commandName: string): CommandStructure => { |
| 15 | const topicName = `${commandName}:` |
| 16 | // Topics are commands that also have sub-commands. |
| 17 | const topics = new Set<string>() |
| 18 | const subCommands: SubCommand[] = [] |
| 19 | const related = (other: CommandModule): false | string => { |
| 20 | if (!other.command) { |
| 21 | return false |
| 22 | } |
| 23 | |
| 24 | if (typeof other.command === 'string' && other.command.startsWith(topicName)) { |
| 25 | return other.command |
| 26 | } |
| 27 | if (typeof other.command === 'object') { |
| 28 | const match = other.command.find(name => name.startsWith(topicName)) |
| 29 | return match ?? false |
| 30 | } |
| 31 | |
| 32 | return false |
| 33 | } |
| 34 | for (const other of commands) { |
| 35 | const relatedCommandCommand = related(other) |
| 36 | if (relatedCommandCommand) { |
| 37 | const relatedCommandName = relatedCommandCommand.split(' ')[0] |
| 38 | const subPart = relatedCommandName.slice(topicName.length) |
| 39 | if (subPart.indexOf(':') !== -1) { |
| 40 | // A sub-command of a command. Grab the first part for a topic. |
| 41 | topics.add(`${topicName}:${subPart.split(':')[0]}`) |
| 42 | } else { |
| 43 | subCommands.push({ command: other, relatedName: relatedCommandName }) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | return { topics: (topics.size ? [...topics] : []).sort(), subCommands } |
| 48 | } |
no test coverage detected