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.
Key Features
Pre-built prompts: Ready-to-use prompt components
Consistent styling: Unified look and feel across all prompts
The group function provides a convenient API for combining a series of questions.
Each question receives the results of previous answers.
The group function returns an object containing the result of every question.
Replaces text in a string, using a regular expression or search string.
@param ― searchValue A string or regular expression to search for.
@param ― replaceValue A string containing the text to replace. When the {@linkcode searchValue} is a RegExp, all matches are replaced if the g flag is set (or only those matches at the beginning, if the y flag is also present). Otherwise, only the first match of {@linkcode searchValue} is replaced.
replace(/@.+$/, '').
String.toLowerCase(): string
Converts all the alphabetic characters in a string to lowercase.
The intro function defines the beginning of an interaction.
It accepts an optional string parameter which is displayed as a title for the interaction.
import {
const intro:(title?:string) => void
intro } from'@clack/prompts';
functionintro(title?:string):void
intro('Welcome to clack');
┌ Welcome to clack
Outro
The outro function defines the end of an interaction.
It accepts an optional string parameter which is displayed as a concluding message.
import {
const outro:(message?:string) => void
outro } from'@clack/prompts';
functionoutro(message?:string):void
outro('All operations are finished');
│
└ All operations are finished
Cancel
The cancel function defines an interruption of an interaction and therefore its end.
It accepts an optional string parameter which is displayed as a cancellation message.
The second parameter of spinner.stop (code) allow you to indicate the ending status of the spinner:
0 (or no value) indicate a success and the symbol for a finished task will be used
1 indicate a cancellation and the red square symbol will be used
Any other code indicate an error and the yellow triangle symbol will be used
Note
The note function renders a box around a message to draw a user’s attention.
This is useful for displaying next steps and linking to your documentation towards the end of an interaction.
│
◇ ─────────────────────────────╮
│ │
│ All files have been created. │
│ │
├────────────────────────────────╯
Logs
The log utilities allow you to add semantic contextual information during an interaction.
Each function renders with specific styling to communicate status.
log is an object containing the following methods:
log.message displays a message without any symbols to communicate state
log.info displays a message with a neutral state
log.warn (alias log.warning) displays a message with a caution state
log.error displays a message with a danger state
log.success displays a message with a success state
log.step displays a message with a neutral, completed state
import {
const log: {
message:(message?:string, { symbol }?:LogMessageOptions) => void;
info:(message:string) => void;
success:(message:string) => void;
step:(message:string) => void;
warn:(message:string) => void;
warning:(message:string) => void;
error:(message:string) => void;
}
log } from'@clack/prompts';
const log: {
message:(message?:string, { symbol }?:LogMessageOptions) => void;
info:(message:string) => void;
success:(message:string) => void;
step:(message:string) => void;
warn:(message:string) => void;
warning:(message:string) => void;
error:(message:string) => void;
}
log.
message: (message?:string, { symbol }?:LogMessageOptions)=>void
message('Entering directory "src"');
const log: {
message:(message?:string, { symbol }?:LogMessageOptions) => void;
info:(message:string) => void;
success:(message:string) => void;
step:(message:string) => void;
warn:(message:string) => void;
warning:(message:string) => void;
error:(message:string) => void;
}
log.
info: (message:string)=>void
info('No files to update');
const log: {
message:(message?:string, { symbol }?:LogMessageOptions) => void;
info:(message:string) => void;
success:(message:string) => void;
step:(message:string) => void;
warn:(message:string) => void;
warning:(message:string) => void;
error:(message:string) => void;
}
log.
warn: (message:string)=>void
warn('Directory is empty, skipping');
const log: {
message:(message?:string, { symbol }?:LogMessageOptions) => void;
info:(message:string) => void;
success:(message:string) => void;
step:(message:string) => void;
warn:(message:string) => void;
warning:(message:string) => void;
error:(message:string) => void;
}
log.
warning: (message:string)=>void
alias for log.warn().
warning('Directory is empty, skipping');
const log: {
message:(message?:string, { symbol }?:LogMessageOptions) => void;
info:(message:string) => void;
success:(message:string) => void;
step:(message:string) => void;
warn:(message:string) => void;
warning:(message:string) => void;
error:(message:string) => void;
}
log.
error: (message:string)=>void
error('Permission denied on file src/secret.js');
const log: {
message:(message?:string, { symbol }?:LogMessageOptions) => void;
info:(message:string) => void;
success:(message:string) => void;
step:(message:string) => void;
warn:(message:string) => void;
warning:(message:string) => void;
error:(message:string) => void;
}
log.
success: (message:string)=>void
success('Installation complete');
const log: {
message:(message?:string, { symbol }?:LogMessageOptions) => void;
info:(message:string) => void;
success:(message:string) => void;
step:(message:string) => void;
warn:(message:string) => void;
warning:(message:string) => void;
error:(message:string) => void;
}
log.
step: (message:string)=>void
step('Check files');
│
│ Entering directory “src”
│
● No files to update
│
▲ Directory is empty, skipping
│
▲ Directory is empty, skipping
│
■ Permission denied on file src/secret.js
│
◆ Installation complete
│
◇ Check files
Stream
The stream utilities allow you, like the log utilities, to add semantic contextual information during an interaction,
expect that the message contains an unknown number of lines.
Each function renders with specific styling to communicate status.
import {
const stream: {
message:(iterable:Iterable<string> |AsyncIterable<string>, { symbol }?:LogMessageOptions) => Promise<void>;
options can include start and end values to read a range of bytes from
the file instead of the entire file. Both start and end are inclusive and
start counting at 0, allowed values are in the
[0, Number.MAX_SAFE_INTEGER] range. If fd is specified and start is
omitted or undefined, fs.createReadStream() reads sequentially from the
current file position. The encoding can be any one of those accepted by Buffer.
If fd is specified, ReadStream will ignore the path argument and will use
the specified file descriptor. This means that no 'open' event will be
emitted. fd should be blocking; non-blocking fds should be passed to net.Socket.
If fd points to a character device that only supports blocking reads
(such as keyboard or sound card), read operations do not finish until data is
available. This can prevent the process from exiting and the stream from
closing naturally.
By default, the stream will emit a 'close' event after it has been
destroyed. Set the emitClose option to false to change this behavior.
By providing the fs option, it is possible to override the corresponding fs implementations for open, read, and close. When providing the fs option,
an override for read is required. If no fd is provided, an override for open is also required. If autoClose is true, an override for close is
also required.
// Artificially marking end-of-stream, as if the underlying resource had
// indicated end-of-file by itself, allows the stream to close.
// This does not cancel pending read operations, and if there is such an
// operation, the process may still not be able to exit successfully
// until it finishes.
stream.push(null);
stream.read(0);
}, 100);
If autoClose is false, then the file descriptor won't be closed, even if
there's an error. It is the application's responsibility to close it and make
sure there's no file descriptor leak. If autoClose is set to true (default
behavior), on 'error' or 'end' the file descriptor will be closed
automatically.
mode sets the file mode (permission and sticky bits), but only if the
file was created.
An example to read the last 10 bytes of a file which is 100 bytes long: