-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathpresentation.ts
89 lines (71 loc) · 3.47 KB
/
presentation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Flags } from '@oclif/core'
import { CapabilityPresentation } from '@smartthings/core-sdk'
import { APIOrganizationCommand, formatAndWriteItem, TableGenerator } from '@smartthings/cli-lib'
import { capabilityIdOrIndexInputArgs, chooseCapability } from '../../lib/commands/capabilities-util'
export function buildTableOutput(tableGenerator: TableGenerator, presentation: CapabilityPresentation): string {
const basicInfo = tableGenerator.buildTableFromItem(presentation, ['id', 'version'])
let dashboardStates = 'No dashboard states'
if (presentation.dashboard?.states && presentation.dashboard.states.length > 0) {
const newDashboardStates = tableGenerator.buildTableFromList(presentation.dashboard.states, ['label',
{ label: 'Alternatives', value: (state) => (state.alternatives?.length?.toString() ?? 'none') },
'group',
])
dashboardStates = `Dashboard States\n${newDashboardStates}`
}
function buildDisplayTypeTable(items: { displayType: string }[]): string {
return tableGenerator.buildTableFromList(items, ['displayType'])
}
function buildLabelDisplayTypeTable(items: { label: string; displayType: string }[]): string {
const subTable = tableGenerator.newOutputTable({ head: ['Label', 'Display Type'] })
for (const item of items) {
subTable.push([item.label, item.displayType])
}
return subTable.toString()
}
let dashboardActions = 'No dashboard actions'
if (presentation.dashboard?.actions?.length) {
dashboardActions = `Dashboard Actions\n${buildDisplayTypeTable(presentation.dashboard.actions)}`
}
let dashboardBasicPlus = 'No dashboard basic plus items'
if (presentation.dashboard?.basicPlus?.length) {
dashboardBasicPlus = `Dashboard Basic Plus\n${buildDisplayTypeTable(presentation.dashboard.basicPlus)}`
}
let detailView = 'No Detail View Items'
if (presentation.detailView?.length) {
const subTable = tableGenerator.buildTableFromList(presentation.detailView, ['label', 'displayType'])
detailView = `Detail View Items\n${subTable}`
}
let automationConditions = 'No automation conditions'
if (presentation.automation?.conditions?.length) {
automationConditions = `Automation Conditions\n${buildLabelDisplayTypeTable(presentation.automation.conditions)}`
}
let automationActions = 'No automation actions'
if (presentation.automation?.actions?.length) {
automationActions = `Automation Actions\n${buildLabelDisplayTypeTable(presentation.automation.actions)}`
}
return `Basic Information\n${basicInfo}\n\n` +
`${dashboardStates}\n\n` +
`${dashboardActions}\n\n` +
`${dashboardBasicPlus}\n\n` +
`${detailView}\n\n` +
`${automationConditions}\n\n` +
automationActions
}
export default class PresentationsCommand extends APIOrganizationCommand<typeof PresentationsCommand.flags> {
static description = 'get presentation information for a specific capability' +
this.apiDocsURL('getCapabilityPresentation')
static flags = {
...APIOrganizationCommand.flags,
...formatAndWriteItem.flags,
namespace: Flags.string({
char: 'n',
description: 'a specific namespace to query; will use all by default',
}),
}
static args = capabilityIdOrIndexInputArgs
async run(): Promise<void> {
const capabilityId = await chooseCapability(this, this.args.id, this.args.version, undefined, this.flags.namespace)
const presentation = await this.client.capabilities.getPresentation(capabilityId.id, capabilityId.version)
await formatAndWriteItem(this, { buildTableOutput: data => buildTableOutput(this.tableGenerator, data) }, presentation)
}
}