-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(exposed/prompts): adds select prompt
- Loading branch information
Showing
4 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './exec'; | ||
export * from './prompt'; | ||
export * from './prompts'; | ||
export { default as options } from './options'; | ||
export { default as line } from './line'; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as confirm } from './confirm'; | ||
export { default as select } from './select'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { TScript, IOfType } from '~/types'; | ||
import prompts from 'prompts'; | ||
|
||
export interface ISelectOptions { | ||
message?: string; | ||
initial?: string; | ||
values: IOfType<TScript>; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
export default function select(options: ISelectOptions) { | ||
return async function select(): Promise<TScript> { | ||
const keys = Object.keys(options.values || {}); | ||
if (!keys.length) throw Error(`No values passed to select`); | ||
|
||
const response = await prompts({ | ||
type: 'select', | ||
name: 'value', | ||
message: options.message || 'Choose an option', | ||
choices: keys.map((key) => ({ | ||
title: key, | ||
value: key | ||
})), | ||
initial: options.initial ? Math.max(0, keys.indexOf(options.initial)) : 0 | ||
}); | ||
|
||
return options.values[response.value] || undefined; | ||
}; | ||
} |