@@ -3,6 +3,8 @@ title: Prompts
3
3
description : Learn about the prompts package and its capabilities
4
4
---
5
5
6
+ import { Aside } from " @astrojs/starlight/components"
7
+
6
8
The ` @clack/prompts ` package provides a collection of pre-built, high-level prompts that make it easy to create interactive command-line interfaces. It builds on top of the core package to provide a more developer-friendly experience.
7
9
8
10
## Key Features
@@ -139,31 +141,167 @@ const shouldProceed = await confirm({
139
141
<font color = " #06989A" >│</font > <font color = " #4E9A06" >●</font > Yes / ○ No
140
142
<font color = " #06989A" >└</font ></pre >
141
143
144
+ ## Grouping
145
+
146
+ ### Group Multiselect
147
+
148
+ ``` ts twoslash
149
+ import { groupMultiselect } from ' @clack/prompts' ;
150
+
151
+ const projectOptions = await groupMultiselect ({
152
+ message: ' Define your project' ,
153
+ options: {
154
+ ' Testing' : [
155
+ { value: ' Jest' },
156
+ { value: ' Playwright' },
157
+ { value: ' Vitest' },
158
+ ],
159
+ ' Language' : [{
160
+ label: " Javascript" ,
161
+ value: ' js' ,
162
+ }, {
163
+ label: ' TypeScript' ,
164
+ value: ' ts' ,
165
+ }, {
166
+ label: " CoffeeScript" ,
167
+ value: ' coffee' ,
168
+ }],
169
+ ' Code quality' : [
170
+ { value: ' Prettier' },
171
+ { value: ' ESLint' },
172
+ { value: ' Biome.js' },
173
+ ],
174
+ },
175
+ });
176
+ ```
177
+
178
+ <pre class = " cli-preview" ><font color = " #555753" >│</font >
179
+ <font color = " #06989A" >◆</font > Define your project
180
+ <font color = " #06989A" >│</font > <font color = " #4E9A06" >◼</font > Testing
181
+ <font color = " #06989A" >│</font > │ <font color = " #4E9A06" >◼</font > Jest
182
+ <font color = " #06989A" >│</font > │ <font color = " #4E9A06" >◼</font > Playwright
183
+ <font color = " #06989A" >│</font > └ <font color = " #4E9A06" >◼</font > Vitest
184
+ <font color = " #06989A" >│</font > <font color = " #06989A" >◻</font > Language
185
+ <font color = " #06989A" >│</font > │ <font color = " #4E9A06" >◼</font > Javascript
186
+ <font color = " #06989A" >│</font > │ <font color = " #06989A" >◻</font > TypeScript
187
+ <font color = " #06989A" >│</font > └ <font color = " #06989A" >◻</font > CoffeeScript
188
+ <font color = " #06989A" >│</font > ◻ Code quality
189
+ <font color = " #06989A" >│</font > │ ◻ Prettier
190
+ <font color = " #06989A" >│</font > │ ◻ ESLint
191
+ <font color = " #06989A" >│</font > └ <font color = " #4E9A06" >◼</font > Biome.js
192
+ <font color = " #06989A" >└</font ></pre >
193
+
194
+ ### Group
195
+
196
+ The ` group ` function provides a convenient API for combining a series of questions.
197
+ Each question receives the ` results ` of previous answers.
198
+ The ` group ` function returns an object containing the result of every question.
199
+
200
+ ``` ts twoslash
201
+ import { group , text , password } from ' @clack/prompts' ;
202
+
203
+ const account = await group ({
204
+ email : () => text ({
205
+ message: ' What is your email address?' ,
206
+ validate : (value ) => {
207
+ if (! / ^ [a-z0-9 _. -] + @[a-z0-9 _. -] + \. [a-z ] {2,} $ / i .test (value )) return ' Please enter a valid email'
208
+ }
209
+ }),
210
+ username : ({results }) => text ({
211
+ message: ' What is your username?' ,
212
+ placeholder: results .email ?.replace (/ @. + $ / , ' ' ).toLowerCase () ?? ' ' ,
213
+ validate : (value ) => {
214
+ // FOR DEMO PURPOSES ONLY! Use a robust validation library in production
215
+ if (value .length < 2 ) return ' Please enter at least 2 characters'
216
+ }
217
+ }),
218
+ password : () => password ({
219
+ message: ' Define your password'
220
+ }),
221
+ });
222
+ ```
223
+
224
+ <pre class = " cli-preview" ><font color = " #555753" >│</font >
225
+ <font color = " #4E9A06" >◇</font > What is your email address?
226
+ <
font color = " #555753" >│</
font >
[email protected]
227
+ <font color = " #555753" >│</font >
228
+ <font color = " #4E9A06" >◇</font > What is your username?
229
+ <font color = " #555753" >│</font > bomb_sh
230
+ <font color = " #555753" >│</font >
231
+ <font color = " #06989A" >◆</font > Define your password
232
+ <font color = " #06989A" >│</font > ▪▪▪▪▪▪▪▪▪▪▪▪<span style = " background-color:#FFFFFF" ><font color = " #FFFFFF" >_ </font ></span >
233
+ <font color = " #06989A" >└</font ></pre >
234
+
142
235
## Support functions
143
236
144
237
### Intro
145
238
239
+ The ` intro ` function defines the beginning of an interaction.
240
+ It accepts an optional string parameter which is displayed as a title for the interaction.
241
+
242
+ <Aside type = " tip" >Feel free to use ANSI escape sequences to add colors and a more branded feel to your intro message!</Aside >
243
+
146
244
``` ts twoslash
147
245
import { intro } from ' @clack/prompts' ;
148
246
149
- intro (' Welcome to clack' )
247
+ intro (' Welcome to clack' );
150
248
```
151
249
152
250
<pre class = " cli-preview" ><font color = " #555753" >┌</font > Welcome to clack</pre >
153
251
154
252
### Outro
155
253
254
+ The ` outro ` function defines the end of an interaction.
255
+ It accepts an optional string parameter which is displayed as a concluding message.
256
+
156
257
``` ts twoslash
157
258
import { outro } from ' @clack/prompts' ;
158
259
159
- outro (' All operations are finished' )
260
+ outro (' All operations are finished' );
160
261
```
161
262
162
263
<pre class = " cli-preview" ><font color = " #555753" >│</font >
163
- <font color = " #555753" >└</font > All operations are finished</pre >
264
+ <font color = " #555753" >└</font > All operations are finished
265
+   ; </pre >
266
+
267
+ ### Cancel
268
+
269
+ The ` cancel ` function defines an interruption of an interaction and therefore its end.
270
+ It accepts an optional string parameter which is displayed as a cancellation message.
271
+
272
+ ``` ts twoslash
273
+ import { cancel } from ' @clack/prompts' ;
274
+
275
+ cancel (' Installation canceled' );
276
+ ```
277
+
278
+ <pre class = " cli-preview" ><font color = " #555753" >└</font > <font color = " #CC0000" >Installation canceled</font >
279
+   ; </pre >
280
+
281
+ ### Spinner
282
+ ``` ts twoslash
283
+ import { spinner } from ' @clack/prompts' ;
284
+
285
+ const spin = spinner ();
286
+ spin .start (' Loading' );
287
+ // Do something
288
+ spin .message (' Finishing' );
289
+ // Do more things
290
+ spin .stop (' Done' );
291
+ ```
292
+ <pre class = " cli-preview" ><font color = " #555753" >│</font >
293
+ <font color = " #AD7FA8" >◒</font > Loading{ ` ... ` } </pre >
294
+
295
+ The second parameter of ` spinner.stop ` (` code ` ) allow you to indicate the ending status of the spinner:
296
+ - ` 0 ` (or no value) indicate a success and the symbol for a finished task will be used
297
+ - ` 1 ` indicate a cancellation and the red square symbol will be used
298
+ - Any other code indicate an error and the yellow triangle symbol will be used
164
299
165
300
### Note
166
301
302
+ The ` note ` function renders a box around a message to draw a user's attention.
303
+ This is useful for displaying next steps and linking to your documentation towards the end of an interaction.
304
+
167
305
``` ts twoslash
168
306
import { note } from ' @clack/prompts' ;
169
307
@@ -198,32 +336,43 @@ note(
198
336
199
337
### Logs
200
338
339
+ The ` log ` utilities allow you to add semantic contextual information during an interaction.
340
+ Each function renders with specific styling to communicate status.
341
+
342
+ ` log ` is an object containing the following methods:
343
+ - ` log.message ` displays a message without any symbols to communicate state
344
+ - ` log.info ` displays a message with a neutral state
345
+ - ` log.warn ` (alias ` log.warning ` ) displays a message with a caution state
346
+ - ` log.error ` displays a message with a danger state
347
+ - ` log.success ` displays a message with a success state
348
+ - ` log.step ` displays a message with a neutral, completed state
349
+
201
350
``` ts twoslash
202
351
import { log } from ' @clack/prompts' ;
203
352
204
- log .step ( ' Check files ' )
205
- log .info (' No files to update' )
206
- log .message ( ' Entering directory "src" ' )
207
- log .error ( ' Permission denied on file src/secret.js ' )
208
- log .success ( ' Installation complete ' )
209
- log .warn ( ' Directory is empty, skipping ' )
210
- log .warning ( ' Directory is empty, skipping ' )
353
+ log .message ( ' Entering directory "src" ' );
354
+ log .info (' No files to update' );
355
+ log .warn ( ' Directory is empty, skipping ' );
356
+ log .warning ( ' Directory is empty, skipping ' );
357
+ log .error ( ' Permission denied on file src/secret.js ' );
358
+ log .success ( ' Installation complete ' );
359
+ log .step ( ' Check files ' );
211
360
```
212
361
213
362
<pre class = " cli-preview" ><font color = " #555753" >│</font >
214
- <font color = " #4E9A06 " >◇ </font > Check files
363
+ <font color = " #555753 " >│ </font > Entering directory & quot ; src & quot ;
215
364
<font color = " #555753" >│</font >
216
365
<font color = " #3465A4" >●</font > No files to update
217
366
<font color = " #555753" >│</font >
218
- <font color = " #555753" >│</font > Entering directory " ; src" ;
219
- <font color = " #555753" >│</font >
367
+ <font color = " #C4A000" >▲</font > Directory is empty, skipping
368
+ <font color = " #555753" >│</font >
369
+ <font color = " #C4A000" >▲</font > Directory is empty, skipping
370
+ <font color = " #555753" >│</font >
220
371
<font color = " #CC0000" >■</font > Permission denied on file src/secret.js
221
372
<font color = " #555753" >│</font >
222
373
<font color = " #4E9A06" >◆</font > Installation complete
223
374
<font color = " #555753" >│</font >
224
- <font color = " #C4A000" >▲</font > Directory is empty, skipping
225
- <font color = " #555753" >│</font >
226
- <font color = " #C4A000" >▲</font > Directory is empty, skipping</pre >
375
+ <font color = " #4E9A06" >◇</font > Check files</pre >
227
376
228
377
## Installation
229
378
@@ -235,4 +384,4 @@ npm install @clack/prompts
235
384
236
385
The prompts package is designed to be intuitive and easy to use. Each prompt function returns a Promise that resolves to the user's input.
237
386
238
- For more detailed examples and advanced usage patterns, check out our [ examples guide] ( /docs/guides/examples ) and [ best practices] ( /docs/guides/best-practices ) .
387
+ For more detailed examples and advanced usage patterns, check out our [ examples guide] ( /docs/guides/examples ) and [ best practices] ( /docs/guides/best-practices ) .
0 commit comments