From 4d3e30f29239d1ac4be2d70a67e890fbc2577884 Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 10 Jan 2024 19:05:13 +0100 Subject: [PATCH 1/4] Support for array parameters is added --- lib/interact-for-open-api-object.ts | 61 +++++++++++++++++------------ 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/lib/interact-for-open-api-object.ts b/lib/interact-for-open-api-object.ts index 742ee2e..4d732db 100644 --- a/lib/interact-for-open-api-object.ts +++ b/lib/interact-for-open-api-object.ts @@ -200,33 +200,44 @@ export const interactForOpenApiObject = async ( args.params[paramToEdit] = value return interactForOpenApiObject(args, ctx) - } else if (prop.type === "array" && (prop as any)?.items?.enum) { - const value = ( - await prompts({ - name: "value", - message: `${paramToEdit}:`, - type: "autocompleteMultiselect", - choices: (prop as any).items.enum.map((v: string) => ({ - title: v.toString(), - value: v.toString(), - })), - }) - ).value + } else if (prop.type === "array") { + if ( + (prop as any)?.items?.type === "string" && + !(prop as any)?.items?.enum + ) { + let values = [] + console.log( + `Enter multiple values for ${paramToEdit}, leave empty when done.` + ) + while (true) { + const { value } = await prompts({ + name: "value", + message: `${paramToEdit}:`, + type: "text", + }) + + if (value) { + values.push(value) + } else { + break + } + } + + args.params[paramToEdit] = values + return interactForOpenApiObject(args, ctx) + } + } else if ((prop as any)?.items?.enum) { + const { value } = await prompts({ + name: "value", + message: `${paramToEdit}:`, + type: "autocompleteMultiselect", + choices: (prop as any).items.enum.map((v: string) => ({ + title: v, + value: v, + })), + }) 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] - return interactForOpenApiObject(args, ctx) } else if (prop.type === "object") { args.params[paramToEdit] = await interactForOpenApiObject( { From 08e51e12dbf0b04abff6f8cb2d6ef867839eb572 Mon Sep 17 00:00:00 2001 From: Andrii Date: Thu, 11 Jan 2024 18:29:11 +0100 Subject: [PATCH 2/4] Revert "Support for array parameters is added" This reverts commit 4d3e30f29239d1ac4be2d70a67e890fbc2577884. --- lib/interact-for-open-api-object.ts | 61 ++++++++++++----------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/lib/interact-for-open-api-object.ts b/lib/interact-for-open-api-object.ts index 4d732db..742ee2e 100644 --- a/lib/interact-for-open-api-object.ts +++ b/lib/interact-for-open-api-object.ts @@ -200,44 +200,33 @@ export const interactForOpenApiObject = async ( args.params[paramToEdit] = value return interactForOpenApiObject(args, ctx) - } else if (prop.type === "array") { - if ( - (prop as any)?.items?.type === "string" && - !(prop as any)?.items?.enum - ) { - let values = [] - console.log( - `Enter multiple values for ${paramToEdit}, leave empty when done.` - ) - while (true) { - const { value } = await prompts({ - name: "value", - message: `${paramToEdit}:`, - type: "text", - }) - - if (value) { - values.push(value) - } else { - break - } - } - - args.params[paramToEdit] = values - return interactForOpenApiObject(args, ctx) - } - } else if ((prop as any)?.items?.enum) { - const { value } = await prompts({ - name: "value", - message: `${paramToEdit}:`, - type: "autocompleteMultiselect", - choices: (prop as any).items.enum.map((v: string) => ({ - title: v, - value: v, - })), - }) + } else if (prop.type === "array" && (prop as any)?.items?.enum) { + const value = ( + await prompts({ + name: "value", + message: `${paramToEdit}:`, + type: "autocompleteMultiselect", + choices: (prop as any).items.enum.map((v: string) => ({ + title: v.toString(), + value: v.toString(), + })), + }) + ).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] + return interactForOpenApiObject(args, ctx) } else if (prop.type === "object") { args.params[paramToEdit] = await interactForOpenApiObject( { From ff0210b4d61d115e49bebd420c04546257d74ea1 Mon Sep 17 00:00:00 2001 From: Andrii Date: Thu, 11 Jan 2024 18:44:54 +0100 Subject: [PATCH 3/4] Logic separated, cli ui changed --- lib/interact-for-array.ts | 61 +++++++++++++++++++++++++++++ lib/interact-for-open-api-object.ts | 18 +++------ 2 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 lib/interact-for-array.ts diff --git a/lib/interact-for-array.ts b/lib/interact-for-array.ts new file mode 100644 index 0000000..e51607a --- /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( From cd6ba9313719592729e1eb671d388e3236dbd88c Mon Sep 17 00:00:00 2001 From: Seam Bot Date: Thu, 11 Jan 2024 17:45:15 +0000 Subject: [PATCH 4/4] ci - format code --- lib/interact-for-array.ts | 68 +++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/lib/interact-for-array.ts b/lib/interact-for-array.ts index e51607a..d16e68c 100644 --- a/lib/interact-for-array.ts +++ b/lib/interact-for-array.ts @@ -1,61 +1,61 @@ -import prompts from "prompts"; +import prompts from "prompts" export const interactForArray = async ( array: string[], message: string ): Promise => { - let updatedArray = [...array]; + let updatedArray = [...array] const displayList = () => { - console.log(`${message} Current list:`); + console.log(`${message} Current list:`) if (updatedArray.length > 0) { updatedArray.forEach((item, index) => { - console.log(`${index + 1}: ${item}`); - }); + console.log(`${index + 1}: ${item}`) + }) } else { - console.log('The list is currently empty.'); + console.log("The list is currently empty.") } - }; + } - let action: string; + let action: string do { - displayList(); + displayList() const response = await prompts({ - type: 'select', - name: 'action', - message: 'Choose an action:', + 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' }, + { title: "Add an item", value: "add" }, + { title: "Remove an item", value: "remove" }, + { title: "Finish editing", value: "done" }, ], - }); + }) - action = response.action; + action = response.action - if (action === 'add') { + if (action === "add") { const { newItem } = await prompts({ - type: 'text', - name: 'newItem', - message: 'Enter the new item:', - }); + type: "text", + name: "newItem", + message: "Enter the new item:", + }) if (newItem) { - updatedArray.push(newItem); + updatedArray.push(newItem) } - } else if (action === 'remove') { + } 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', - }); + 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); + updatedArray.splice(index - 1, 1) } } + } while (action !== "done") - } while (action !== 'done'); - - return updatedArray; -}; + return updatedArray +}