@fnet/prompt
@fnet/prompt
The @fnet/prompt project offers a straightforward way to gather user input through command-line prompts. Built on top of the Enquirer library, it simplifies the process of defining and collecting responses, making it useful for developers who need a quick and easy solution for interactive command-line applications.
How It Works
This project leverages the Enquirer library to present prompts to users in the command line. By defining the prompts in an array or object, users can specify the type and name of each prompt. If not provided, default values are assigned. Once configured, the prompts are displayed, and the user's input is collected for further processing.
Key Features
- Type Flexibility: Automatically assigns a default type of 'input' if not specified, ensuring a smoother user experience.
- Dynamic Prompt Naming: Generates default names for prompts, allowing for consistent identification of responses.
- Enquirer Integration: Utilizes the Enquirer library to manage and display prompts, offering reliability and ease of use.
Conclusion
The @fnet/prompt project serves as a helpful tool for developers when user input is needed in command-line applications. Its integration with Enquirer provides a simple and reliable interface, handling user prompts effectively with minimal setup.
Developer Guide for @fnet/prompt
Overview
The @fnet/prompt library provides a streamlined interface for creating interactive command-line prompts in Node.js applications. It wraps the powerful enquirer library, offering simplified configuration and sensible defaults while maintaining full access to Enquirer's capabilities.
Installation
npm install @fnet/prompt
# or
yarn add @fnet/prompt
Usage
The library exports a single async function that handles both single prompts and arrays of prompts.
Basic Usage
Single prompt:
import prompt from '@fnet/prompt';
const response = await prompt({
message: 'What is your name?'
// type defaults to 'input'
// name defaults to 'input'
});
console.log(response); // { input: 'John' }
Multiple prompts:
const responses = await prompt([
{
message: 'Username?',
// name defaults to 'input_0'
},
{
type: 'password',
message: 'Password?',
// name defaults to 'input_1'
}
]);
Prompt Types
The library supports all Enquirer prompt types:
Text Input
await prompt({
type: 'input',
name: 'username',
message: 'Enter username:',
initial: 'guest'
});
Password
await prompt({
type: 'password',
name: 'secret',
message: 'Enter password:'
});
Selection
await prompt({
type: 'select',
name: 'color',
message: 'Choose color:',
choices: ['red', 'blue', 'green']
});
Multiple Selection
await prompt({
type: 'multiselect',
name: 'toppings',
message: 'Select toppings:',
choices: [
{ name: 'cheese', value: 'cheese' },
{ name: 'pepperoni', value: 'pepperoni' },
{ name: 'mushrooms', value: 'mushrooms' }
]
});
Confirmation
await prompt({
type: 'confirm',
name: 'proceed',
message: 'Continue?',
initial: true
});
Number Input
await prompt({
type: 'number',
name: 'age',
message: 'Enter age:',
initial: 18
});
Advanced Features
Input Validation
await prompt({
type: 'input',
name: 'email',
message: 'Enter email:',
validate: value => {
return value.includes('@') || 'Please enter a valid email';
}
});
Custom Formatting
await prompt({
type: 'input',
name: 'username',
message: 'Username:',
format: value => value.toLowerCase(),
result: value => value.trim()
});
Conditional Prompts
await prompt([
{
type: 'confirm',
name: 'hasAccount',
message: 'Do you have an account?'
},
{
type: 'input',
name: 'email',
message: 'Enter email:',
skip: ({ hasAccount }) => !hasAccount
}
]);
Response Handling
The prompt function returns a Promise that resolves to an object containing the responses:
- Single prompt:
{ [name]: value } - Multiple prompts:
{ [name1]: value1, [name2]: value2, ... }
Notes
- The library automatically assigns defaults:
typedefaults to 'input'namedefaults to 'input' for single prompts or 'input_n' for arrays
- All Enquirer options are supported through direct pass-through
- Custom prompt types from Enquirer can be used directly
Error Handling
try {
const response = await prompt({
message: 'Enter data:',
validate: value => {
if (!value) throw new Error('Value required');
return true;
}
});
} catch (error) {
console.error('Prompt failed:', error);
}
Input Schema
$schema: https://json-schema.org/draft/2020-12/schema
type:
- object
- array
oneOf:
- type: array
items:
$ref: "#/$defs/promptConfig"
- $ref: "#/$defs/promptConfig"
$defs:
promptConfig:
type: object
properties:
type:
type: string
description: Type of the prompt from Enquirer
enum:
- input
- password
- invisible
- number
- confirm
- list
- toggle
- select
- multiselect
- autocomplete
- survey
- scale
- snippet
- sort
- quiz
default: input
name:
type: string
description: Name of the prompt, used as key in response object
message:
type: string
description: Question or prompt to display to the user
initial:
type:
- string
- number
- boolean
description: Default value for the prompt
choices:
type: array
description: Options for select, multiselect, autocomplete prompts
items:
oneOf:
- type: string
- type: object
properties:
name:
type: string
value:
type:
- string
- number
message:
type: string
limit:
type: number
description: Number of items to display at once
skip:
type:
- boolean
- function
description: Whether to skip the prompt
validate:
type: function
description: Function to validate the user input
format:
type: function
description: Function to format the user input
result:
type: function
description: Function to format the final value
stdin:
type: object
description: Custom stdin stream
stdout:
type: object
description: Custom stdout stream
required:
- message
additionalProperties: true
Output Schema
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
response:
type: array
description: An array of responses from the prompt.
items:
type: object
description: The response object for each prompt item.
additionalProperties: true
required:
- response
$defs: null