diff --git a/lib/interact-for-array.ts b/lib/interact-for-array.ts new file mode 100644 index 0000000..d16e68c --- /dev/null +++ b/lib/interact-for-array.ts @@ -0,0 +1,61 @@ +import prompts from "prompts" + +export const interactForArray = async ( + array: string[], + message: string +): Promise => { + let updatedArray = [...array] + + const displayList = () => { + console.log(`${message} Current list:`) + if (updatedArray.length > 0) { + updatedArray.forEach((item, index) => { + console.log(`${index + 1}: ${item}`) + }) + } else { + console.log("The list is currently empty.") + } + } + + let action: string + do { + displayList() + + const response = await prompts({ + type: "select", + name: "action", + message: "Choose an action:", + choices: [ + { title: "Add an item", value: "add" }, + { title: "Remove an item", value: "remove" }, + { title: "Finish editing", value: "done" }, + ], + }) + + action = response.action + + if (action === "add") { + const { newItem } = await prompts({ + type: "text", + name: "newItem", + message: "Enter the new item:", + }) + if (newItem) { + updatedArray.push(newItem) + } + } else if (action === "remove") { + const { index } = await prompts({ + type: "number", + name: "index", + message: "Enter the index of the item to remove:", + validate: (value) => + value > 0 && value <= updatedArray.length ? true : "Invalid index", + }) + if (index) { + updatedArray.splice(index - 1, 1) + } + } + } while (action !== "done") + + return updatedArray +} diff --git a/lib/interact-for-open-api-object.ts b/lib/interact-for-open-api-object.ts index 742ee2e..1cae5ee 100644 --- a/lib/interact-for-open-api-object.ts +++ b/lib/interact-for-open-api-object.ts @@ -13,6 +13,7 @@ import { interactForCredentialPool } from "./interact-for-credential-pool" import { ContextHelpers } from "./types" import { interactForAcsEntrance } from "./interact-for-acs-entrance" import { ellipsis } from "./util/ellipsis" +import { interactForArray } from "./interact-for-array" const ergonomicPropOrder = [ "name", @@ -214,18 +215,11 @@ export const interactForOpenApiObject = async ( ).value args.params[paramToEdit] = value return interactForOpenApiObject(args, ctx) - } else if ( - prop.type === "array" && - (prop as any)?.items?.type === "string" - ) { - const value = ( - await prompts({ - name: "value", - message: `${paramToEdit}:`, - type: "text", - }) - ).value - args.params[paramToEdit] = [value] + } else if (prop.type === "array") { + args.params[paramToEdit] = await interactForArray( + args.params[paramToEdit] || [], + `Edit the list for ${paramToEdit}` + ) return interactForOpenApiObject(args, ctx) } else if (prop.type === "object") { args.params[paramToEdit] = await interactForOpenApiObject(