Skip to content

Commit

Permalink
feat(exposed/prompts): adds select prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 26, 2019
1 parent 76f99e5 commit 71cdfa7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/exposed/index.ts
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.
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';
29 changes: 29 additions & 0 deletions src/exposed/prompts/select.ts
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;
};
}

0 comments on commit 71cdfa7

Please sign in to comment.