@topcli/prompts 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,13 +1,13 @@
1
- Copyright 2023 Pierre Demailly
2
-
3
- Permission to use, copy, modify, and/or distribute this software for any purpose
4
- with or without fee is hereby granted, provided that the above copyright notice
5
- and this permission notice appear in all copies.
6
-
7
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
9
- FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
11
- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
12
- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
13
- THIS SOFTWARE.
1
+ Copyright 2023-2024 Pierre Demailly
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any purpose
4
+ with or without fee is hereby granted, provided that the above copyright notice
5
+ and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
9
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
11
+ OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
12
+ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
13
+ THIS SOFTWARE.
package/README.md CHANGED
@@ -1,214 +1,211 @@
1
- # prompts
2
-
3
- ![version](https://img.shields.io/badge/dynamic/json.svg?style=for-the-badge&url=https://raw.githubusercontent.com/TopCli/prompts/main/package.json&query=$.version&label=Version)
4
- [![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg?style=for-the-badge)](https://github.com/TopCli/prompts/commit-activity)
5
- [![isc](https://img.shields.io/badge/License-ISC-blue.svg?style=for-the-badge)](https://github.com/TopCli/prompts/blob/main/LICENSE)
6
- ![build](https://img.shields.io/github/actions/workflow/status/TopCli/prompts/node.js.yml?style=for-the-badge)
7
-
8
- Node.js user prompt library for command-line interfaces.
9
-
10
- ## Requirements
11
- - [Node.js](https://nodejs.org/en/) v16 or higher
12
-
13
- ## Getting Started
14
-
15
- > [!CAUTION]
16
- > This package is ESM only.
17
-
18
- This package is available in the Node Package Repository and can be easily installed with [npm](https://docs.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com).
19
-
20
- ```bash
21
- $ npm i @topcli/prompts
22
- # or
23
- $ yarn add @topcli/prompts
24
- ```
25
-
26
- ## Usage exemple
27
-
28
- You can locally run `node ./demo.js`
29
-
30
- ```js
31
- import { question, confirm, select, multiselect } from "@topcli/prompts";
32
-
33
- const kTestRunner = ["node", "tap", "tape", "vitest", "mocha", "ava"];
34
-
35
- const name = await question("Project name ?", { defaultValue: "foo" });
36
- const runner = await select("Choose a test runner", { choices: kTestRunner, maxVisible: 5 });
37
- const isCLI = await confirm("Your project is a CLI ?", { initial: true });
38
- const os = await multiselect("Choose OS", {
39
- choices: ["linux", "mac", "windows"],
40
- preSelectedChoices: ["linux"]
41
- });
42
-
43
- console.log(name, runner, isCLI, os);
44
- ```
45
-
46
- ## API
47
-
48
- ### `question()`
49
-
50
- ```ts
51
- question(message: string, options?: PromptOptions): Promise<string>
52
- ```
53
-
54
- Simple prompt, similar to `rl.question()` with an improved UI.
55
- Use `options.secure` if you need to hide both input and answer.
56
- Use `options.validators` to handle user input.
57
-
58
- **Example**
59
-
60
- ```js
61
- const packageName = await question('Package name', {
62
- validators: [
63
- {
64
- validate: (value) => !existsSync(join(process.cwd(), value)),
65
- error: (value) => `Folder ${value} already exists`
66
- }
67
- ]
68
- });
69
- ```
70
-
71
- **This package provide some validators for common usage**
72
-
73
- - required
74
-
75
- ```js
76
- import { prompt, required } from "@topcli/prompts";
77
-
78
- const name = await prompt("What's your name ?", {
79
- validators: [required()]
80
- });
81
- ```
82
-
83
- ### `select()`
84
-
85
- ```ts
86
- select(message: string, options: SelectOptions): Promise<string>
87
- ```
88
-
89
- Scrollable select depending `maxVisible` (default `8`).
90
- Use `ignoreValues` to skip result render & clear lines after a selected one.
91
-
92
- ### `multiselect()`
93
-
94
- ```ts
95
- multiselect(message: string, options: MultiselectOptions): Promise<[string]>
96
- ```
97
-
98
- Scrollable multiselect depending `maxVisible` (default `8`).<br>
99
- Use `preSelectedChoices` to pre-select choices.
100
-
101
- Use `validators` to handle user input.
102
-
103
- **Example**
104
-
105
- ```js
106
- const os = await multiselect('Choose OS', {
107
- choices: ["linux", "mac", "windows"]
108
- validators: [required()]
109
- });
110
- ```
111
-
112
- Use `autocomplete` to allow filtered choices. This can be usefull for a large list of choices.
113
-
114
- Use `caseSensitive` to make autocomplete filters case sensitive. Default `false`
115
-
116
- ### `confirm()`
117
-
118
- ```ts
119
- confirm(message: string, options?: ConfirmOptions): Promise<boolean>
120
- ```
121
-
122
- Boolean prompt, return `options.initial` if user input is different from `y`/`yes`/`n`/`no` (case insensitive), (default `false`).
123
-
124
- ### `PromptAgent`
125
-
126
- The `PromptAgent` class allows to programmatically set the next answers for any prompt function, this can be useful for testing.
127
-
128
- ```ts
129
- const agent = PromptAgent.agent();
130
- agent.nextAnswer("John");
131
-
132
- const input = await question("What's your name?");
133
- assert.equal(input, "John");
134
- ```
135
-
136
- > [!WARNING]
137
- > Answers set with `PromptAgent` will **bypass** any logical & validation rules.
138
- > Examples:
139
- > - When using `question()`, `validators` functions will not be executed.
140
- > - When using `select()`, the answer can be different from the available choices.
141
- > - When using `confirm()`, the answer can be any type other than boolean.
142
- > - etc<br>
143
- > **Use with caution**
144
-
145
- ## Interfaces
146
-
147
- ```ts
148
- export interface SharedOptions {
149
- stdin?: NodeJS.ReadStream & {
150
- fd: 0;
151
- };
152
- stdout?: NodeJS.WriteStream & {
153
- fd: 1;
154
- };
155
- }
156
-
157
- export interface Validator {
158
- validate: (input: string) => boolean;
159
- error: (input?: string) => string;
160
- }
161
-
162
- export interface QuestionOptions extends SharedOptions {
163
- defaultValue?: string;
164
- validators?: Validator[];
165
- secure?: boolean;
166
- }
167
-
168
- export interface Choice {
169
- value: any;
170
- label: string;
171
- description?: string;
172
- }
173
-
174
- export interface SelectOptions extends SharedOptions {
175
- choices: (Choice | string)[];
176
- maxVisible?: number;
177
- ignoreValues?: (string | number | boolean)[];
178
- }
179
-
180
- export interface MultiselectOptions extends SharedOptions {
181
- choices: (Choice | string)[];
182
- maxVisible?: number;
183
- preSelectedChoices?: (Choice | string)[];
184
- validators?: Validator[];
185
- autocomplete?: boolean;
186
- caseSensitive?: boolean;
187
- }
188
-
189
- export interface ConfirmOptions extends SharedOptions {
190
- initial?: boolean;
191
- }
192
- ```
193
-
194
- ## Contributors
195
-
196
- <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
197
- <!-- prettier-ignore-start -->
198
- <!-- markdownlint-disable -->
199
- <table>
200
- <tbody>
201
- <tr>
202
- <td align="center" valign="top" width="14.28%"><a href="https://github.com/PierreDemailly"><img src="https://avatars.githubusercontent.com/u/39910767?v=4?s=100" width="100px;" alt="PierreDemailly"/><br /><sub><b>PierreDemailly</b></sub></a><br /><a href="https://github.com/TopCli/prompts/commits?author=PierreDemailly" title="Code">💻</a> <a href="https://github.com/TopCli/prompts/commits?author=PierreDemailly" title="Tests">⚠️</a></td>
203
- <td align="center" valign="top" width="14.28%"><a href="https://www.linkedin.com/in/thomas-gentilhomme/"><img src="https://avatars.githubusercontent.com/u/4438263?v=4?s=100" width="100px;" alt="Gentilhomme"/><br /><sub><b>Gentilhomme</b></sub></a><br /><a href="https://github.com/TopCli/prompts/pulls?q=is%3Apr+reviewed-by%3Afraxken" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/TopCli/prompts/commits?author=fraxken" title="Code">💻</a> <a href="https://github.com/TopCli/prompts/commits?author=fraxken" title="Documentation">📖</a></td>
204
- <td align="center" valign="top" width="14.28%"><a href="http://tonygo.dev"><img src="https://avatars0.githubusercontent.com/u/22824417?v=4?s=100" width="100px;" alt="Tony Gorez"/><br /><sub><b>Tony Gorez</b></sub></a><br /><a href="https://github.com/TopCli/prompts/pulls?q=is%3Apr+reviewed-by%3Atony-go" title="Reviewed Pull Requests">👀</a></td>
205
- <td align="center" valign="top" width="14.28%"><a href="http://sofiand.github.io/portfolio-client/"><img src="https://avatars.githubusercontent.com/u/39944043?v=4?s=100" width="100px;" alt="Yefis"/><br /><sub><b>Yefis</b></sub></a><br /><a href="https://github.com/TopCli/prompts/commits?author=SofianD" title="Code">💻</a> <a href="https://github.com/TopCli/prompts/commits?author=SofianD" title="Documentation">📖</a></td>
206
- <td align="center" valign="top" width="14.28%"><a href="http://justie.dev"><img src="https://avatars.githubusercontent.com/u/7118300?v=4?s=100" width="100px;" alt="Ben"/><br /><sub><b>Ben</b></sub></a><br /><a href="https://github.com/TopCli/prompts/commits?author=JUSTIVE" title="Documentation">📖</a> <a href="#maintenance-JUSTIVE" title="Maintenance">🚧</a></td>
207
- </tr>
208
- </tbody>
209
- </table>
210
-
211
- <!-- markdownlint-restore -->
212
- <!-- prettier-ignore-end -->
213
-
214
- <!-- ALL-CONTRIBUTORS-LIST:END -->
1
+ # prompts
2
+
3
+ ![version](https://img.shields.io/badge/dynamic/json.svg?style=for-the-badge&url=https://raw.githubusercontent.com/TopCli/prompts/main/package.json&query=$.version&label=Version)
4
+ [![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg?style=for-the-badge)](https://github.com/TopCli/prompts/commit-activity)
5
+ [![isc](https://img.shields.io/badge/License-ISC-blue.svg?style=for-the-badge)](https://github.com/TopCli/prompts/blob/main/LICENSE)
6
+ ![build](https://img.shields.io/github/actions/workflow/status/TopCli/prompts/node.js.yml?style=for-the-badge)
7
+
8
+ Node.js user prompt library for command-line interfaces.
9
+
10
+ ## Requirements
11
+ - [Node.js](https://nodejs.org/en/) v18 or higher
12
+
13
+ ## Getting Started
14
+
15
+ This package is available in the Node Package Repository and can be easily installed with [npm](https://docs.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com).
16
+
17
+ ```bash
18
+ $ npm i @topcli/prompts
19
+ # or
20
+ $ yarn add @topcli/prompts
21
+ ```
22
+
23
+ ## Usage exemple
24
+
25
+ You can locally run `node ./demo.js`
26
+
27
+ ```js
28
+ import { question, confirm, select, multiselect } from "@topcli/prompts";
29
+
30
+ const kTestRunner = ["node", "tap", "tape", "vitest", "mocha", "ava"];
31
+
32
+ const name = await question("Project name ?", { defaultValue: "foo" });
33
+ const runner = await select("Choose a test runner", { choices: kTestRunner, maxVisible: 5 });
34
+ const isCLI = await confirm("Your project is a CLI ?", { initial: true });
35
+ const os = await multiselect("Choose OS", {
36
+ choices: ["linux", "mac", "windows"],
37
+ preSelectedChoices: ["linux"]
38
+ });
39
+
40
+ console.log(name, runner, isCLI, os);
41
+ ```
42
+
43
+ ## API
44
+
45
+ ### `question()`
46
+
47
+ ```ts
48
+ question(message: string, options?: PromptOptions): Promise<string>
49
+ ```
50
+
51
+ Simple prompt, similar to `rl.question()` with an improved UI.
52
+ Use `options.secure` if you need to hide both input and answer.
53
+ Use `options.validators` to handle user input.
54
+
55
+ **Example**
56
+
57
+ ```js
58
+ const packageName = await question('Package name', {
59
+ validators: [
60
+ {
61
+ validate: (value) => !existsSync(join(process.cwd(), value)),
62
+ error: (value) => `Folder ${value} already exists`
63
+ }
64
+ ]
65
+ });
66
+ ```
67
+
68
+ **This package provide some validators for common usage**
69
+
70
+ - required
71
+
72
+ ```js
73
+ import { prompt, required } from "@topcli/prompts";
74
+
75
+ const name = await prompt("What's your name ?", {
76
+ validators: [required()]
77
+ });
78
+ ```
79
+
80
+ ### `select()`
81
+
82
+ ```ts
83
+ select(message: string, options: SelectOptions): Promise<string>
84
+ ```
85
+
86
+ Scrollable select depending `maxVisible` (default `8`).
87
+ Use `ignoreValues` to skip result render & clear lines after a selected one.
88
+
89
+ ### `multiselect()`
90
+
91
+ ```ts
92
+ multiselect(message: string, options: MultiselectOptions): Promise<[string]>
93
+ ```
94
+
95
+ Scrollable multiselect depending `maxVisible` (default `8`).<br>
96
+ Use `preSelectedChoices` to pre-select choices.
97
+
98
+ Use `validators` to handle user input.
99
+
100
+ **Example**
101
+
102
+ ```js
103
+ const os = await multiselect('Choose OS', {
104
+ choices: ["linux", "mac", "windows"]
105
+ validators: [required()]
106
+ });
107
+ ```
108
+
109
+ Use `autocomplete` to allow filtered choices. This can be usefull for a large list of choices.
110
+
111
+ Use `caseSensitive` to make autocomplete filters case sensitive. Default `false`
112
+
113
+ ### `confirm()`
114
+
115
+ ```ts
116
+ confirm(message: string, options?: ConfirmOptions): Promise<boolean>
117
+ ```
118
+
119
+ Boolean prompt, return `options.initial` if user input is different from `y`/`yes`/`n`/`no` (case insensitive), (default `false`).
120
+
121
+ ### `PromptAgent`
122
+
123
+ The `PromptAgent` class allows to programmatically set the next answers for any prompt function, this can be useful for testing.
124
+
125
+ ```ts
126
+ const agent = PromptAgent.agent();
127
+ agent.nextAnswer("John");
128
+
129
+ const input = await question("What's your name?");
130
+ assert.equal(input, "John");
131
+ ```
132
+
133
+ > [!WARNING]
134
+ > Answers set with `PromptAgent` will **bypass** any logical & validation rules.
135
+ > Examples:
136
+ > - When using `question()`, `validators` functions will not be executed.
137
+ > - When using `select()`, the answer can be different from the available choices.
138
+ > - When using `confirm()`, the answer can be any type other than boolean.
139
+ > - etc<br>
140
+ > **Use with caution**
141
+
142
+ ## Interfaces
143
+
144
+ ```ts
145
+ export interface SharedOptions {
146
+ stdin?: NodeJS.ReadStream & {
147
+ fd: 0;
148
+ };
149
+ stdout?: NodeJS.WriteStream & {
150
+ fd: 1;
151
+ };
152
+ }
153
+
154
+ export interface PromptValidator<T = string | string[] | boolean> {
155
+ validate: (input: T) => boolean;
156
+ error: (input: T) => string;
157
+ }
158
+
159
+ export interface QuestionOptions extends SharedOptions {
160
+ defaultValue?: string;
161
+ validators?: Validator[];
162
+ secure?: boolean;
163
+ }
164
+
165
+ export interface Choice {
166
+ value: any;
167
+ label: string;
168
+ description?: string;
169
+ }
170
+
171
+ export interface SelectOptions extends SharedOptions {
172
+ choices: (Choice | string)[];
173
+ maxVisible?: number;
174
+ ignoreValues?: (string | number | boolean)[];
175
+ }
176
+
177
+ export interface MultiselectOptions extends SharedOptions {
178
+ choices: (Choice | string)[];
179
+ maxVisible?: number;
180
+ preSelectedChoices?: (Choice | string)[];
181
+ validators?: Validator[];
182
+ autocomplete?: boolean;
183
+ caseSensitive?: boolean;
184
+ }
185
+
186
+ export interface ConfirmOptions extends SharedOptions {
187
+ initial?: boolean;
188
+ }
189
+ ```
190
+
191
+ ## Contributors
192
+
193
+ <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
194
+ <!-- prettier-ignore-start -->
195
+ <!-- markdownlint-disable -->
196
+ <table>
197
+ <tbody>
198
+ <tr>
199
+ <td align="center" valign="top" width="14.28%"><a href="https://github.com/PierreDemailly"><img src="https://avatars.githubusercontent.com/u/39910767?v=4?s=100" width="100px;" alt="PierreDemailly"/><br /><sub><b>PierreDemailly</b></sub></a><br /><a href="https://github.com/TopCli/prompts/commits?author=PierreDemailly" title="Code">💻</a> <a href="https://github.com/TopCli/prompts/commits?author=PierreDemailly" title="Tests">⚠️</a></td>
200
+ <td align="center" valign="top" width="14.28%"><a href="https://www.linkedin.com/in/thomas-gentilhomme/"><img src="https://avatars.githubusercontent.com/u/4438263?v=4?s=100" width="100px;" alt="Gentilhomme"/><br /><sub><b>Gentilhomme</b></sub></a><br /><a href="https://github.com/TopCli/prompts/pulls?q=is%3Apr+reviewed-by%3Afraxken" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/TopCli/prompts/commits?author=fraxken" title="Code">💻</a> <a href="https://github.com/TopCli/prompts/commits?author=fraxken" title="Documentation">📖</a></td>
201
+ <td align="center" valign="top" width="14.28%"><a href="http://tonygo.dev"><img src="https://avatars0.githubusercontent.com/u/22824417?v=4?s=100" width="100px;" alt="Tony Gorez"/><br /><sub><b>Tony Gorez</b></sub></a><br /><a href="https://github.com/TopCli/prompts/pulls?q=is%3Apr+reviewed-by%3Atony-go" title="Reviewed Pull Requests">👀</a></td>
202
+ <td align="center" valign="top" width="14.28%"><a href="http://sofiand.github.io/portfolio-client/"><img src="https://avatars.githubusercontent.com/u/39944043?v=4?s=100" width="100px;" alt="Yefis"/><br /><sub><b>Yefis</b></sub></a><br /><a href="https://github.com/TopCli/prompts/commits?author=SofianD" title="Code">💻</a> <a href="https://github.com/TopCli/prompts/commits?author=SofianD" title="Documentation">📖</a></td>
203
+ <td align="center" valign="top" width="14.28%"><a href="http://justie.dev"><img src="https://avatars.githubusercontent.com/u/7118300?v=4?s=100" width="100px;" alt="Ben"/><br /><sub><b>Ben</b></sub></a><br /><a href="https://github.com/TopCli/prompts/commits?author=JUSTIVE" title="Documentation">📖</a> <a href="#maintenance-JUSTIVE" title="Maintenance">🚧</a></td>
204
+ </tr>
205
+ </tbody>
206
+ </table>
207
+
208
+ <!-- markdownlint-restore -->
209
+ <!-- prettier-ignore-end -->
210
+
211
+ <!-- ALL-CONTRIBUTORS-LIST:END -->