Skip to content

Commit 5405881

Browse files
committed
refactor: convert presentation command to yargs
1 parent 3fc6282 commit 5405881

File tree

4 files changed

+87
-54
lines changed

4 files changed

+87
-54
lines changed

src/commands/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import locationsRoomsUpdateCommand from './locations/rooms/update.js'
8787
import logoutCommand from './logout.js'
8888
import organizationsCommand from './organizations.js'
8989
import organizationsCurrentCommand from './organizations/current.js'
90+
import presentationCommand from './presentation.js'
9091
import presentationDeviceConfigCommand from './presentation/device-config.js'
9192
import presentationDeviceConfigCreateCommand from './presentation/device-config/create.js'
9293
import presentationDeviceConfigGenerateCommand from './presentation/device-config/generate.js'
@@ -200,6 +201,7 @@ export const commands: CommandModule<object, any>[] = [
200201
logoutCommand,
201202
organizationsCommand,
202203
organizationsCurrentCommand,
204+
presentationCommand,
203205
presentationDeviceConfigCommand,
204206
presentationDeviceConfigCreateCommand,
205207
presentationDeviceConfigGenerateCommand,

src/commands/presentation.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { type ArgumentsCamelCase, type Argv, type CommandModule } from 'yargs'
2+
3+
import { type PresentationDevicePresentation } from '@smartthings/core-sdk'
4+
5+
import { apiCommand, apiCommandBuilder, apiDocsURL, type APICommandFlags } from '../lib/command/api-command.js'
6+
import { outputItem, outputItemBuilder, type OutputItemFlags } from '../lib/command/output-item.js'
7+
import { buildTableOutput } from '../lib/command/util/presentation-table.js'
8+
9+
10+
export type CommandArgs =
11+
& APICommandFlags
12+
& OutputItemFlags
13+
& {
14+
presentationId?: string
15+
manufacturerName?: string
16+
}
17+
18+
const command = 'presentation <presentationId> [manufacturer-name]'
19+
20+
const describe = 'query device presentation by presentation id'
21+
22+
const builder = (yargs: Argv): Argv<CommandArgs> =>
23+
outputItemBuilder(apiCommandBuilder(yargs))
24+
.positional(
25+
'presentation-id',
26+
{
27+
describe: 'system generated identifier that corresponds to a device presentation',
28+
type: 'string',
29+
required: true,
30+
},
31+
)
32+
.positional(
33+
'manufacturer-name',
34+
{
35+
describe: 'manufacturer name, defaults to SmartThingsCommunity',
36+
type: 'string',
37+
},
38+
)
39+
.example([
40+
[
41+
'$0 presentation fd4adb7f-4a23-4134-9b39-05ed889a03cf',
42+
'display the specified presentation, which is associated with the SmartThingsCommunity manufacturer id'],
43+
[
44+
'$0 devices presentation 4ea31e30-2aba-41c7-a3ec-8f97423d565a DoodadsInc',
45+
'display the specified presentation, which is associated with the DoodadsInc manufacturer id',
46+
],
47+
])
48+
.epilog('The language can be overridden by specifying an ISO language code with the "--language" option. If' +
49+
' "NONE" is specified for the language code then no language header is specified in the API request.\n\n' +
50+
apiDocsURL('getDevicePresentation'))
51+
52+
const handler = async (argv: ArgumentsCamelCase<CommandArgs>): Promise<void> => {
53+
const command = await apiCommand(argv)
54+
55+
const config = {
56+
buildTableOutput: (data: PresentationDevicePresentation) => buildTableOutput(command.tableGenerator, data),
57+
}
58+
await outputItem(
59+
command,
60+
config,
61+
// presentationId is required in the `positional` argument above so it will always be set here.
62+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
63+
() => command.client.presentation.getPresentation(argv.presentationId!, argv.manufacturerName))
64+
}
65+
66+
const cmd: CommandModule<object, CommandArgs> = { command, describe, builder, handler }
67+
export default cmd

src/commands/presentation/device-config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type CommandArgs =
1515

1616
const command = 'presentation:device-config <presentationId> [manufacturer-name]'
1717

18-
const describe = 'query device config by presentationId'
18+
const describe = 'query device config by presentation id'
1919

2020
const builder = (yargs: Argv): Argv<CommandArgs> =>
2121
outputItemBuilder(apiCommandBuilder(yargs))
@@ -25,9 +25,8 @@ const builder = (yargs: Argv): Argv<CommandArgs> =>
2525
required: true,
2626
})
2727
.positional('manufacturer-name', {
28-
describe: 'manufacturer name',
28+
describe: 'manufacturer name, defaults to SmartThingsCommunity',
2929
type: 'string',
30-
default: 'SmartThingsCommunity',
3130
})
3231
.example([
3332
[
Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
import { PresentationDevicePresentation } from '@smartthings/core-sdk'
1+
import { type PresentationDevicePresentation } from '@smartthings/core-sdk'
22

3-
import { APICommand, outputItem, TableGenerator } from '@smartthings/cli-lib'
3+
import { type TableGenerator } from '../../table-generator.js'
44

55

6-
export function buildTableOutput(tableGenerator: TableGenerator, presentation: PresentationDevicePresentation): string {
7-
const basicInfo = tableGenerator.buildTableFromItem(presentation, [
8-
{ prop: 'presentationId', label: 'Presentation ID' }, { prop: 'manufacturerName', label: 'Manufacturer Name' }, 'iconUrl',
9-
])
6+
export const buildTableOutput = (
7+
tableGenerator: TableGenerator,
8+
presentation: PresentationDevicePresentation,
9+
): string => {
10+
const basicInfo = tableGenerator.buildTableFromItem(
11+
presentation,
12+
[
13+
{ prop: 'presentationId', label: 'Presentation ID' },
14+
{ prop: 'manufacturerName', label: 'Manufacturer Name' },
15+
'iconUrl',
16+
],
17+
)
1018

1119
let dashboardStates = 'No dashboard states'
1220
if (presentation.dashboard?.states && presentation.dashboard.states.length > 0) {
@@ -24,15 +32,15 @@ export function buildTableOutput(tableGenerator: TableGenerator, presentation: P
2432
dashboardStates = `Dashboard States\n${subTable.toString()}`
2533
}
2634

27-
function buildDisplayTypeTable(items: { displayType: string }[]): string {
35+
const buildDisplayTypeTable = (items: { displayType: string }[]): string => {
2836
const subTable = tableGenerator.newOutputTable({ head: ['Display Type'] })
2937
for (const item of items) {
3038
subTable.push([item.displayType])
3139
}
3240
return subTable.toString()
3341
}
3442

35-
function buildLabelDisplayTypeTable(items: { label: string; displayType: string }[]): string {
43+
const buildLabelDisplayTypeTable = (items: { label: string; displayType: string }[]): string => {
3644
const subTable = tableGenerator.newOutputTable({ head: ['Label', 'Display Type'] })
3745
for (const item of items) {
3846
subTable.push([item.label, item.displayType])
@@ -81,46 +89,3 @@ export function buildTableOutput(tableGenerator: TableGenerator, presentation: P
8189
`${automationConditions}\n\n` +
8290
automationActions
8391
}
84-
85-
export default class PresentationCommand extends APICommand<typeof PresentationCommand.flags> {
86-
static description = 'query device presentation by vid' +
87-
this.apiDocsURL('getDevicePresentation')
88-
89-
static flags = {
90-
...APICommand.flags,
91-
...outputItem.flags,
92-
}
93-
94-
static args = [
95-
{
96-
name: 'presentationId',
97-
description: 'system generated identifier that corresponds to a device presentation',
98-
required: true,
99-
},
100-
{
101-
name: 'manufacturerName',
102-
description: 'manufacturer name. Defaults to SmartThingsCommunity',
103-
required: false,
104-
},
105-
]
106-
107-
static examples = [
108-
'$ smartthings presentation fd4adb7f-4a23-4134-9b39-05ed889a03cf',
109-
'$ smartthings presentation 4ea31e30-2aba-41c7-a3ec-8f97423d565a SmartThings',
110-
'$ smartthings presentation fd4adb7f-4a23-4134-9b39-05ed889a03cf --language=ko',
111-
'$ smartthings presentation fd4adb7f-4a23-4134-9b39-05ed889a03cf --language=NONE',
112-
'',
113-
'Specifying only the presentationId defaults to the "SmartThingsCommunity" manufacturer',
114-
'name and the language set for the computer\'s operating system. The language can be',
115-
'overridden by specifying an ISO language code. If "NONE" is specified for the language',
116-
'flag then no language header is specified in the API request',
117-
]
118-
119-
async run(): Promise<void> {
120-
const config = {
121-
buildTableOutput: (data: PresentationDevicePresentation) => buildTableOutput(this.tableGenerator, data),
122-
}
123-
await outputItem(this, config,
124-
() => this.client.presentation.getPresentation(this.args.presentationId, this.args.manufacturerName))
125-
}
126-
}

0 commit comments

Comments
 (0)