### Install prompt-list Package Source: https://github.com/enquirer/enquirer/blob/master/support/src/content/examples.md This command installs the 'prompt-list' package, a dependency for creating list-style prompts with the Enquirer library. It uses npm for package management. ```sh npm install prompt-list ``` -------------------------------- ### Basic Enquirer Prompt Usage Example Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md This example shows how to use the `prompt` function from the 'enquirer' library to ask a user a question. It defines a simple 'input' type prompt and logs the user's answer to the console. Ensure 'enquirer' is installed. ```javascript const { prompt } = require('enquirer'); const question = { type: 'input', name: 'username', message: 'What is your username?' }; prompt(question) .then(answer => console.log('Answer:', answer)) .catch(console.error); ``` -------------------------------- ### Install Dependencies and Run Tests (npm) Source: https://github.com/enquirer/enquirer/blob/master/README.md Installs project dependencies using npm and then executes the test suite. This is a common command for setting up and verifying a project's functionality. ```sh npm install && npm test ``` -------------------------------- ### Using a Custom Prompt (JavaScript) Source: https://github.com/enquirer/enquirer/blob/master/support/src/content/prompts.md This example shows how to instantiate and use a custom prompt, 'prompt-awesome', after it has been published and installed. It includes setting prompt options like 'name' and 'message', and handling the returned answer via a Promise. ```javascript var Prompt = require('prompt-awesome'); var prompt = new Prompt({ name: 'fun', message: 'Having fun yet?!' }); prompt.run() .then(function(answer) { console.log(answer); //=> 'of course!' }) ``` -------------------------------- ### Basic enquirer prompt example Source: https://github.com/enquirer/enquirer/blob/master/support/src/content/index.md This Node.js code snippet demonstrates how to use enquirer to create and run simple prompts. It defines two questions, 'first' and 'last', prompts the user for answers, and logs the collected answers to the console. This serves as a quickstart for integrating enquirer into applications. ```javascript var Enquirer = require('enquirer'); var enquirer = new Enquirer(); enquirer.question('first', 'First name?'); enquirer.question('last', 'Last name?'); enquirer.prompt(['first', 'last']) .then(function(answers) { console.log(answers) }); ``` -------------------------------- ### Generate Readme Documentation Source: https://github.com/enquirer/enquirer/blob/master/README.md Installs global dependencies for readme generation and then runs the documentation generation process. This command is used for projects that automate their readme file creation. ```sh npm install -g verbose/verb#dev verb-generate-readme && verb ``` -------------------------------- ### Install Dependencies and Run Tests (yarn) Source: https://github.com/enquirer/enquirer/blob/master/README.md Installs project dependencies using yarn and then executes the test suite. This command is an alternative to the npm version for projects using yarn as their package manager. ```sh yarn && yarn test ``` -------------------------------- ### Install enquirer using npm Source: https://github.com/enquirer/enquirer/blob/master/support/src/content/index.md This command installs the enquirer library as a dependency for your Node.js project using npm. It is the first step in using enquirer for creating terminal prompts. ```sh npm install --save enquirer ``` -------------------------------- ### Install Enquirer using npm Source: https://github.com/enquirer/enquirer/blob/master/README.md This command installs the Enquirer package and saves it as a dependency in your project's package.json file using npm. It's the standard way to add Node.js packages to your project. ```sh npm install enquirer --save ``` -------------------------------- ### Enquirer Event Handling Example in JavaScript Source: https://github.com/enquirer/enquirer/blob/master/support/docs.md Shows how to instantiate a prompt, listen for 'keypress' events, store the keypress data, and then run the prompt, logging the collected keypresses upon completion. It also includes basic error handling. ```javascript const prompt = new Prompt({ name: 'username', message: 'What is your username?' }); const keypresses = []; prompt.on('keypress', (s, key) => keypresses.push(key)); prompt.run() .then(() => console.log(keypresses)) .catch(console.log); ``` -------------------------------- ### Select Prompt Example with Enquirer.js Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md Demonstrates how to use the Select prompt to allow users to choose from a list of simple string options. It requires the 'enquirer' package and returns the selected string value. ```javascript const { Select } = require('enquirer'); const prompt = new Select({ name: 'color', message: 'Pick a flavor', choices: ['apple', 'grape', 'watermelon', 'cherry', 'orange'] }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); ``` -------------------------------- ### Enquirer Scale Prompt Example in JavaScript Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md This JavaScript example showcases the Scale prompt from the Enquirer library, a streamlined version of the Survey prompt for quick feedback using a Likert scale. It defines the prompt's name, message, and the scale options. Each choice includes a name, message, and an initial selected value. The .run() method executes the prompt, and the .then() callback handles the collected user ratings. ```javascript const { Scale } = require('enquirer'); const prompt = new Scale({ name: 'experience', message: 'Please rate your experience', scale: [ { name: '1', message: 'Strongly Disagree' }, { name: '2', message: 'Disagree' }, { name: '3', message: 'Neutral' }, { name: '4', message: 'Agree' }, { name: '5', message: 'Strongly Agree' } ], margin: [0, 0, 2, 1], choices: [ { name: 'interface', message: 'The website has a friendly interface.', initial: 2 }, { name: 'navigation', message: 'The website is easy to navigate.', initial: 2 }, { name: 'images', message: 'The website usually has good images.', initial: 2 }, { name: 'upload', message: 'The website makes it easy to upload images.', initial: 2 }, { name: 'colors', message: 'The website has a pleasing color palette.', initial: 2 } ] }); prompt.run() .then(value => console.log('ANSWERS:', value)) .catch(console.error); ``` -------------------------------- ### List Prompt Example with Enquirer Source: https://github.com/enquirer/enquirer/blob/master/support/src/content/examples.md This JavaScript code snippet demonstrates how to create and use a list prompt using the 'prompt-list' module from Enquirer. It defines a prompt message and a list of choices, including one that is disabled. The 'ask' method is used to display the prompt and handle the user's answer. ```javascript var Prompt = require('prompt-list'); var prompt = new Prompt({ name: 'drink', message: 'What would you like to drink?', choices: [ 'Coke', 'Diet Coke', 'Cherry Coke', {name: 'Sprite', disabled: 'Temporarily unavailable'}, 'Water' ] }); prompt.ask(function(answer) { console.log(answer); }); ``` -------------------------------- ### Enquirer Select Prompt Example in JavaScript Source: https://github.com/enquirer/enquirer/blob/master/support/docs.md An example of a 'select' type question object for Enquirer. It includes the 'type', 'name', 'message', an 'initial' selection index, and a 'choices' array with message and value properties for each option. ```javascript const question = { type: 'select', name: 'colors', message: 'Pick a color', initial: 1, choices: [ { message: 'Red', value: '#ff0000' }, { message: 'Green', value: '#00ff00' }, { message: 'Blue', value: '#0000ff' } ] }; ``` -------------------------------- ### BasicAuth Prompt Example in JavaScript Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md Shows how to implement a BasicAuth prompt for username and password authentication using Enquirer. The default authentication can be overridden with custom logic. Requires the 'enquirer' package. ```javascript const { BasicAuth } = require('enquirer'); const prompt = new BasicAuth({ name: 'password', message: 'Please enter your password', username: 'rajat-sr', password: '123', showPassword: true }); prompt .run() .then(answer => console.log('Answer:', answer)) .catch(console.error); ``` -------------------------------- ### List Prompt Example (JavaScript) Source: https://context7.com/enquirer/enquirer/llms.txt Demonstrates the List prompt, which allows users to input comma-separated values that are then returned as an array. This is convenient for collecting tags or multiple keywords. ```javascript const { List } = require('enquirer'); const prompt = new List({ name: 'keywords', message: 'Type comma-separated keywords' }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); // User input: "node, javascript, cli" // Expected output: Answer: ['node', 'javascript', 'cli'] ``` -------------------------------- ### Install Enquirer JavaScript Library Source: https://github.com/enquirer/enquirer/blob/master/docs/overview.md This snippet shows how to install the Enquirer library in a JavaScript project using a require statement. This is the first step to using Enquirer for running prompts. ```javascript const Enquirer = require('enquirer'); ``` -------------------------------- ### Use BooleanPrompt Class - JavaScript Source: https://github.com/enquirer/enquirer/blob/master/support/src/content/types/index.md Demonstrates how to require and use the BooleanPrompt class from the Enquirer library. This example serves as a basic starting point for creating custom boolean prompts. ```javascript const BooleanPrompt = require('{%= name %}/lib/types/boolean'); // todo (simplest example for a custom boolean prompt we can think of) ``` -------------------------------- ### AutoComplete Prompt Example in JavaScript Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md Demonstrates how to use the AutoComplete prompt from the Enquirer library. This prompt provides type-ahead suggestions and allows users to select a value from a list. It requires the 'enquirer' package. ```javascript const { AutoComplete } = require('enquirer'); const prompt = new AutoComplete({ name: 'flavor', message: 'Pick your favorite flavor', limit: 10, initial: 2, choices: [ 'Almond', 'Apple', 'Banana', 'Blackberry', 'Blueberry', 'Cherry', 'Chocolate', 'Cinnamon', 'Coconut', 'Cranberry', 'Grape', 'Nougat', 'Orange', 'Pear', 'Pineapple', 'Raspberry', 'Strawberry', 'Vanilla', 'Watermelon', 'Wintergreen' ] }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); ``` -------------------------------- ### Toggle Prompt Example (JavaScript) Source: https://context7.com/enquirer/enquirer/llms.txt Shows how to create a binary choice prompt (toggle) using enquirer. This prompt allows users to switch between two states, typically represented as true/false. Dependencies: 'enquirer'. ```javascript const { Toggle } = require('enquirer'); const prompt = new Toggle({ message: 'Want to answer?', enabled: 'Yep', disabled: 'Nope' }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); // Expected output: Answer: true (or false) ``` -------------------------------- ### Numeral Prompt Example - Enquirer JS Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md Provides an example of the Numeral prompt, which is designed for taking numerical input from the user. It validates that the input is a number. Dependencies: 'enquirer'. ```javascript const { NumberPrompt } = require('enquirer'); const prompt = new NumberPrompt({ name: 'number', message: 'Please enter a number' }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); ``` -------------------------------- ### Install Enquirer using yarn Source: https://github.com/enquirer/enquirer/blob/master/README.md This command installs the Enquirer package and adds it to your project's dependencies using the yarn package manager. It's an alternative to npm for managing Node.js packages. ```sh yarn add enquirer ``` -------------------------------- ### Snippet Prompt Example with Enquirer Source: https://github.com/enquirer/enquirer/blob/master/README.md Shows how to use the Snippet prompt from Enquirer to fill in placeholders in a template string, such as a package.json file. It uses 'semver' for validation and requires 'enquirer'. ```javascript const semver = require('semver'); const { Snippet } = require('enquirer'); const prompt = new Snippet({ name: 'username', message: 'Fill out the fields in package.json', required: true, fields: [ { name: 'author_name', message: 'Author Name' }, { name: 'version', validate(value, state, item, index) { if (item && item.name === 'version' && !semver.valid(value)) { return prompt.styles.danger('version should be a valid semver value'); } return true; } } ], template: `{ "name": "${name}", "description": "${description}", "version": "${version}", "homepage": "https://github.com/${username}/${name}", "author": "${author_name} (https://github.com/${username})", "repository": "${username}/${name}", "license": "${license:ISC}" } ` }); prompt.run() .then(answer => console.log('Answer:', answer.result)) .catch(console.error); ``` -------------------------------- ### Add Multiple Fields to an Enquirer Form Prompt Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts/form/getting-started.md This example extends a form prompt to include multiple fields: 'First Name', 'Last Name', and 'GitHub username'. It shows how to populate the `choices` array with multiple field configurations. ```javascript const { prompt } = require("enquirer"); const results = prompt({ choices: [ { message: "First Name", name: "firstname" }, { message: "Last Name", name: "lastname" }, { message: "GitHub username", name: "username" } ], message: "Please provide the following information:", name: "user", type: "form" }); ``` -------------------------------- ### Passing Options Directly to Prompt with Enquirer (JavaScript) Source: https://github.com/enquirer/enquirer/blob/master/docs/usage.md Presents another way to run Enquirer prompts by directly passing the prompt options to the `prompt` function. This method also utilizes promises to handle the response and requires the 'enquirer' library. ```javascript const { prompt } = require('enquirer'); prompt({ type: 'confirm', name: 'question', message: 'Did you like enquirer?' }) .then(answer => console.log('Answer:', answer)); ``` -------------------------------- ### Run a Single Prompt using enquirer.prompt Source: https://github.com/enquirer/enquirer/blob/master/README.md This example shows a concise way to run a single prompt by directly calling the imported `prompt` function. It collects user input for a username and logs the response. ```javascript const { prompt } = require('enquirer'); const response = await prompt({ type: 'input', name: 'username', message: 'What is your username?' }); console.log(response); ``` -------------------------------- ### MultiSelect Prompt Example - Enquirer JS Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md Demonstrates how to use the MultiSelect prompt to allow users to select multiple items from a list of choices. It shows basic selection and how to handle the results. Dependencies: 'enquirer'. ```javascript const { MultiSelect } = require('enquirer'); const prompt = new MultiSelect({ name: 'value', message: 'Pick your favorite colors', limit: 7, choices: [ { name: 'aqua', value: '#00ffff' }, { name: 'black', value: '#000000' }, { name: 'blue', value: '#0000ff' }, { name: 'fuchsia', value: '#ff00ff' }, { name: 'gray', value: '#808080' }, { name: 'green', value: '#008000' }, { name: 'lime', value: '#00ff00' }, { name: 'maroon', value: '#800000' }, { name: 'navy', value: '#000080' }, { name: 'olive', value: '#808000' }, { name: 'purple', value: '#800080' }, { name: 'red', value: '#ff0000' }, { name: 'silver', value: '#c0c0c0' }, { name: 'teal', value: '#008080' }, { name: 'white', value: '#ffffff' }, { name: 'yellow', value: '#ffff00' } ] }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); // Answer: ['aqua', 'blue', 'fuchsia'] ``` -------------------------------- ### MultiSelect Prompt Example (JavaScript) Source: https://context7.com/enquirer/enquirer/llms.txt Demonstrates how to use the MultiSelect prompt to allow users to select multiple items from a list. It shows how to configure choices and handle the returned array of selected values. An alternative 'result' function is also shown to return key-value pairs. ```javascript const { MultiSelect } = require('enquirer'); const prompt = new MultiSelect({ name: 'value', message: 'Pick your favorite colors', limit: 7, choices: [ { name: 'aqua', value: '#00ffff' }, { name: 'black', value: '#000000' }, { name: 'blue', value: '#0000ff' }, { name: 'fuchsia', value: '#ff00ff' }, { name: 'gray', value: '#808080' }, { name: 'green', value: '#008000' }, { name: 'lime', value: '#00ff00' }, { name: 'maroon', value: '#800000' }, { name: 'navy', value: '#000080' }, { name: 'purple', value: '#800080' }, { name: 'red', value: '#ff0000' } ] }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); // Expected output: Answer: ['aqua', 'blue', 'fuchsia'] // Return key-value pairs using result function const colorPrompt = new MultiSelect({ name: 'value', message: 'Pick your favorite colors', choices: [ { name: 'aqua', value: '#00ffff' }, { name: 'blue', value: '#0000ff' }, { name: 'fuchsia', value: '#ff00ff' } ], result(names) { return this.map(names); } }); colorPrompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); // Expected output: Answer: { aqua: '#00ffff', blue: '#0000ff' } ``` -------------------------------- ### Toggle Prompt Example with Enquirer.js Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md Illustrates the Toggle prompt, which provides a simple yes/no (true/false) selection. It's useful for binary choices and requires the 'enquirer' package. The prompt returns a boolean value based on user selection. ```javascript const { Toggle } = require('enquirer'); const prompt = new Toggle({ message: 'Want to answer?', enabled: 'Yep', disabled: 'Nope' }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); ``` -------------------------------- ### Snippet Prompt Example (JavaScript) Source: https://context7.com/enquirer/enquirer/llms.txt Explains the Snippet prompt, designed for filling in placeholders within a template or code snippet. It allows defining fields to be populated and a template string with placeholders. ```javascript const { Snippet } = require('enquirer'); const prompt = new Snippet({ name: 'username', message: 'Fill out the fields in package.json', required: true, fields: [ { name: 'author_name', message: 'Author Name' }, { name: 'version', message: 'Version' } ], template: `{ "name": "${name}", "description": "${description}", "version": "${version}", "homepage": "https://github.com/${username}/${name}", "author": "${author_name} (https://github.com/${username})", "repository": "${username}/${name}", "license": "${license:ISC}" } ` }); prompt.run() .then(answer => console.log('Answer:', answer.result)) .catch(console.error); // Expected output: Complete package.json content with filled values ``` -------------------------------- ### AutoComplete Prompt Example (JavaScript) Source: https://context7.com/enquirer/enquirer/llms.txt Illustrates the AutoComplete prompt, which provides real-time filtering of choices as the user types. This is useful for long lists where users might not know the exact spelling. ```javascript const { AutoComplete } = require('enquirer'); const prompt = new AutoComplete({ name: 'flavor', message: 'Pick your favorite flavor', limit: 10, initial: 2, choices: [ 'Almond', 'Apple', 'Banana', 'Blackberry', 'Blueberry', 'Cherry', 'Chocolate', 'Cinnamon', 'Coconut', 'Cranberry', 'Grape', 'Nougat', 'Orange', 'Pear', 'Pineapple', 'Raspberry', 'Strawberry', 'Vanilla', 'Watermelon', 'Wintergreen' ] }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); // Expected output: Answer: Cherry (or user's choice) ``` -------------------------------- ### Using Specific Prompt Type with Enquirer (JavaScript) Source: https://github.com/enquirer/enquirer/blob/master/docs/usage.md Illustrates an alternative method to run Enquirer prompts by importing a specific prompt type (e.g., 'Confirm') and creating an instance. This approach uses promises for handling the answer. It requires the 'enquirer' library. ```javascript const { Confirm } = require('enquirer'); const prompt = new Confirm({ name: 'question', message: 'Did you like enquirer?' }); prompt.run() .then(answer => console.log('Answer:', answer)); ``` -------------------------------- ### Form Prompt Example (JavaScript) Source: https://context7.com/enquirer/enquirer/llms.txt Shows how to use the Form prompt to collect multiple related values from the user on a single screen. Users can navigate between fields, and the prompt returns an object with all the collected values. ```javascript const { Form } = require('enquirer'); const prompt = new Form({ name: 'user', message: 'Please provide the following information:', choices: [ { name: 'firstname', message: 'First Name', initial: 'Jon' }, { name: 'lastname', message: 'Last Name', initial: 'Schlinkert' }, { name: 'username', message: 'GitHub username', initial: 'jonschlinkert' } ] }); prompt.run() .then(value => console.log('ANSWERS:', value)) .catch(console.error); // Expected output: ANSWERS: { firstname: 'Jon', lastname: 'Schlinkert', username: 'jonschlinkert' } ``` -------------------------------- ### Password Prompt Example - Enquirer JS Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md Demonstrates the Password prompt, which masks user input in the terminal, making it suitable for sensitive information like passwords. Dependencies: 'enquirer'. ```javascript const { Password } = require('enquirer'); const prompt = new Password({ name: 'password', message: 'What is your password?' }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); ``` -------------------------------- ### Single Prompt with Enquirer (JavaScript) Source: https://github.com/enquirer/enquirer/blob/master/docs/usage.md Demonstrates how to use Enquirer to ask a single input prompt. It requires the 'enquirer' library and expects the code to be run within an async function. The output is an object containing the user's input. ```javascript const { prompt } = require('enquirer'); const response = await prompt({ type: 'input', name: 'username', message: 'What is your username?' }); console.log(response); // { username: 'jonschlinkert' } ``` -------------------------------- ### Define a Custom Prompt using Enquirer's Prompt Class Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md This snippet demonstrates how to extend the base `Prompt` class from the 'enquirer' library to create a custom prompt. It assumes you have 'enquirer' installed as a dependency. ```javascript const { Prompt } = require('enquirer'); class MyCustomPrompt extends Prompt {} ``` -------------------------------- ### Confirm Prompt Example in JavaScript Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md Illustrates the usage of the Confirm prompt in Enquirer, which returns a boolean value (true or false). This is useful for simple yes/no questions. Requires the 'enquirer' package. ```javascript const { Confirm } = require('enquirer'); const prompt = new Confirm({ name: 'question', message: 'Want to answer?' }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); ``` -------------------------------- ### Multiple Prompts with Enquirer (JavaScript) Source: https://github.com/enquirer/enquirer/blob/master/docs/usage.md Shows how to use Enquirer to ask a series of prompts by passing an array of question objects. This method also requires the 'enquirer' library and an async function context. The response is an object containing answers to all prompts. ```javascript const { prompt } = require('enquirer'); const response = await prompt([ { type: 'input', name: 'name', message: 'What is your name?' }, { type: 'input', name: 'username', message: 'What is your username?' } ]); console.log(response); // { name: 'Edward Chan', username: 'edwardmchan' } ``` -------------------------------- ### Create String Prompt for Text Input Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md This example illustrates how to create a StringPrompt for capturing string input from the user. It also allows for custom header and footer messages. The prompt returns the input as a string. ```javascript const { StringPrompt } = require('enquirer'); const prompt = new StringPrompt({ header: '************************', message: 'Input the String:', footer: '************************' }); prompt.run() .then(answer => console.log('String is:', answer)) .catch(console.error); ``` -------------------------------- ### Extend Prompt-Base with Custom Prompt (JavaScript) Source: https://github.com/enquirer/enquirer/blob/master/support/src/content/prompts.md This snippet demonstrates how to create a custom prompt by extending the base Prompt class from the 'prompt-base' module. It shows the constructor setup and the use of Prompt.extend() for inheritance, which is crucial for custom prompt functionality. ```javascript var Prompt = require('prompt-base'); function CustomPrompt() { Prompt.apply(this, arguments); } Prompt.extend(CustomPrompt); ``` -------------------------------- ### Enquirer Quiz Prompt Example in JavaScript Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md This JavaScript snippet demonstrates how to use the Quiz prompt from the Enquirer library to create an interactive multiple-choice quiz. It requires the 'enquirer' package and configures the prompt with a message, choices, and the index of the correct answer. The .run() method executes the prompt, and the .then() block handles the user's answer, checking if it's correct. ```javascript const { Quiz } = require('enquirer'); const prompt = new Quiz({ name: 'countries', message: 'How many countries are there in the world?', choices: ['165', '175', '185', '195', '205'], correctChoice: 3 }); prompt .run() .then(answer => { if (answer.correct) { console.log('Correct!'); } else { console.log(`Wrong! Correct answer is ${answer.correctAnswer}`); } }) .catch(console.error); ``` -------------------------------- ### Create Number Prompt for Numerical Input Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md This example demonstrates creating a NumberPrompt for collecting numerical input from the user. Similar to BooleanPrompt, it supports custom headers and footers. The prompt returns the input as a string or a number. ```javascript const { NumberPrompt } = require('enquirer'); const prompt = new NumberPrompt({ header: '************************', message: 'Input the Numbers:', footer: '************************', }); prompt.run() .then(answer => console.log('Numbers are:', answer)) .catch(console.error); ``` -------------------------------- ### Form Prompt Example in JavaScript Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md Demonstrates how to use the Form prompt from Enquirer to collect multiple user inputs within a single terminal interface. It allows defining multiple fields with labels and initial values. Requires the 'enquirer' package. ```javascript const { Form } = require('enquirer'); const prompt = new Form({ name: 'user', message: 'Please provide the following information:', choices: [ { name: 'firstname', message: 'First Name', initial: 'Jon' }, { name: 'lastname', message: 'Last Name', initial: 'Schlinkert' }, { name: 'username', message: 'GitHub username', initial: 'jonschlinkert' } ] }); prompt.run() .then(value => console.log('Answer:', value)) .catch(console.error); ``` -------------------------------- ### Enquirer Select Prompt Example (JavaScript) Source: https://github.com/enquirer/enquirer/blob/master/README.md Demonstrates how to use the Enquirer library to create a 'select' prompt in JavaScript. This involves defining a question object with the prompt type, name, message, initial selection, and an array of choices, where each choice is an object with name, message, and value properties. ```javascript const { prompt } = require('enquirer'); const questions = [{ type: 'select', name: 'color', message: 'Favorite color?', initial: 1, choices: [ { name: 'red', message: 'Red', value: '#ff0000' }, //<= choice object { name: 'green', message: 'Green', value: '#00ff00' }, //<= choice object { name: 'blue', message: 'Blue', value: '#0000ff' } //<= choice object ] }]; let answers = await prompt(questions); console.log('Answer:', answers.color); ``` -------------------------------- ### Create Custom Authentication Prompt with AuthPrompt Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md This example demonstrates how to create a custom authentication prompt by extending the AuthPrompt class. It requires an 'authenticate' function to handle the authentication logic. The prompt collects username and password from the user and validates them. ```javascript const { AuthPrompt } = require('enquirer'); function authenticate(value, state) { if (value.username === this.options.username && value.password === this.options.password) { return true; } return false; } const CustomAuthPrompt = AuthPrompt.create(authenticate); const prompt = new CustomAuthPrompt({ name: 'password', message: 'Please enter your password', username: 'rajat-sr', password: '1234567', choices: [ { name: 'username', message: 'username' }, { name: 'password', message: 'password' } ] }); prompt .run() .then(answer => console.log('Authenticated?', answer)) .catch(console.error); ``` -------------------------------- ### Enquirer Survey Prompt Example in JavaScript Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md This JavaScript code illustrates the usage of the Survey prompt in Enquirer, designed for collecting user feedback on a series of statements using a defined scale. It initializes the prompt with a name, a main message, a scale configuration (defining the rating options), and a list of choices, where each choice represents a question to be rated. The .run() method initiates the survey, and the .then() block processes the collected answers. ```javascript const { Survey } = require('enquirer'); const prompt = new Survey({ name: 'experience', message: 'Please rate your experience', scale: [ { name: '1', message: 'Strongly Disagree' }, { name: '2', message: 'Disagree' }, { name: '3', message: 'Neutral' }, { name: '4', message: 'Agree' }, { name: '5', message: 'Strongly Agree' } ], margin: [0, 0, 2, 1], choices: [ { name: 'interface', message: 'The website has a friendly interface.' }, { name: 'navigation', message: 'The website is easy to navigate.' }, { name: 'images', message: 'The website usually has good images.' }, { name: 'upload', message: 'The website makes it easy to upload images.' }, { name: 'colors', message: 'The website has a pleasing color palette.' } ] }); prompt.run() .then(value => console.log('ANSWERS:', value)) .catch(console.error); ``` -------------------------------- ### Create Boolean Prompt for True/False Selections Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md This example shows how to create a BooleanPrompt for prompts that require a true or false answer. It includes optional header and footer messages for better user experience. The prompt returns a boolean value. ```javascript const { BooleanPrompt } = require('enquirer'); const prompt = new BooleanPrompt({ header: '========================', message: 'Do you love enquirer?', footer: '========================', }); prompt.run() .then(answer => console.log('Selected:', answer)) .catch(console.error); ``` -------------------------------- ### Create and Use Custom Enquirer Prompt in JavaScript Source: https://github.com/enquirer/enquirer/blob/master/README.md Demonstrates how to create a custom prompt by extending Enquirer's Prompt class. It includes methods for rendering, handling up/down input, and using the custom prompt with Enquirer. This example requires the 'enquirer' package. ```javascript const { Prompt } = require('enquirer'); class HaiKarate extends Prompt { constructor(options = {}) { super(options); this.value = options.initial || 0; this.cursorHide(); } up() { this.value++; this.render(); } down() { this.value--; this.render(); } render() { this.clear(); // clear previously rendered prompt from the terminal this.write(`${this.state.message}: ${this.value}`); } } // Use the prompt by creating an instance of your custom prompt class. const prompt = new HaiKarate({ message: 'How many sprays do you want?', initial: 10 }); prompt.run() .then(answer => console.log('Sprays:', answer)) .catch(console.error); ``` ```javascript const Enquirer = require('enquirer'); const enquirer = new Enquirer(); enquirer.register('haikarate', HaiKarate); let spritzer = require('cologne-drone'); let answers = await enquirer.prompt([ { type: 'haikarate', name: 'cologne', message: 'How many sprays do you need?', initial: 10, async onSubmit(name, value) { await spritzer.activate(value); //<= activate drone return value; } } ]); ``` -------------------------------- ### Sort Prompt Example with Enquirer.js and ansi-colors Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md Demonstrates the Sort prompt for reordering items, utilizing 'ansi-colors' for enhanced visual feedback in the terminal. The prompt requires user interaction to sort a list of color preferences, returning the sorted array. ```javascript const colors = require('ansi-colors'); const { Sort } = require('enquirer'); const prompt = new Sort({ name: 'colors', message: 'Sort the colors in order of preference', hint: 'Top is best, bottom is worst', numbered: true, choices: ['red', 'white', 'green', 'cyan', 'yellow'].map(n => ({ name: n, message: colors[n](n) })) }); prompt.run() .then(function(answer = []) { console.log(answer); console.log('Your preferred order of colors is:'); console.log(answer.map(key => colors[key](key)).join('\n')); }) .catch(console.error); ``` -------------------------------- ### MultiSelect Prompt Example - Enquirer JS Source: https://github.com/enquirer/enquirer/blob/master/README.md Demonstrates how to use the MultiSelect prompt from the Enquirer library to allow users to select multiple items from a list. It shows basic usage with choices and how to retrieve the selected values. An optional `result` function is shown to map selected names to their corresponding values. ```javascript const { MultiSelect } = require('enquirer'); const prompt = new MultiSelect({ name: 'value', message: 'Pick your favorite colors', limit: 7, choices: [ { name: 'aqua', value: '#00ffff' }, { name: 'black', value: '#000000' }, { name: 'blue', value: '#0000ff' }, { name: 'fuchsia', value: '#ff00ff' }, { name: 'gray', value: '#808080' }, { name: 'green', value: '#008000' }, { name: 'lime', value: '#00ff00' }, { name: 'maroon', value: '#800000' }, { name: 'navy', value: '#000080' }, { name: 'olive', value: '#808000' }, { name: 'purple', value: '#800080' }, { name: 'red', value: '#ff0000' }, { name: 'silver', value: '#c0c0c0' }, { name: 'teal', value: '#008080' }, { name: 'white', value: '#ffffff' }, { name: 'yellow', value: '#ffff00' } ] }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); // Answer: ['aqua', 'blue', 'fuchsia'] ``` ```javascript const { MultiSelect } = require('enquirer'); const prompt = new MultiSelect({ name: 'value', message: 'Pick your favorite colors', limit: 7, choices: [ { name: 'aqua', value: '#00ffff' }, { name: 'black', value: '#000000' }, { name: 'blue', value: '#0000ff' }, { name: 'fuchsia', value: '#ff00ff' }, { name: 'gray', value: '#808080' }, { name: 'green', value: '#008000' }, { name: 'lime', value: '#00ff00' }, { name: 'maroon', value: '#800000' }, { name: 'navy', value: '#000080' }, { name: 'olive', value: '#808000' }, { name: 'purple', value: '#800080' }, { name: 'red', value: '#ff0000' }, { name: 'silver', value: '#c0c0c0' }, { name: 'teal', value: '#008080' }, { name: 'white', value: '#ffffff' }, { name: 'yellow', value: '#ffff00' } ], result(names) { return this.map(names); } }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); // Answer: { aqua: '#00ffff', blue: '#0000ff', fuchsia: '#ff00ff' } ``` ```javascript const { MultiSelect } = require('enquirer'); const prompt = new MultiSelect({ name: 'color', message: 'Pick a flavor', choices: [ { message: 'Negative Red', name: 'cyan', value: '#00ffff' }, { message: 'Lights Out', name: 'black', value: '#000000' }, { message: 'The Ocean', name: 'blue', value: '#0000ff' }, ] }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); ``` -------------------------------- ### Enquirer Prompt Choice Normalization (String) Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md This JavaScript example demonstrates Enquirer's normalization of choices defined as simple strings. It shows an array of strings provided as choices, and then illustrates the resulting internal structure where each string is transformed into a choice object with 'name', 'message', and 'value' properties set to the original string. This highlights Enquirer's flexibility in accepting choice definitions. ```javascript const question = { name: 'fruit', message: 'Favorite fruit?', choices: ['Apple', 'Orange', 'Raspberry'] }; // Normalizes to the following when the prompt is run: const question = { name: 'fruit', message: 'Favorite fruit?', choices: [ { name: 'Apple', message: 'Apple', value: 'Apple' }, { name: 'Orange', message: 'Orange', value: 'Orange' }, { name: 'Raspberry', message: 'Raspberry', value: 'Raspberry' } ] }; ``` -------------------------------- ### Register a Custom Prompt Type in Enquirer Source: https://github.com/enquirer/enquirer/blob/master/README.md This example shows how to register a custom prompt type with Enquirer. The `register` method takes the type name and either a `Prompt` class or a function returning one. This allows for extending Enquirer's functionality. ```javascript const Enquirer = require('enquirer'); const enquirer = new Enquirer(); enquirer.register('customType', require('./custom-prompt')); ``` -------------------------------- ### Select Prompt: Return Entire Choice Object with JavaScript Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts/select.md This JavaScript example illustrates how to configure the select prompt to return the entire choice object that was selected by the user. The 'result' function is used to find and return the matching choice object based on its name. ```javascript result(value) { // this will return the entire choice object return this.choices.find(choice => choice.name === value); } ``` -------------------------------- ### Enquirer Prompt Choice Normalization (Object) Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md This JavaScript example illustrates how Enquirer normalizes choice objects for prompts. It shows the initial definition of a choice with name, message, and value properties, and then presents the internal representation after normalization, which ensures consistency for prompt processing. This is key for understanding how Enquirer handles user-defined choices. ```javascript const question = { name: 'fruit', message: 'Favorite fruit?', choices: [ { name: 'Apple', message: 'Apple', value: 'Apple' }, { name: 'Orange', message: 'Orange', value: 'Orange' }, { name: 'Raspberry', message: 'Raspberry', value: 'Raspberry' } ] }; ``` -------------------------------- ### Enquirer Choice Definition Normalization (JavaScript) Source: https://github.com/enquirer/enquirer/blob/master/README.md Illustrates how Enquirer normalizes choice definitions within a prompt. It shows an example where choices are initially provided as an array of strings and how they are transformed into an array of objects, each conforming to a specific interface with name, message, and value properties. ```javascript const question = { name: 'fruit', message: 'Favorite fruit?', choices: ['Apple', 'Orange', 'Raspberry'] }; // Normalizes to: const question = { name: 'fruit', message: 'Favorite fruit?', choices: [ { name: 'Apple', message: 'Apple', value: 'Apple' }, { name: 'Orange', message: 'Orange', value: 'Orange' }, { name: 'Raspberry', message: 'Raspberry', value: 'Raspberry' } ] }; ``` -------------------------------- ### Initialize Enquirer Instance Source: https://github.com/enquirer/enquirer/blob/master/README.md This code demonstrates how to create an instance of the `Enquirer` class, which is the main export of the library. This instance can be used to manage and run prompts. Options and initial answers can be passed during instantiation. ```javascript const Enquirer = require('enquirer'); const enquirer = new Enquirer(); ``` -------------------------------- ### Enquirer List Prompt: Get Comma-Separated String Input Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts.md This prompt allows users to enter a string of comma-separated values, which are then split into an array. The default delimiter is a comma, with optional whitespace handling. It's useful for collecting multiple tags or keywords. Requires the 'enquirer' library. ```javascript const { List } = require('enquirer'); const prompt = new List({ name: 'keywords', message: 'Type comma-separated keywords' }); prompt.run() .then(answer => console.log('Answer:', answer)) .catch(console.error); ``` -------------------------------- ### Complete Form prompt with initial and dynamic values Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts/form/default-values.md Presents a complete example of an enquirer form prompt that includes fields with both static 'initial' values and a field whose 'initial' value is dynamically determined using the 'onChoice' method based on prior inputs. ```javascript const { prompt } = require("enquirer"); const results = prompt({ choices: [ { initial: "Jon", message: "First Name", name: "firstname" }, { initial: "Schlinkert", message: "Last Name", name: "lastname" }, { message: "GitHub username", name: "username", onChoice(state, choice, i) { const { firstname, lastname } = this.values; choice.initial = `${firstname}${lastname}`.toLowerCase(); } } ], message: "Please provide the following information:", name: "user", type: "form" }); ``` -------------------------------- ### Create a Form Prompt with Enquirer.js Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts/form/getting-started.md This code demonstrates transforming a basic prompt into a form prompt by setting `type: "form"` and providing an array of choices. It sets up a form for user information with a single 'First Name' field. ```javascript const { prompt } = require("enquirer"); const results = prompt({ choices: [{ message: "First Name", name: "firstname" }], message: "Please provide the following information:", name: "user", type: "form" }); ``` -------------------------------- ### Define a Basic Input Prompt with Enquirer.js Source: https://github.com/enquirer/enquirer/blob/master/docs/prompts/form/getting-started.md This snippet shows how to define a simple input prompt using the Enquirer library. It requires the `prompt` function and configures a prompt for user's first name. ```javascript const { prompt } = require("enquirer"); const results = prompt({ message: "First Name:", name: "firstname", type: "input" }); ``` -------------------------------- ### Run Basic Prompts with Enquirer JavaScript Source: https://github.com/enquirer/enquirer/blob/master/docs/overview.md Demonstrates how to use the `prompt` function from Enquirer to run a series of questions. It defines an array of question objects, each with a type, name, and message, and then logs the collected answers. Requires the 'enquirer' package. ```javascript const { prompt } = require('enquirer'); const questions = [ { type: 'input', name: 'username', message: 'What is your username?' }, { type: 'password', name: 'password', message: 'What is your password?' } ]; const answers = await prompt(questions); console.log(answers); ``` -------------------------------- ### Create an Input Prompt with Enquirer Source: https://context7.com/enquirer/enquirer/llms.txt Shows how to create and run a basic text input prompt using the `Input` class from Enquirer. This prompt collects a string value from the user and can have an initial value. ```javascript const { Input } = require('enquirer'); const prompt = new Input({ message: 'What is your username?', initial: 'jonschlinkert' }); prompt.run() .then(answer => console.log('ANSWER:', answer)) .catch(console.error); // Expected output: ANSWER: jonschlinkert ```