Gathering detailed insights and metrics for prompt-base
Gathering detailed insights and metrics for prompt-base
Gathering detailed insights and metrics for prompt-base
Gathering detailed insights and metrics for prompt-base
prompt-actions
Action manager for prompt-base.
@clack/core
Clack contains low-level primitives for implementing your own command-line applications.
password-prompt
cross-platform masked or hidden prompt
enquirer
Stylish, intuitive and user-friendly prompt system. Fast and lightweight enough for small projects, powerful and extensible enough for the most advanced use cases.
npm install prompt-base
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
23 Stars
180 Commits
1 Forks
4 Watching
1 Branches
6 Contributors
Updated on 02 Feb 2023
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-14.1%
8,503
Compared to previous day
Last week
3.9%
52,738
Compared to previous week
Last month
25.5%
218,399
Compared to previous month
Last year
-33%
2,243,160
Compared to previous year
Base prompt module used for creating custom prompts.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install with npm:
1$ npm install --save prompt-base
See the changelog for detailed release history.
prompt-base is a node.js library for creating command line prompts. You can use prompt-base directly for simple input prompts, or as a "base" for creating custom prompts:
See the examples folder for additional usage examples.
1var Prompt = require('prompt-base'); 2var prompt = new Prompt({ 3 name: 'color', 4 message: 'What is your favorite color?' 5}); 6 7// promise 8prompt.run() 9 .then(function(answer) { 10 console.log(answer); 11 //=> 'blue' 12 }) 13 14// or async 15prompt.ask(function(answer) { 16 console.log(answer); 17 //=> 'blue' 18});
You can also pass a string directly to the main export:
1var prompt = require('prompt-base')('What is your favorite color?'); 2 3prompt.run() 4 .then(function(answer) { 5 console.log(answer); 6 })
Inherit
1var Prompt = require('prompt-base');
2
3function CustomPrompt(/*question, answers, rl*/) {
4 Prompt.apply(this, arguments);
5}
6
7Prompt.extend(CustomPrompt);
Create a new Prompt with the given question
object, answers
and optional instance of readline-ui.
Params
question
{Object}: Plain object or instance of prompt-question.answers
{Object}: Optionally pass an answers object from a prompt manager (like enquirer).ui
{Object}: Optionally pass an instance of readline-ui. If not passed, an instance is created for you.Example
1var prompt = new Prompt({
2 name: 'color',
3 message: 'What is your favorite color?'
4});
5
6prompt.ask(function(answer) {
7 console.log(answer);
8 //=> 'blue'
9});
Modify the answer value before it's returned. Must return a string or promise.
returns
{String}Example
1var answers = {};
2var Prompt = require('prompt-base');
3var prompt = new Prompt({
4 name: 'name',
5 message: 'What is your name?',
6 transform: function(input) {
7 return input.toUpperCase();
8 }
9});
Validate user input on keypress
events and the answer value when it's submitted by the line
event (when the user hits enter. This may be overridden in custom prompts. If the function returns false
, either question.errorMessage
or the default validation error message (invalid input
) is used. Must return a boolean, string or promise.
returns
{Boolean}Example
1var Prompt = require('prompt-base'); 2var prompt = new Prompt({ 3 name: 'first', 4 message: 'What is your name?', 5 errorMessage: 'alphabetical characters only', 6 validate: function(input) { 7 var str = input ? input.trim() : ''; 8 var isValid = /^[a-z]+$/i.test(str); 9 if (this.state === 'submitted') { 10 return str.length > 10 && isValid; 11 } 12 return isValid; 13 } 14});
A custom .when
function may be defined to determine
whether or not a question should be asked at all. Must
return a boolean, undefined, or a promise.
returns
{Boolean}Example
1var answers = {}; 2var Prompt = require('prompt-base'); 3var prompt = new Prompt({ 4 name: 'name', 5 message: 'What is your name?', 6 when: function(answers) { 7 return !answers.name; 8 } 9});
Run the prompt with the given callback
function.
Params
callback
{Function}returns
{undefined}Example
1var Prompt = require('prompt-base'); 2var prompt = new Prompt({ 3 name: 'name', 4 message: 'What is your name?' 5}); 6 7prompt.ask(function(answer) { 8 console.log(answer); 9});
Run the prompt and resolve answers. If when is defined and returns false, the prompt will be skipped.
Params
answers
{Object}: (optional) When supplied, the answer value will be added to a property where the key is the question name.returns
{Promise}Example
1var answers = {}; 2var Prompt = require('prompt-base'); 3var prompt = new Prompt({ 4 name: 'name', 5 message: 'What is your name?' 6}); 7 8prompt.run(answers) 9 .then(function(answer) { 10 console.log(answer); 11 console.log(answers); 12 });
Get the answer to use. This can be overridden in custom prompts.
returns
{String}Example
1console.log(prompt.getDefault());
Get the error message to use. This can be overridden in custom prompts.
returns
{String}Example
1console.log(prompt.getError());
Get the help message to use. This can be overridden in custom prompts.
returns
{String}Example
1console.log(prompt.getHelp());
Get the answer to use. This can be overridden in custom prompts.
returns
{String}Example
1console.log(prompt.getAnswer());
(Re-)render the prompt message, along with any help or error messages, user input, choices, list items, and so on. This is called to render the initial prompt, then it's called again each time the prompt changes, such as on keypress events (when the user enters input, or a multiple-choice option is selected). This method may be overridden in custom prompts, but it's recommended that you override the more specific render "status" methods instead.
returns
{undefined}Example
1prompt.ui.on('keypress', prompt.render.bind(prompt));
Format the prompt message.
returns
{String}Example
1var answers = {};
2var Prompt = require('prompt-base');
3var prompt = new Prompt({
4 name: 'name',
5 message: 'What is your name?',
6 transform: function(input) {
7 return input.toUpperCase();
8 }
9});
Called by render to render the readline line
when prompt.status
is anything besides answered
, which
includes everything except for error and help messages.
returns
{String}Called by render to add a footer after the message body.
returns
{String}Called by render to render a help message when the
prompt.status
is initialized
or help
(usually when the
prompt is first rendered). Calling this method changes the
prompt.status
to "interacted"
, and as such, by default, the
message is only displayed until the user interacts. By default
the help message is positioned to the right of the prompt "question".
A custom help message may be defined on options.helpMessage
.
Params
valid
{boolean|string|undefined}returns
{String}Render an error message in the prompt, when valid
is
false or a string. This is used when a validation method
either returns false
, indicating that the input
was invalid, or the method returns a string, indicating
that a custom error message should be rendered. A custom
error message may also be defined on options.errorMessage
.
Params
valid
{boolean|string|undefined}returns
{String}Mask user input. Called by renderBody, this is an identity function that does nothing by default, as it's intended to be overwritten in custom prompts, such as prompt-password.
returns
{String}Render the user's "answer". Called by render when
the prompt.status
is changed to answered
.
returns
{String}Get action name
, or set action name
with the given fn
.
This is useful for overridding actions in custom prompts.
Actions are used to move the pointer position, toggle checkboxes
and so on
Params
name
{String}fn
{Function}returns
{Object|Function}: Returns the prompt instance if setting, or the action function if getting.Move the cursor in the given direction
when a keypress
event is emitted.
Params
direction
{String}event
{Object}Default error event handler. If an error
listener exist, an error
event will be emitted, otherwise the error is logged onto stderr
and
the process is exited. This can be overridden in custom prompts.
Params
err
{Object}Re-render and pass the final answer to the callback. This can be replaced by custom prompts.
Ensures that events for event name
are only registered once and are disabled correctly when specified. This is different from .once
, which only emits once.
Example
1prompt.only('keypress', function() { 2 // do keypress stuff 3});
Mutes the output stream that was used to create the readline interface, and returns a function for unmuting the stream. This is useful in unit tests.
returns
{Function}Example
1// mute the stream 2var unmute = prompt.mute(); 3 4// unmute the stream 5unmute();
Pause the readline and unmute the output stream that was
used to create the readline interface, which is process.stdout
by default.
Resume the readline input stream if it has been paused.
returns
{undefined}Getter for getting the choices array from the question.
returns
{Object}: Choices objectGetter that returns question.message
after passing it to format.
returns
{String}: A formatted prompt message.Getter/setter for getting the checkbox symbol to use.
returns
{String}: The formatted symbol.Example
1// customize 2prompt.symbol = '[ ]';
Getter/setter that returns the prefix to use before question.message
. The default value is a green ?
.
returns
{String}: The formatted prefix.Example
1// customize 2prompt.prefix = ' ❤ ';
Static convenience method for running the .ask method. Takes the same arguments as the contructror.
Params
question
{Object}: Plain object or instance of prompt-question.answers
{Object}: Optionally pass an answers object from a prompt manager (like enquirer).ui
{Object}: Optionally pass an instance of readline-ui. If not passed, an instance is created for you.callback
{Function}returns
{undefined}Example
1var prompt = require('prompt-base'); 2 .ask('What is your favorite color?', function(answer) { 3 console.log({color: answer}); 4 //=> { color: 'blue' } 5 });
Static convenience method for running the .run method. Takes the same arguments as the contructror.
Params
question
{Object}: Plain object or instance of prompt-question.answers
{Object}: Optionally pass an answers object from a prompt manager (like enquirer).ui
{Object}: Optionally pass an instance of readline-ui. If not passed, an instance is created for you.returns
{Promise}Example
1var prompt = require('prompt-base'); 2 .run('What is your favorite color?') 3 .then(function(answer) { 4 console.log({color: answer}); 5 //=> { color: 'blue' } 6 });
Create a new Question
. See prompt-question for more details.
Params
options
{Object}returns
{Object}: Returns an instance of prompt-questionExample
1var question = new Prompt.Question({name: 'foo'});
Create a new Choices
object. See prompt-choices for more details.
Params
choices
{Array}: Array of choicesreturns
{Object}: Returns an intance of Choices.Example
1var choices = new Prompt.Choices(['foo', 'bar', 'baz']);
Create a new Separator
object. See choices-separator for more details.
Params
separator
{String}: Optionally pass a string to use as the separator.returns
{Object}: Returns a separator object.Example
1new Prompt.Separator('---');
Emitted when a prompt (plugin) is instantiated, after the readline interface is created, but before the actual "question" is asked.
Example usage
1enquirer.on('prompt', function(prompt) { 2 // do stuff with "prompt" instance 3});
Emitted when the actual "question" is asked.
Example usage
Emit keypress
events to supply the answer (and potentially skip the prompt if the answer is valid):
1enquirer.on('ask', function(prompt) { 2 prompt.rl.input.emit('keypress', 'foo'); 3 prompt.rl.input.emit('keypress', '\n'); 4});
Change the prompt message:
1enquirer.on('ask', function(prompt) { 2 prompt.message = 'I..\'m Ron Burgundy...?'; 3});
Emitted when the final (valid) answer is submitted, and custom validation function (if defined) returns true.
(An "answer" is the final input value that's captured when the readline
emits a line
event; e.g. when the user hits enter
)
Example usage
1enquirer.on('answer', function(answer) { 2 // do stuff with answer 3});
The following custom prompts were created using this library:
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the contributing guide for advice on opening issues, pull requests, and coding standards.
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
1$ npm install && npm test
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
You might also be interested in these projects:
Commits | Contributor |
---|---|
170 | jonschlinkert |
6 | doowb |
1 | sbj42 |
Jon Schlinkert
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on October 20, 2017.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More