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

Function objectDef

src/lib/item-input/object.ts:119–241  ·  view source on GitHub ↗
(name: string, inputDefsByProperty: InputDefsByProperty<T>,
		options?: ObjectDefOptions<T>)

Source from the content-addressed store, hash-verified

117 * Calling `updateIfNeeded` on rolled up properties is not yet implemented.
118 */
119export function objectDef<T extends object>(name: string, inputDefsByProperty: InputDefsByProperty<T>,
120 options?: ObjectDefOptions<T>): InputDefinition<T> {
121 const buildFromUserInput = async (context: unknown[] = []): Promise<T | CancelAction> => {
122 // Since we're just going to run through all the options and ask for their value, we don't
123 // have a menu or any place to put help for the whole object so we'll just display it
124 // before asking the questions.
125 if (options?.helpText) {
126 console.log(`\n${options.helpText}\n`)
127 }
128 const retVal = {} as Partial<T>
129 for (const propertyName in inputDefsByProperty) {
130 const propertyInputDefinition = inputDefsByProperty[propertyName]
131 // Allow `undefined` definitions, but do nothing with them. This allows callers of
132 // `objectDef` to have a field skipped by setting it to `undefined` as well as by leaving
133 // it out.
134 if (!propertyInputDefinition) {
135 continue
136 }
137 const value = await propertyInputDefinition.buildFromUserInput([{ ...retVal }, ...context])
138 if (value !== cancelAction) {
139 retVal[propertyName] = value
140 } else {
141 return cancelAction
142 }
143 }
144 return retVal as T
145 }
146
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

Callers 7

getInputFromUserFunction · 0.85
getInputFromUserFunction · 0.85
getInputFromUserFunction · 0.85
buildInputDefinitionFunction · 0.85
object.test.tsFile · 0.85

Calls 1

Tested by

no test coverage detected