From 1a82786825185b4a482fb391207c546cde941893 Mon Sep 17 00:00:00 2001 From: Ross Stenersen Date: Tue, 10 Jun 2025 15:17:30 -0500 Subject: [PATCH] refactor: convert devices:rename command to yargs --- packages/cli/src/commands/devices/rename.ts | 40 ----------------- src/commands/devices/rename.ts | 50 +++++++++++++++++++++ src/commands/index.ts | 6 ++- 3 files changed, 54 insertions(+), 42 deletions(-) delete mode 100644 packages/cli/src/commands/devices/rename.ts create mode 100644 src/commands/devices/rename.ts diff --git a/packages/cli/src/commands/devices/rename.ts b/packages/cli/src/commands/devices/rename.ts deleted file mode 100644 index 45ee0aa5..00000000 --- a/packages/cli/src/commands/devices/rename.ts +++ /dev/null @@ -1,40 +0,0 @@ -import inquirer from 'inquirer' - -import { APICommand, chooseDevice, outputItem } from '@smartthings/cli-lib' - -import { buildTableOutput } from '../../lib/commands/devices-util.js' - - -export default class DeviceRenameCommand extends APICommand { - static description = 'rename a device' + - this.apiDocsURL('updateDevice') - - static flags = { - ...APICommand.flags, - ...outputItem.flags, - } - - static args = [ - { - name: 'id', - description: 'the device id', - }, - { - name: 'label', - description: 'the new device label', - }, - ] - - async run(): Promise { - const id = await chooseDevice(this, this.args.id) - - const label = this.args.label ?? (await inquirer.prompt({ - type: 'input', - name: 'label', - message: 'Enter new device label:', - })).label - - await outputItem(this, { buildTableOutput: data => buildTableOutput(this.tableGenerator, data) }, - () => this.client.devices.update(id, { label })) - } -} diff --git a/src/commands/devices/rename.ts b/src/commands/devices/rename.ts new file mode 100644 index 00000000..5172b7ed --- /dev/null +++ b/src/commands/devices/rename.ts @@ -0,0 +1,50 @@ +import { type ArgumentsCamelCase, type Argv, type CommandModule } from 'yargs' + +import { stringInput } from '../../lib/user-query.js' +import { apiCommand, apiCommandBuilder, type APICommandFlags, apiDocsURL } from '../../lib/command/api-command.js' +import { outputItem, outputItemBuilder, type OutputItemFlags } from '../../lib/command/output-item.js' +import { chooseDevice } from '../../lib/command/util/devices-choose.js' +import { buildTableOutput } from '../../lib/command/util/devices-table.js' + + +export type CommandArgs = + & APICommandFlags + & OutputItemFlags + & { + id?: string + newLabel?: string + } + +const command = 'devices:rename [id] [new-label]' + +const describe = 'rename a device' + +const builder = (yargs: Argv): Argv => + outputItemBuilder(apiCommandBuilder(yargs)) + .positional('id', { describe: 'device id', type: 'string' }) + .positional('new-label', { describe: 'new device label', type: 'string' }) + .example([ + ['$0 devices:rename', 'prompt for a device and a new label and rename the chosen device'], + [ + '$0 devices:rename f22bb71f-b6a0-43de-9131-20e7dfc0f973 "New Name"', + 'rename the specified device with the specified name', + ], + ]) + .epilog(apiDocsURL('updateDevice')) + +const handler = async (argv: ArgumentsCamelCase): Promise => { + const command = await apiCommand(argv) + + const id = await chooseDevice(command, argv.id) + + const label = argv.newLabel ?? await stringInput('Enter new device label:') + + await outputItem( + command, + { buildTableOutput: data => buildTableOutput(command.tableGenerator, data) }, + () => command.client.devices.update(id, { label }), + ) +} + +const cmd: CommandModule = { command, describe, builder, handler } +export default cmd diff --git a/src/commands/index.ts b/src/commands/index.ts index 98360328..898f699d 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -47,12 +47,13 @@ import devicesCommand from './devices.js' import devicesCapabilityStatusCommand from './devices/capability-status.js' 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 devicesPresentationCommand from './devices/presentation.js' +import devicesRenameCommand from './devices/rename.js' +import devicesStatusCommand from './devices/status.js' import devicesUpdateCommand from './devices/update.js' import edgeChannelsCommand from './edge/channels.js' import edgeChannelsCreateCommand from './edge/channels/create.js' @@ -167,12 +168,13 @@ export const commands: CommandModule[] = [ devicesCapabilityStatusCommand, devicesComponentStatusCommand, devicesCommandsCommand, - devicesStatusCommand, devicesDeleteCommand, devicesHealthCommand, devicesHistoryCommand, devicesPreferencesCommand, devicesPresentationCommand, + devicesRenameCommand, + devicesStatusCommand, devicesUpdateCommand, edgeChannelsCommand, edgeChannelsCreateCommand,