@poppinss/prompts
Advanced tools
Comparing version 1.0.9 to 1.1.0
@@ -6,3 +6,3 @@ /** | ||
import { EventEmitter } from 'events'; | ||
import { PromptContract, TextPromptOptions, BooleanPromptOptions, TogglePromptOptions, ChoicePromptOptions, PromptEventOptions, PromptChoice, MultiplePromptOptions } from './Contracts'; | ||
import { PromptChoice, PromptContract, EnumPromptOptions, TextPromptOptions, PromptEventOptions, TogglePromptOptions, ChoicePromptOptions, BooleanPromptOptions, MultiplePromptOptions, AutoCompletePromptOptions } from './Contracts'; | ||
/** | ||
@@ -20,23 +20,31 @@ * Base class extended by [[Enquirer]] and [[Emitter]] classes to have | ||
*/ | ||
ask<Result extends any = string>(title: string, options?: TextPromptOptions): Promise<Result>; | ||
ask<Result extends any = string>(title: string, options?: TextPromptOptions<Result>): Promise<Result>; | ||
/** | ||
* Prompts for text input | ||
*/ | ||
enum<Result extends any = string[]>(title: string, options?: EnumPromptOptions<Result>): Promise<Result>; | ||
/** | ||
* Prompts for text input but mangles the output (for password) | ||
*/ | ||
secure<Result extends any = string>(title: string, options?: TextPromptOptions): Promise<Result>; | ||
secure<Result extends any = string>(title: string, options?: TextPromptOptions<Result>): Promise<Result>; | ||
/** | ||
* Asks for `Y/n` | ||
*/ | ||
confirm<Result extends any = boolean>(title: string, options?: BooleanPromptOptions): Promise<Result>; | ||
confirm<Result extends any = boolean>(title: string, options?: BooleanPromptOptions<Result>): Promise<Result>; | ||
/** | ||
* Similar to [[this.confirm]] but with custom toggle options | ||
*/ | ||
toggle<Result extends any = boolean>(title: string, choices: [string, string], options?: TogglePromptOptions): Promise<Result>; | ||
toggle<Result extends any = boolean>(title: string, choices: [string, string], options?: TogglePromptOptions<Result>): Promise<Result>; | ||
/** | ||
* Prompts for text input | ||
*/ | ||
choice<Result extends any = string>(title: string, choices: (string | PromptChoice)[], options?: ChoicePromptOptions): Promise<Result>; | ||
choice<Choice extends string, Result extends any = Choice>(title: string, choices: readonly (Choice | PromptChoice<Choice>)[], options?: ChoicePromptOptions<Choice, Result>): Promise<Result>; | ||
/** | ||
* Prompts for text input | ||
*/ | ||
multiple<Result extends any = string[]>(title: string, choices: (string | PromptChoice)[], options?: MultiplePromptOptions): Promise<Result>; | ||
multiple<Choice extends string, Result extends any = Choice[]>(title: string, choices: readonly (Choice | PromptChoice<Choice>)[], options?: MultiplePromptOptions<Choice, Result>): Promise<Result>; | ||
/** | ||
* Prompts for text input | ||
*/ | ||
autocomplete<Choice extends string, Multiple extends boolean = false, Result extends any = Multiple extends true ? Choice[] : Choice>(title: string, choices: readonly Choice[], options?: AutoCompletePromptOptions<Choice, Multiple, Result>): Promise<Result>; | ||
} |
@@ -34,2 +34,3 @@ "use strict"; | ||
builder.addProp('message', title); | ||
builder.addProp('hint', options.hint); | ||
builder.addProp('initial', options.default); | ||
@@ -42,2 +43,19 @@ builder.addProp('result', options.result); | ||
/** | ||
* Prompts for text input | ||
*/ | ||
async enum(title, options) { | ||
options = options || {}; | ||
const builder = new ObjectBuilder_1.ObjectBuilder(); | ||
builder.addProp('type', 'list'); | ||
builder.addProp('sep', options.seperator || ','); | ||
builder.addProp('name', options.name); | ||
builder.addProp('message', title); | ||
builder.addProp('hint', options.hint); | ||
builder.addProp('initial', options.default); | ||
builder.addProp('result', options.result); | ||
builder.addProp('format', options.format); | ||
builder.addProp('validate', options.validate); | ||
return this.prompt(builder.toJSON()); | ||
} | ||
/** | ||
* Prompts for text input but mangles the output (for password) | ||
@@ -66,2 +84,3 @@ */ | ||
builder.addProp('message', title); | ||
builder.addProp('hint', options.hint); | ||
builder.addProp('initial', options.default); | ||
@@ -82,2 +101,3 @@ builder.addProp('result', options.result); | ||
builder.addProp('message', title); | ||
builder.addProp('hint', options.hint); | ||
builder.addProp('initial', options.default); | ||
@@ -101,2 +121,3 @@ builder.addProp('result', options.result); | ||
builder.addProp('initial', options.default); | ||
builder.addProp('hint', options.hint); | ||
builder.addProp('result', options.result); | ||
@@ -125,2 +146,3 @@ builder.addProp('format', options.format); | ||
builder.addProp('format', options.format); | ||
builder.addProp('hint', options.hint); | ||
builder.addProp('validate', options.validate); | ||
@@ -135,3 +157,21 @@ builder.addProp('choices', choices.map((choice) => { | ||
} | ||
/** | ||
* Prompts for text input | ||
*/ | ||
async autocomplete(title, choices, options) { | ||
options = options || {}; | ||
const builder = new ObjectBuilder_1.ObjectBuilder(); | ||
builder.addProp('type', 'autocomplete'); | ||
builder.addProp('name', options.name); | ||
builder.addProp('message', title); | ||
builder.addProp('initial', options.default); | ||
builder.addProp('multiple', options.multiple); | ||
builder.addProp('result', options.result); | ||
builder.addProp('hint', options.hint); | ||
builder.addProp('format', options.format); | ||
builder.addProp('validate', options.validate); | ||
builder.addProp('choices', choices); | ||
return this.prompt(builder.toJSON()); | ||
} | ||
} | ||
exports.Prompt = Prompt; |
@@ -25,10 +25,11 @@ /** | ||
*/ | ||
export declare type PromptResultFunction<T extends any> = (value: T) => any | Promise<any>; | ||
export declare type PromptResultFunction<T extends any, Result extends any> = (value: T) => Result | Promise<Result>; | ||
/** | ||
* Prompt options for text based prompts | ||
*/ | ||
export declare type TextPromptOptions = { | ||
export declare type TextPromptOptions<Result extends any> = { | ||
default?: string; | ||
name?: string; | ||
result?: PromptResultFunction<string>; | ||
hint?: string; | ||
result?: PromptResultFunction<string, Result>; | ||
format?: PromptFormatFunction<string>; | ||
@@ -38,11 +39,24 @@ validate?: PromptValidationFunction<PromptState<string>>; | ||
/** | ||
* Prompt options for enum prompt | ||
*/ | ||
export declare type EnumPromptOptions<Result extends any> = { | ||
default?: string; | ||
name?: string; | ||
result?: PromptResultFunction<string[], Result>; | ||
format?: PromptFormatFunction<string>; | ||
validate?: PromptValidationFunction<PromptState<string[]>>; | ||
hint?: string; | ||
seperator?: string; | ||
}; | ||
/** | ||
* Prompt options for the choice prompt | ||
*/ | ||
export declare type ChoicePromptOptions = { | ||
export declare type ChoicePromptOptions<Choice extends string, Result extends any> = { | ||
default?: string; | ||
name?: string; | ||
result?: PromptResultFunction<string>; | ||
format?: PromptFormatFunction<string>; | ||
validate?: PromptValidationFunction<PromptState<string> & { | ||
choices: PromptChoice[]; | ||
hint?: string; | ||
result?: PromptResultFunction<Choice, Result>; | ||
format?: PromptFormatFunction<Choice>; | ||
validate?: PromptValidationFunction<PromptState<Choice> & { | ||
choices: PromptChoice<Choice>[]; | ||
}>; | ||
@@ -53,9 +67,10 @@ }; | ||
*/ | ||
export declare type MultiplePromptOptions = { | ||
export declare type MultiplePromptOptions<Choice extends string, Result extends any> = { | ||
default?: string[]; | ||
name?: string; | ||
result?: PromptResultFunction<string[]>; | ||
format?: PromptFormatFunction<string[]>; | ||
validate?: PromptValidationFunction<PromptState<string[]> & { | ||
choices: PromptChoice[]; | ||
hint?: string; | ||
result?: PromptResultFunction<Choice[], Result>; | ||
format?: PromptFormatFunction<Choice>; | ||
validate?: PromptValidationFunction<PromptState<Choice[]> & { | ||
choices: PromptChoice<Choice>[]; | ||
}>; | ||
@@ -66,6 +81,7 @@ }; | ||
*/ | ||
export declare type BooleanPromptOptions = { | ||
export declare type BooleanPromptOptions<Result extends any> = { | ||
default?: boolean; | ||
name?: string; | ||
result?: PromptResultFunction<boolean>; | ||
hint?: string; | ||
result?: PromptResultFunction<boolean, Result>; | ||
format?: PromptFormatFunction<boolean>; | ||
@@ -77,6 +93,7 @@ validate?: PromptValidationFunction<PromptState<boolean>>; | ||
*/ | ||
export declare type TogglePromptOptions = { | ||
export declare type TogglePromptOptions<Result extends any> = { | ||
default?: boolean; | ||
name?: string; | ||
result?: PromptResultFunction<boolean>; | ||
hint?: string; | ||
result?: PromptResultFunction<boolean, Result>; | ||
format?: PromptFormatFunction<boolean>; | ||
@@ -86,2 +103,17 @@ validate?: PromptValidationFunction<PromptState<boolean>>; | ||
/** | ||
* Prompt options for the autocomplete prompt | ||
*/ | ||
export declare type AutoCompletePromptOptions<Choice extends string, Multiple extends boolean, Result extends any> = { | ||
default?: number; | ||
limit?: number; | ||
name?: string; | ||
hint?: string; | ||
multiple?: Multiple; | ||
result?: PromptResultFunction<Multiple extends true ? Choice[] : Choice, Result>; | ||
format?: PromptFormatFunction<Choice>; | ||
validate?: PromptValidationFunction<PromptState<Multiple extends true ? Choice[] : Choice> & { | ||
choices: PromptChoice<Choice>[]; | ||
}>; | ||
}; | ||
/** | ||
* The following options are passed to the emitter `prompt` | ||
@@ -95,3 +127,3 @@ * event handler | ||
initial?: string | boolean | string[]; | ||
result?: PromptResultFunction<any>; | ||
result?: PromptResultFunction<any, any>; | ||
format?: PromptFormatFunction<any>; | ||
@@ -108,4 +140,4 @@ validate?: PromptValidationFunction<any>; | ||
*/ | ||
export declare type PromptChoice = { | ||
name: string; | ||
export declare type PromptChoice<Choice extends string> = { | ||
name: Choice; | ||
message?: string; | ||
@@ -119,8 +151,19 @@ hint?: string; | ||
export interface PromptContract { | ||
ask<Result extends any = string>(title: string, options?: TextPromptOptions): Promise<Result>; | ||
secure<Result extends any = string>(title: string, options?: TextPromptOptions): Promise<Result>; | ||
confirm<Result extends any = boolean>(title: string, options?: BooleanPromptOptions): Promise<Result>; | ||
toggle<Result extends any = boolean>(title: string, choices: [string, string], options?: TogglePromptOptions): Promise<Result>; | ||
choice<Result extends any = string>(title: string, choices: (string | PromptChoice)[], options?: ChoicePromptOptions): Promise<Result>; | ||
multiple<Result extends any = string[]>(title: string, choices: (string | PromptChoice)[], options?: MultiplePromptOptions): Promise<Result>; | ||
ask<Result extends any = string>(title: string, options?: TextPromptOptions<Result>): Promise<Result>; | ||
enum<Result extends any = string[]>(title: string, options?: EnumPromptOptions<Result>): Promise<Result>; | ||
secure<Result extends any = string>(title: string, options?: TextPromptOptions<Result>): Promise<Result>; | ||
confirm<Result extends any = boolean>(title: string, options?: BooleanPromptOptions<Result>): Promise<Result>; | ||
toggle<Result extends any = boolean>(title: string, choices: [string, string], options?: TogglePromptOptions<Result>): Promise<Result>; | ||
/** | ||
* Prompts to select one item | ||
*/ | ||
choice<Choice extends string, Result extends any = Choice>(title: string, choices: readonly (Choice | PromptChoice<Choice>)[], options?: ChoicePromptOptions<Choice, Result>): Promise<Result>; | ||
/** | ||
* Prompts to select multiple item | ||
*/ | ||
multiple<Choice extends string, Result extends any = Choice[]>(title: string, choices: readonly (Choice | PromptChoice<Choice>)[], options?: MultiplePromptOptions<Choice, Result>): Promise<Result>; | ||
/** | ||
* Prompts for choice with auto complete feature | ||
*/ | ||
autocomplete<Choice extends string, Multiple extends boolean = false, Result extends any = Multiple extends true ? Choice[] : Choice>(title: string, choices: readonly Choice[], options?: AutoCompletePromptOptions<Choice, Multiple, Result>): Promise<Result>; | ||
on(event: 'prompt', callback: (options: PromptEventOptions) => any): this; | ||
@@ -127,0 +170,0 @@ on(event: 'prompt:error', callback: (message: string) => any): this; |
@@ -24,3 +24,10 @@ "use strict"; | ||
options = Object.assign({ name: 'prompt' }, options, { | ||
multipleSelection: options.type === 'multiselect' || (options.type === 'autocomplete' && options.multiple), | ||
/** | ||
* The default format function for the list prompt | ||
*/ | ||
format: options.format || options.type === 'list' ? function (input) { | ||
return input ? String(input).split(this.sep) : []; | ||
} : undefined, | ||
/** | ||
* Accept the confirmation prompt | ||
@@ -49,4 +56,5 @@ */ | ||
} | ||
const answer = this.choices[index].name; | ||
return this.answer(this.type === 'multiselect' ? [answer] : answer); | ||
let answer = this.choices[index]; | ||
answer = typeof (answer) === 'string' ? answer : answer.name; | ||
return this.answer(this.multipleSelection ? [answer] : answer); | ||
}, | ||
@@ -57,4 +65,4 @@ /** | ||
async multiSelect(indexes) { | ||
if (this.type !== 'multiselect') { | ||
reject(new Error('[prompt multiselect]: method can only be with multiple choices prompt')); | ||
if (!this.multipleSelection) { | ||
reject(new Error('[prompt multiselect]: method can only used be with multiple choices prompt')); | ||
return; | ||
@@ -67,3 +75,6 @@ } | ||
} | ||
return this.answer(indexes.map((index) => this.choices[index].name)); | ||
return this.answer(indexes.map((index) => { | ||
const answer = this.choices[index]; | ||
return typeof (answer) === 'string' ? answer : answer.name; | ||
})); | ||
}, | ||
@@ -70,0 +81,0 @@ /** |
{ | ||
"name": "@poppinss/prompts", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"description": "Module on top of enquirer with API to fake prompts during testing", | ||
@@ -39,14 +39,14 @@ "main": "build/index.js", | ||
"@adonisjs/mrm-preset": "^2.3.0", | ||
"@types/node": "^13.11.1", | ||
"commitizen": "^4.0.4", | ||
"cz-conventional-changelog": "^3.1.0", | ||
"@types/node": "^13.13.5", | ||
"commitizen": "^4.1.2", | ||
"cz-conventional-changelog": "^3.2.0", | ||
"del-cli": "^3.0.0", | ||
"doctoc": "^1.4.0", | ||
"eslint": "^6.8.0", | ||
"eslint": "^7.0.0", | ||
"eslint-plugin-adonis": "^1.0.9", | ||
"husky": "^4.2.5", | ||
"japa": "^3.0.1", | ||
"mrm": "^2.2.1", | ||
"mrm": "^2.3.0", | ||
"np": "^5.2.1", | ||
"ts-node": "^8.8.2", | ||
"ts-node": "^8.10.1", | ||
"typescript": "^3.8.3" | ||
@@ -53,0 +53,0 @@ }, |
@@ -9,3 +9,3 @@ # Prompts | ||
For testing, we make use of Event emitter instead of executing actual prompts and you can act on those events programmatically. | ||
For testing, we make use of Event Emitter instead of executing actual prompts and you can act on those events programmatically. | ||
@@ -28,2 +28,4 @@ > Please note: Only a subset of prompts are implemented. However, I am open to accept PR's for adding more. | ||
- [multiple(title: string, choices: (string | {})[], options?: MultiplePromptOptions)](#multipletitle-string-choices-string---options-multiplepromptoptions) | ||
- [autocomplete(title: string, choices: string[], options?: AutoCompletePromptOptions)](#autocompletetitle-string-choices-string-options-autocompletepromptoptions) | ||
- [enum(title: string, options?: EnumPromptOptions)](#enumtitle-string-options-enumpromptoptions) | ||
@@ -35,3 +37,3 @@ <!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
You want test a command that asks for the username and password and this is how you may go about writing it. | ||
You want to test a command that asks for the **username** and **password** and this is how you may go about writing it. | ||
@@ -194,2 +196,40 @@ ```ts | ||
#### autocomplete(title: string, choices: string[], options?: AutoCompletePromptOptions) | ||
Uses the [autocomplete](https://github.com/enquirer/enquirer#autocomplete-prompt) prompt. | ||
```ts | ||
await prompt.autocomplete('Select country', [ | ||
'India', | ||
'USA', | ||
'UK', | ||
'Ireland', | ||
'Australia', | ||
]) | ||
``` | ||
For multi-select, you can pass the `multiple` property | ||
```ts | ||
await prompt.autocomplete('Select country', [ | ||
'India', | ||
'USA', | ||
'UK', | ||
'Ireland', | ||
'Australia', | ||
], { | ||
multiple: true, | ||
}) | ||
``` | ||
#### enum(title: string, options?: EnumPromptOptions) | ||
Similar to the `ask` prompt, but allows comma (,) separated values. Uses the [list](https://github.com/enquirer/enquirer#list-prompt) prompt. | ||
```ts | ||
await prompt.enum('Define tags', { | ||
hint: 'Accepts comma separated values' | ||
}) | ||
``` | ||
[circleci-image]: https://img.shields.io/circleci/project/github/poppinss/prompts/master.svg?style=for-the-badge&logo=circleci | ||
@@ -196,0 +236,0 @@ [circleci-url]: https://circleci.com/gh/poppinss/prompts "circleci" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35014
633
242