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

Function editList

src/lib/item-input/array.ts:111–158  ·  view source on GitHub ↗
(list: T[], context: unknown[])

Source from the content-addressed store, hash-verified

109
110 // Common code from build and edit
111 const editList = async (list: T[], context: unknown[]): Promise<FinishAction | CancelAction> => {
112 while (true) {
113 const choices: (Choice<symbol | number>)[] = list.map((item, index) => ({
114 name: `Edit ${itemSummary(item)}.`,
115 value: index,
116 }))
117
118 if (choices.length > 0) {
119 choices.push(new Separator())
120 }
121
122 if (options?.helpText) {
123 choices.push(helpOption)
124 }
125 if (!options?.maxItems || list.length < options.maxItems) {
126 choices.push(addOption(itemDef.name))
127 }
128 if (list.length >= minItems) {
129 choices.push(finishOption(name))
130 }
131 choices.push(cancelOption)
132
133 const action = await select({
134 message: `Add or edit ${name}.`,
135 choices,
136 default: list.length >= minItems ? finishAction : addAction,
137 pageSize: inquirerPageSize,
138 })
139
140 if (action === addAction) {
141 const newValue = await itemDef.buildFromUserInput([[...list], ...context])
142 if (newValue !== cancelAction && checkForDuplicate(list, newValue, -1)) {
143 list.push(newValue)
144 }
145 } else if (action === helpAction) {
146 console.log(`\n${options?.helpText}\n`)
147 } else if (typeof action === 'number') {
148 await editItem(itemDef, list, action, context)
149 } else if (action === finishAction) {
150 return finishAction
151 } else if (action === cancelAction) {
152 return cancelAction
153 } else {
154 throw Error(`unexpected state in arrayDef; action = ${inspect(action)}`)
155 }
156 }
157 }
158
159 const buildFromUserInput = async (context: unknown[] = []): Promise<T[] | CancelAction> => {
160 const retVal: T[] = []
161

Callers 2

buildFromUserInputFunction · 0.85
updateFromUserInputFunction · 0.85

Calls 5

itemSummaryFunction · 0.85
addOptionFunction · 0.85
finishOptionFunction · 0.85
checkForDuplicateFunction · 0.85
editItemFunction · 0.85

Tested by

no test coverage detected