Skip to content

Commit c3ebcc9

Browse files
committed
refactor: update device profile and preference create commands to use new inquirer
1 parent 853392f commit c3ebcc9

2 files changed

Lines changed: 40 additions & 62 deletions

File tree

‎src/commands/devicepreferences/create.ts‎

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import inquirer from 'inquirer'
1+
import { select } from '@inquirer/prompts'
22
import { type ArgumentsCamelCase, type Argv, type CommandModule } from 'yargs'
33

44
import { type DevicePreferenceCreate, type PreferenceType } from '@smartthings/core-sdk'
@@ -17,6 +17,7 @@ import {
1717
import { userInputProcessor } from '../../lib/command/input-processor.js'
1818
import { tableFieldDefinitions } from '../../lib/command/util/devicepreferences-util.js'
1919
import {
20+
booleanInput,
2021
optionalIntegerInput,
2122
optionalNumberInput,
2223
optionalStringInput,
@@ -64,19 +65,13 @@ const getInputFromUser = async (): Promise<DevicePreferenceCreate> => {
6465
const title = await stringInput('Preference title:')
6566
const description = await optionalStringInput('Preference description:')
6667

67-
const required = (await inquirer.prompt({
68-
type: 'confirm',
69-
name: 'value',
70-
message: 'Is the preference required?',
71-
default: false,
72-
})).value as boolean
68+
const required = await booleanInput('Is the preference required?', { default: false })
7369

74-
const preferenceType = (await inquirer.prompt({
75-
type: 'list',
76-
name: 'preferenceType',
70+
const preferenceTypeChoices: PreferenceType[] = ['integer', 'number', 'boolean', 'string', 'enumeration']
71+
const preferenceType = await select({
7772
message: 'Choose a type for your preference.',
78-
choices: ['integer', 'number', 'boolean', 'string', 'enumeration'],
79-
})).preferenceType as PreferenceType
73+
choices: preferenceTypeChoices,
74+
}) as PreferenceType
8075

8176
const base = {
8277
name, title, description, required,
@@ -115,14 +110,12 @@ const getInputFromUser = async (): Promise<DevicePreferenceCreate> => {
115110
}
116111

117112
if (preferenceType === 'boolean') {
118-
const defaultValue = (await inquirer.prompt({
119-
type: 'list',
120-
name: 'defaultValue',
113+
const defaultValue = await select({
121114
message: 'Choose a default value.',
122115
choices: [{ name: 'none', value: undefined },
123116
{ name: 'true', value: true },
124117
{ name: 'false', value: false }],
125-
})).defaultValue as boolean | undefined
118+
})
126119
return {
127120
...base, preferenceType, definition: {
128121
default: defaultValue ?? undefined,
@@ -136,13 +129,11 @@ const getInputFromUser = async (): Promise<DevicePreferenceCreate> => {
136129
'Optional maximum length.',
137130
{ validate: numberValidateFn({ min: minLength || 1 }) },
138131
)
139-
const stringType = (await inquirer.prompt({
140-
type: 'list',
141-
name: 'stringType',
132+
const stringType = await select({
142133
message: 'Choose a type of string.',
143134
choices: ['text', 'password', 'paragraph'],
144135
default: 'text',
145-
})).stringType as 'text' | 'password' | 'paragraph'
136+
}) as 'text' | 'password' | 'paragraph'
146137
const defaultValue = await optionalStringInput('Optional default value.', {
147138
validate: input => {
148139
if (minLength !== undefined && input.length < minLength) {
@@ -179,15 +170,13 @@ const getInputFromUser = async (): Promise<DevicePreferenceCreate> => {
179170
}
180171
} while (name)
181172

182-
const defaultValue = (await inquirer.prompt({
183-
type: 'list',
184-
name: 'defaultValue',
173+
const defaultValue = await select({
185174
message: 'Choose a default option.',
186175
choices: [
187176
{ name: 'none', value: undefined },
188177
...Object.entries(options).map(([name, value]) => ({ name: `${value} (${name})`, value: name }))],
189178
default: undefined,
190-
})).defaultValue as 'text' | 'password' | 'paragraph'
179+
})
191180

192181
return {
193182
...base, preferenceType, definition: {

‎src/lib/command/util/deviceprofiles-create.ts‎

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import inquirer from 'inquirer'
1+
import { input, select } from '@inquirer/prompts'
22

33
import {
44
type DeviceProfile,
@@ -8,11 +8,12 @@ import {
88
type SmartThingsClient,
99
} from '@smartthings/core-sdk'
1010

11+
import { optionalStringInput, stringInput } from '../../user-query.js'
12+
import { fatalError } from '../../util.js'
13+
import { APICommand } from '../api-command.js'
1114
import { chooseCapabilityFiltered } from './capabilities-choose.js'
1215
import { type CapabilityId } from './capabilities-util.js'
1316
import { cleanupForCreate, cleanupForUpdate, DeviceDefinitionRequest } from './deviceprofiles-util.js'
14-
import { APICommand } from '../api-command.js'
15-
import { fatalError } from '../../util.js'
1617

1718

1819
const capabilitiesWithoutPresentations = ['healthCheck', 'execute']
@@ -128,18 +129,20 @@ export const capabilityDefined = async (client: SmartThingsClient, idStr: string
128129
}
129130
}
130131

131-
export const promptAndAddCapability = async (command: APICommand, deviceProfile: DeviceProfileRequest, componentId: string, prompt = 'Capability ID'): Promise<CapabilityId> => {
132+
export const promptAndAddCapability = async (
133+
command: APICommand,
134+
deviceProfile: DeviceProfileRequest,
135+
componentId: string,
136+
prompt = 'Capability Id',
137+
): Promise<CapabilityId> => {
132138
let capabilityId: CapabilityId = { id: '', version: 0 }
133-
const idStr = (await inquirer.prompt({
134-
type: 'input',
135-
name: 'id',
139+
const idStr = await input({
136140
message: `${prompt} (type ? for a list):`,
137-
validate: async (input) => {
138-
return (input.endsWith('?') || input === '' || await capabilityDefined(command.client, input))
141+
validate: async (input) =>
142+
(input.endsWith('?') || input === '' || await capabilityDefined(command.client, input))
139143
? true
140-
: `Invalid ID "${input}". Please enter a valid capability ID or ? for a list of available capabilities.`
141-
},
142-
})).id
144+
: `Invalid Id "${input}". Please enter a valid capability Id or ? for a list of available capabilities.`,
145+
})
143146

144147
if (idStr) {
145148
if (idStr.endsWith('?')) {
@@ -162,34 +165,25 @@ export const promptAndAddCapability = async (command: APICommand, deviceProfile:
162165
return capabilityId
163166
}
164167

168+
const componentRegExp = new RegExp(/^[0-9a-zA-Z]{1,100}$/)
165169
export const promptAndAddComponent = async (deviceProfile: DeviceProfileRequest, previousComponentId: string): Promise<string> => {
166170
const components = deviceProfile.components || []
167-
let componentId: string = (await inquirer.prompt({
168-
type: 'input',
169-
name: 'componentId',
170-
message: 'ComponentId ID: ',
171-
validate: (input) => {
172-
return (new RegExp(/^[0-9a-zA-Z]{1,100}$/).test(input) && !components.find(it => it.id === input)) || 'Invalid component name'
173-
},
174-
})).componentId
171+
const componentId = await optionalStringInput('Component Id:', {
172+
validate: input => (componentRegExp.test(input) && !components.find(it => it.id === input)) || 'Invalid component name',
173+
})
175174

176175
if (componentId) {
177176
components.push({ id: componentId, capabilities: [] })
178-
} else {
179-
componentId = previousComponentId
177+
return componentId
180178
}
181-
return componentId
179+
return previousComponentId
182180
}
183181

182+
const profileNameRegExp = new RegExp(/^(?!\s)[-_!.~'() *0-9a-zA-Z]{1,100}(?<!\s)$/)
184183
export const getInputFromUser = async (command: APICommand): Promise<DeviceProfileRequest> => {
185-
const name = (await inquirer.prompt({
186-
type: 'input',
187-
name: 'deviceProfileName',
188-
message: 'Device Profile Name:',
189-
validate: (input: string) => {
190-
return new RegExp(/^(?!\s)[-_!.~'() *0-9a-zA-Z]{1,100}(?<!\s)$/).test(input) || 'Invalid device profile name'
191-
},
192-
})).deviceProfileName
184+
const name = await stringInput('Device Profile Name:', {
185+
validate: (input: string) => profileNameRegExp.test(input) || 'Invalid device profile name',
186+
})
193187

194188
const deviceProfile: DeviceProfileRequest = {
195189
name,
@@ -203,7 +197,7 @@ export const getInputFromUser = async (command: APICommand): Promise<DeviceProfi
203197

204198
let primaryCapabilityId: CapabilityId
205199
do {
206-
primaryCapabilityId = await promptAndAddCapability(command, deviceProfile, 'main', 'Primary capability ID')
200+
primaryCapabilityId = await promptAndAddCapability(command, deviceProfile, 'main', 'Primary capability Id')
207201
} while (!primaryCapabilityId.id)
208202

209203
const enum Action {
@@ -216,12 +210,7 @@ export const getInputFromUser = async (command: APICommand): Promise<DeviceProfi
216210
let componentId = 'main'
217211
const choices = [Action.ADD_CAPABILITY, Action.ADD_COMPONENT, Action.FINISH]
218212
do {
219-
action = (await inquirer.prompt({
220-
type: 'list',
221-
name: 'action',
222-
message: 'Select an action...',
223-
choices,
224-
})).action
213+
action = await select({ message: 'Select an action...', choices })
225214

226215
if (action === Action.ADD_CAPABILITY) {
227216
await promptAndAddCapability(command, deviceProfile, componentId)

0 commit comments

Comments
 (0)