|
1 | | -# nodejs-prompt |
2 | | -NodeJS STDIN prompt module |
| 1 | +# NodeJS Prompt System |
| 2 | + |
| 3 | +> Module to get input from user that supports 1 to n prompt(s) and also prompt with options. |
| 4 | +
|
| 5 | +## Why |
| 6 | + |
| 7 | +> There already bunch of similar modules on NPM ... but then why not :)? Kidding aside, we learn better by doing, so made this module to learn/hone working w/ Typescript, Unit Test, Test Coverage, and NPM. |
| 8 | +
|
| 9 | +## Notable Features |
| 10 | + |
| 11 | +* Prompt with configuration |
| 12 | +* Single or multiple prompts |
| 13 | +* Prompt with options |
| 14 | +* Default value for prompt |
| 15 | +* ...few others |
| 16 | + |
| 17 | +## Notable Stacks |
| 18 | + |
| 19 | +* Typescript |
| 20 | +* Node readline, process.stdin, and process.stdout |
| 21 | +* Reductive promise chaining |
| 22 | +* Jest with ts-jest |
| 23 | +* readline mock |
| 24 | + |
| 25 | +## Install |
| 26 | + |
| 27 | +* `npm i @simplyappdevs/nodejs-prompt` |
| 28 | + |
| 29 | +## Manual Build |
| 30 | + |
| 31 | +* Clone repo: `git clone https://github.com/simplyappdevs/nodejs-prompt` |
| 32 | +* Install deps: `npm i` |
| 33 | +* Clear existing output: `npm run clean` |
| 34 | +* Build module: `npm run build` |
| 35 | +* Test: `npm test` |
| 36 | +* Clear existing example output: `npm run clean:examples` |
| 37 | +* Build example: `npm run build:examples` |
| 38 | +* Run example: `npm run exec` |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +```typescript |
| 43 | +// import |
| 44 | +import prompter, {PromptInput, PromptResult, PromptItem} from '@simplyappdevs/nodejs-prompt'; |
| 45 | + |
| 46 | +// prompt 1 |
| 47 | +const inp01: PromptInput = { |
| 48 | + id: 'inp01', |
| 49 | + allowEmptyValue: false, |
| 50 | + defaultValue: '', |
| 51 | + endIfEmpty: false, |
| 52 | + valueToEndPrompt: '', |
| 53 | + prompt: 'Enter first name' |
| 54 | +}; |
| 55 | + |
| 56 | +const inp02: PromptInput = { |
| 57 | + ...inp01, |
| 58 | + prompt: 'Enter last name' |
| 59 | +}; |
| 60 | + |
| 61 | +const inp03: PromptInput = { |
| 62 | + ...inp02, |
| 63 | + endIfEmpty: true, |
| 64 | + promptList: [ |
| 65 | + {id: 1, key: 'M', text: 'Male'}, |
| 66 | + {id: 2, key: 'F', text: 'Female'} |
| 67 | + ], |
| 68 | + prompt: 'Enter your gender' |
| 69 | +}; |
| 70 | + |
| 71 | +const inp04: PromptInput = { |
| 72 | + ...inp02, |
| 73 | + endIfEmpty: false, |
| 74 | + allowEmptyValue: true, |
| 75 | + defaultValue: 'Secret', |
| 76 | + prompt: 'Enter your age' |
| 77 | +}; |
| 78 | + |
| 79 | +// single prompt |
| 80 | +prompter.prompt(inp01).then((res: PromptResult) => { |
| 81 | + console.log(`User entered: ${res.enteredValue}`); |
| 82 | +}).catch((err)=>{ |
| 83 | + console.log(`Error: ${err.message}`); |
| 84 | +}); |
| 85 | + |
| 86 | +// single prompt with options |
| 87 | +prompter.prompt(inp03).then((res: PromptResult) => { |
| 88 | + console.log(`User entered: ${res.enteredValue}`); |
| 89 | +}).catch((err)=>{ |
| 90 | + console.log(`Error: ${err.message}`); |
| 91 | +}); |
| 92 | + |
| 93 | +// multi prompts |
| 94 | +prompter.prompts([inp01, inp02, inp03, inp04]).then((res: PromptResult[]) => { |
| 95 | + console.log(`User entered: ${res[0].enteredValue}`); |
| 96 | + console.log(`User entered: ${res[1].enteredValue}`); |
| 97 | + console.log(`User entered: ${res[2].enteredValue}`); |
| 98 | + console.log(`User entered: ${res[3].enteredValue}`); |
| 99 | +}).catch((err)=>{ |
| 100 | + console.log(`Error: ${err.message}`); |
| 101 | +}); |
| 102 | +``` |
| 103 | + |
| 104 | +## Objects |
| 105 | + |
| 106 | +> ### PromptInput |
| 107 | +
|
| 108 | +Property | Type | Required | Comment |
| 109 | +---------|----------|---------|--------- |
| 110 | + id | string | Y | Unique id for this prompt (useful in multi-prompt) |
| 111 | + allowEmptyValue | boolean | Y | Accept empty string `''` as a valid input |
| 112 | + defaultValue | string | Y | Value to use if user entered empty string `''` |
| 113 | + endIfEmpty | boolean | Y | True will end prompt session if user enter empty string `''` |
| 114 | + valueToEndPrompt | string | N | Future used? |
| 115 | + prompt | string | Y | Prompt text |
| 116 | + promptList | PromptItem[] | N | List of prompt options |
| 117 | + |
| 118 | +> ### PromptItem |
| 119 | +
|
| 120 | +Property | Type | Required | Comment |
| 121 | +---------|----------|---------|--------- |
| 122 | + id | number | Y | Unique id for this option |
| 123 | + key | string | Y | Value to select this option |
| 124 | + text | string | Y | Prompt option text |
| 125 | + |
| 126 | +> ### PromptResult (extends PromptInput) |
| 127 | +
|
| 128 | +Property | Type | Comment |
| 129 | +---------|----------|--------- |
| 130 | + enteredValue | string | Returns what the user entered |
| 131 | + |
| 132 | +## Methods |
| 133 | + |
| 134 | +> Single prompt: `prompter.prompt()` returns `Promise<PromptResult>` |
| 135 | +> |
| 136 | +> Multi prompts: `prompter.prompts()` returns `Promise<PromptResult[]>` |
| 137 | +
|
| 138 | +## Information |
| 139 | + |
| 140 | +* [Home Page](https://github.com/simplyappdevs/nodejs-prompt/blob/master/README.md) |
| 141 | +* [Repo](https://github.com/simplyappdevs/nodejs-prompt) |
| 142 | +* [Issues](https://github.com/simplyappdevs/nodejs-prompt/issues) |
| 143 | +* [Developer](https://www.simplyappdevs.com) |
| 144 | + |
| 145 | +Brought to you by www.simplyappdevs.com (2020) |
0 commit comments