MCPcopy Create free account
hub / github.com/SmartThingsCommunity/smartthings-cli / findTopicsAndSubcommands

Function findTopicsAndSubcommands

src/lib/command-util.ts:14–48  ·  view source on GitHub ↗
(commandName: string)

Source from the content-addressed store, hash-verified

12 subCommands: SubCommand[]
13}
14export 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}

Callers 2

buildEpilogFunction · 0.85

Calls 1

relatedFunction · 0.85

Tested by

no test coverage detected