@topcli/prompts 1.7.0 → 1.8.1
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 +13 -13
- package/README.md +212 -214
- package/dist/index.cjs +883 -0
- package/dist/index.d.cts +77 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.js +842 -0
- package/package.json +55 -43
- package/index.d.ts +0 -77
- package/index.js +0 -30
- package/src/constants.js +0 -39
- package/src/prompt-agent.js +0 -56
- package/src/prompts/abstract.js +0 -68
- package/src/prompts/confirm.js +0 -135
- package/src/prompts/index.js +0 -12
- package/src/prompts/multiselect.js +0 -346
- package/src/prompts/question.js +0 -115
- package/src/prompts/select.js +0 -215
- package/src/utils.js +0 -43
- package/src/validators.js +0 -6
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,212 @@
|
|
|
1
|
-
# prompts
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-
[](https://github.com/TopCli/prompts/commit-activity)
|
|
5
|
-
[](https://github.com/TopCli/prompts/blob/main/LICENSE)
|
|
6
|
-

|
|
7
|
-
|
|
8
|
-
Node.js user prompt library for command-line interfaces.
|
|
9
|
-
|
|
10
|
-
## Requirements
|
|
11
|
-
- [Node.js](https://nodejs.org/en/)
|
|
12
|
-
|
|
13
|
-
## Getting Started
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
select
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
multiselect
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
>
|
|
137
|
-
>
|
|
138
|
-
>
|
|
139
|
-
> -
|
|
140
|
-
>
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
fd:
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
<
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
<td align="center" valign="top" width="14.28%"><a href="
|
|
203
|
-
<td align="center" valign="top" width="14.28%"><a href="
|
|
204
|
-
<td align="center" valign="top" width="14.28%"><a href="
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
<!--
|
|
213
|
-
|
|
214
|
-
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
1
|
+
# prompts
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
[](https://github.com/TopCli/prompts/commit-activity)
|
|
5
|
+
[](https://github.com/TopCli/prompts/blob/main/LICENSE)
|
|
6
|
+

|
|
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
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/ncukondo"><img src="https://avatars.githubusercontent.com/u/17022138?v=4?s=100" width="100px;" alt="Takeshi Kondo"/><br /><sub><b>Takeshi Kondo</b></sub></a><br /><a href="#maintenance-ncukondo" title="Maintenance">🚧</a></td>
|
|
205
|
+
</tr>
|
|
206
|
+
</tbody>
|
|
207
|
+
</table>
|
|
208
|
+
|
|
209
|
+
<!-- markdownlint-restore -->
|
|
210
|
+
<!-- prettier-ignore-end -->
|
|
211
|
+
|
|
212
|
+
<!-- ALL-CONTRIBUTORS-LIST:END -->
|