MCPcopy Create free account
hub / github.com/SmartThingsCommunity/smartthings-cli / updateFromUserInput

Function updateFromUserInput

src/lib/item-input/object.ts:149–234  ·  view source on GitHub ↗
(original: T, context: unknown[] = [])

Source from the content-addressed store, hash-verified

147 const summarizeForEdit = options?.summarizeForEdit ?? defaultSummarizeForEditFn(name)
148
149 const updateFromUserInput = async (original: T, context: unknown[] = []): Promise<T | CancelAction> => {
150 const updated = { ...original }
151 while (true) {
152 const contextForChildren = [{ ...updated }, ...context]
153 const choices = buildPropertyChoices(inputDefsByProperty, updated, contextForChildren)
154 choices.push(new Separator())
155 if (options?.helpText) {
156 choices.push(helpOption)
157 }
158 choices.push(finishOption(name))
159 choices.push(cancelOption)
160
161 const action = await select({ message: name, choices, default: finishAction, pageSize: inquirerPageSize })
162
163 if (action === helpAction) {
164 console.log(`\n${options?.helpText}\n`)
165 } else if (action === cancelAction) {
166 return cancelAction
167 } else if (action === finishAction) {
168 return updated
169 } else {
170 const props = action.split('.')
171
172 const updatedPropertyName = props[0] as keyof T
173 const propertyInputDefinition = inputDefsByProperty[updatedPropertyName]
174 if (props.length === 1) {
175 // top-level property
176 const updatedPropertyValue = await propertyInputDefinition
177 .updateFromUserInput(updated[updatedPropertyName], contextForChildren)
178 if (updatedPropertyValue !== cancelAction && updated[updatedPropertyName] !== updatedPropertyValue) {
179 updated[updatedPropertyName] = updatedPropertyValue
180 let afterUpdatedProperty = false
181 for (const propertyName in inputDefsByProperty) {
182 if (afterUpdatedProperty) {
183 const laterPropertyInputDefinition = inputDefsByProperty[propertyName]
184 const updateIfNeeded = laterPropertyInputDefinition.updateIfNeeded
185 if (updateIfNeeded) {
186 const laterPropertyValue = await updateIfNeeded(updated[propertyName], updatedPropertyName, [{ ...updated }, ...context])
187 if (laterPropertyValue !== cancelAction) {
188 updated[propertyName] = laterPropertyValue
189 }
190 }
191 } else if (propertyName === updatedPropertyName) {
192 afterUpdatedProperty = true
193 }
194 }
195 }
196 } else {
197 // nested property that is rolled up
198 const nestedPropertyName = props[1]
199 const itemTypeData = propertyInputDefinition.itemTypeData as ObjectItemTypeData<unknown>
200
201 const objectValue = updated[updatedPropertyName] as { [key: string]: unknown }
202
203 const nestedPropertyInputDefinition =
204 (itemTypeData.inputDefsByProperty as { [key: string]: InputDefinition<unknown> })[nestedPropertyName]
205
206 const updatedPropertyValue = await nestedPropertyInputDefinition.updateFromUserInput(

Callers

nothing calls this directly

Calls 3

buildPropertyChoicesFunction · 0.85
finishOptionFunction · 0.85
updateIfNeededFunction · 0.85

Tested by

no test coverage detected