Skip to content

Commit

Permalink
🪚 OApp config bootstrap: new prompts [4/N] (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Jan 24, 2024
1 parent defa7f0 commit ed450c4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-monkeys-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@layerzerolabs/io-devtools": patch
---

Add promptToSelectOne and promptToSelectMultiple utilities
57 changes: 57 additions & 0 deletions packages/io-devtools/src/stdio/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,60 @@ export const promptToContinue = async (

return value
}

export interface PromptOption<TValue> {
title: string
description?: string
disabled?: boolean
selected?: boolean
value?: TValue
}

interface SelectProps<TValue> {
options: PromptOption<TValue>[]
/**
* A message displayed to the user if they focus on a disabled value
*/
disabledHint?: string
}

export const promptToSelectOne = async <TValue>(message: string, { options }: SelectProps<TValue>): Promise<TValue> => {
const { value } = await prompts({
type: 'select',
name: 'value',
message,
choices: options,
onState: handlePromptState,
})

return value
}

interface MultiSelectProps<TValue> extends SelectProps<TValue> {
/**
* Minimum number of options to select
*/
min?: number
/**
* Maximum number of options to select
*/
max?: number
}

export const promptToSelectMultiple = async <TValue>(
message: string,
{ options, disabledHint, min, max }: MultiSelectProps<TValue>
): Promise<TValue[]> => {
const { value } = await prompts({
type: 'autocompleteMultiselect',
name: 'value',
message,
choices: options,
onState: handlePromptState,
warn: disabledHint,
min,
max,
})

return value
}

0 comments on commit ed450c4

Please sign in to comment.