From f8c37be63e99e75f3373071a238a9ef0e0103605 Mon Sep 17 00:00:00 2001 From: Ross Stenersen Date: Tue, 10 Jun 2025 14:56:25 -0500 Subject: [PATCH] refactor: convert devices:health command to yargs --- packages/cli/src/commands/devices/health.ts | 22 --------- src/commands/devices/health.ts | 52 +++++++++++++++++++++ src/commands/index.ts | 2 + 3 files changed, 54 insertions(+), 22 deletions(-) delete mode 100644 packages/cli/src/commands/devices/health.ts create mode 100644 src/commands/devices/health.ts diff --git a/packages/cli/src/commands/devices/health.ts b/packages/cli/src/commands/devices/health.ts deleted file mode 100644 index 48b9ba7a..00000000 --- a/packages/cli/src/commands/devices/health.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { APICommand, chooseDevice, formatAndWriteItem } from '@smartthings/cli-lib' - - -export default class DeviceHealthCommand extends APICommand { - static description = 'get the current health status of a device' - - static flags = { - ...APICommand.flags, - ...formatAndWriteItem.flags, - } - - static args = [{ - name: 'id', - description: 'the device id', - }] - - async run(): Promise { - const deviceId = await chooseDevice(this, this.args.id, { allowIndex: true }) - const health = await this.client.devices.getHealth(deviceId) - await formatAndWriteItem(this, { tableFieldDefinitions: ['deviceId', 'state', 'lastUpdatedDate'] }, health) - } -} diff --git a/src/commands/devices/health.ts b/src/commands/devices/health.ts new file mode 100644 index 00000000..02e89e0a --- /dev/null +++ b/src/commands/devices/health.ts @@ -0,0 +1,52 @@ +import { type ArgumentsCamelCase, type Argv, type CommandModule } from 'yargs' + +import { + apiCommand, + apiCommandBuilder, + type APICommandFlags, +} from '../../lib/command/api-command.js' +import { + formatAndWriteItem, + formatAndWriteItemBuilder, + type FormatAndWriteItemFlags, +} from '../../lib/command/format.js' +import { chooseDevice } from '../../lib/command/util/devices-choose.js' + + +export type CommandArgs = + & APICommandFlags + & FormatAndWriteItemFlags + & { + idOrIndex?: string + } + +const command = 'devices:health [id-or-index]' + +const describe = 'get the current health status of a device' + +const builder = (yargs: Argv): Argv => + formatAndWriteItemBuilder(apiCommandBuilder(yargs)) + .positional('id-or-index', { describe: 'device id or number from list', type: 'string' }) + .example([ + ['$0 devices:health', 'choose a device from a list and display its health status'], + [ + '$0 devices:health 3', + 'display the health status for the third device listed when running "smartthings devices"', + ], + [ + '$0 devices:health a13a3fbb-2b97-4c73-978d-ae7c2fca3ea8', + 'display the health status for the specified device', + ], + ]) + + +const handler = async (argv: ArgumentsCamelCase): Promise => { + const command = await apiCommand(argv) + + const deviceId = await chooseDevice(command, argv.idOrIndex, { allowIndex: true }) + const health = await command.client.devices.getHealth(deviceId) + await formatAndWriteItem(command, { tableFieldDefinitions: ['deviceId', 'state', 'lastUpdatedDate'] }, health) +} + +const cmd: CommandModule = { command, describe, builder, handler } +export default cmd diff --git a/src/commands/index.ts b/src/commands/index.ts index 337e3b0c..22eb8b17 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -48,6 +48,7 @@ import devicesComponentStatusCommand from './devices/component-status.js' import devicesCommandsCommand from './devices/commands.js' import devicesStatusCommand from './devices/status.js' import devicesDeleteCommand from './devices/delete.js' +import devicesHealthCommand from './devices/health.js' import devicesHistoryCommand from './devices/history.js' import devicesPreferencesCommand from './devices/preferences.js' import devicesUpdateCommand from './devices/update.js' @@ -165,6 +166,7 @@ export const commands: CommandModule[] = [ devicesCommandsCommand, devicesStatusCommand, devicesDeleteCommand, + devicesHealthCommand, devicesHistoryCommand, devicesPreferencesCommand, devicesUpdateCommand,