1- import inquirer from 'inquirer'
1+ import { input , select } from '@ inquirer/prompts '
22
33import {
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'
1114import { chooseCapabilityFiltered } from './capabilities-choose.js'
1215import { type CapabilityId } from './capabilities-util.js'
1316import { cleanupForCreate , cleanupForUpdate , DeviceDefinitionRequest } from './deviceprofiles-util.js'
14- import { APICommand } from '../api-command.js'
15- import { fatalError } from '../../util.js'
1617
1718
1819const 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 - 9 a - z A - Z ] { 1 , 100 } $ / )
165169export 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 - 9 a - z A - 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 - 9 a - z A - Z ] { 1 , 100 } (?< ! \s ) $ / )
184183export 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 - 9 a - z A - 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