# @fnet/prompt

Latest version **0.2.16** (published 2025-04-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @fnet/prompt
pnpm add @fnet/prompt
yarn add @fnet/prompt
bun add @fnet/prompt
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.16 |
| Published | 2025-04-11 |
| First published | 2023-11-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 24.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | serdar986, serdark, gboyraz |

## Links

- npm: https://www.npmjs.com/package/@fnet/prompt
- Repository: https://gitlab.com/fnetai/prompt
- npm.io page: https://npm.io/package/@fnet/prompt

## Dependencies (2)

- [enquirer](https://npm.io/package/enquirer.md) ^2.4
- [@fnet/args](https://npm.io/package/@fnet/args.md) ^0.1

## Recent versions

- 0.2.16 (latest) — 2025-04-11
- 0.2.15 — 2025-01-06
- 0.2.14 — 2024-11-03
- 0.2.13 — 2024-10-12
- 0.2.12 — 2024-09-30
- 0.2.11 — 2024-09-30
- 0.2.10 — 2024-02-09
- 0.2.9 — 2024-02-08
- 0.2.8 — 2024-01-22
- 0.2.7 — 2023-11-30
- 0.2.6 — 2023-11-30
- 0.2.5 — 2023-11-29
- 0.2.4 — 2023-11-29
- 0.2.3 — 2023-11-29
- 0.2.2 — 2023-11-29
- … 3 more at https://npm.io/package/@fnet/prompt/versions

## README

# @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

```bash
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:
```javascript
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:
```javascript
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
```javascript
await prompt({
    type: 'input',
    name: 'username',
    message: 'Enter username:',
    initial: 'guest'
});
```

#### Password
```javascript
await prompt({
    type: 'password',
    name: 'secret',
    message: 'Enter password:'
});
```

#### Selection
```javascript
await prompt({
    type: 'select',
    name: 'color',
    message: 'Choose color:',
    choices: ['red', 'blue', 'green']
});
```

#### Multiple Selection
```javascript
await prompt({
    type: 'multiselect',
    name: 'toppings',
    message: 'Select toppings:',
    choices: [
        { name: 'cheese', value: 'cheese' },
        { name: 'pepperoni', value: 'pepperoni' },
        { name: 'mushrooms', value: 'mushrooms' }
    ]
});
```

#### Confirmation
```javascript
await prompt({
    type: 'confirm',
    name: 'proceed',
    message: 'Continue?',
    initial: true
});
```

#### Number Input
```javascript
await prompt({
    type: 'number',
    name: 'age',
    message: 'Enter age:',
    initial: 18
});
```

### Advanced Features

#### Input Validation
```javascript
await prompt({
    type: 'input',
    name: 'email',
    message: 'Enter email:',
    validate: value => {
        return value.includes('@') || 'Please enter a valid email';
    }
});
```

#### Custom Formatting
```javascript
await prompt({
    type: 'input',
    name: 'username',
    message: 'Username:',
    format: value => value.toLowerCase(),
    result: value => value.trim()
});
```

#### Conditional Prompts
```javascript
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:
  - `type` defaults to 'input'
  - `name` defaults 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

```javascript
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

```yaml
$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

```yaml
$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

```

---
_Source: https://npm.io/package/@fnet/prompt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
