Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
question-cache
Advanced tools
A wrapper around inquirer that makes it easy to create and selectively reuse questions.
A wrapper around inquirer that makes it easy to create and selectively reuse questions.
Install with npm:
$ npm install --save question-cache
'use strict';
var Store = require('data-store');
var hints = new Store('example-hints-store');
var questions = require('question-cache');
questions()
.use(function() {
this.on('ask', function(val, key, question) {
question.default = hints.get(key);
});
this.on('answer', function(val, key) {
hints.set(key, val);
});
})
.set('first', 'What is your first name?')
.set('last', 'What is your last name?')
.set('foo', 'What is foo?', {
when: function() {
// console.log(arguments)
}
})
.ask(function(err, answers) {
console.log(answers);
});
Screen capture
See the working examples.
See the working examples.
var questions = require('question-cache')();
// question type "input" is used by default
questions
.set('name', 'What is your name?')
.ask('name', function (err, answers) {
console.log(answers);
});
You may optionally pass your own instance of inquirer to the constructor:
// on the options
var questions = require('question-cache');
var questions = new Questions({
inquirer: require('inquirer2')
});
// or if inquirer is the only thing passed
var questions = new Questions(require('inquirer2'));
question-cache is a wrapper around inquirer2. If you have any issues related to the interface (like scrolling, colors, styling, etc), then please create an issue on the inquirer2 project.
Asking questions
The simplest way to ask a question is by passing a string and a callback:
questions.ask('name', function (err, answers) {
console.log(answers);
});
Ask all cached questions
questions.ask(function (err, answers) {
console.log(answers);
});
Create an instance of Questions
with the given options
.
Params
options
{Object}: question cache optionsExample
var Questions = new Questions(options);
Calls addQuestion, with the only difference being that .set
returns the questions
instance and .addQuestion
returns the question object. So use .set
if you want to chain questions, or .addQuestion
if you need the created question object.
Params
name
{Object|String}: Question name, message (string), or question/options object.value
{Object|String}: Question message (string), or question/options object.options
{Object|String}: Question/options object.Example
questions
.set('drink', 'What is your favorite beverage?')
.set('color', 'What is your favorite color?')
.set('season', 'What is your favorite season?');
// or
questions.set('drink', {
type: 'input',
message: 'What is your favorite beverage?'
});
// or
questions.set({
name: 'drink'
type: 'input',
message: 'What is your favorite beverage?'
});
Add a question to be asked at a later point. Creates an instance of Question, so any Question
options or settings may be used. Also, the default type
is input
if not defined by the user.
Params
name
{Object|String}: Question name, message (string), or question/options object.value
{Object|String}: Question message (string), or question/options object.options
{Object|String}: Question/options object.Example
questions.addQuestion('drink', 'What is your favorite beverage?');
// or
questions.addQuestion('drink', {
type: 'input',
message: 'What is your favorite beverage?'
});
// or
questions.addQuestion({
name: 'drink'
type: 'input',
message: 'What is your favorite beverage?'
});
Create a "choices" question from an array of values.
Params
key
{String}: Question keymsg
{String}: Question messageitems
{Array}: Choice itemsoptions
{Object|Function}: Question options or callback functioncallback
{Function}: callback functionExample
questions.choices('foo', ['a', 'b', 'c']);
// or
questions.choices('foo', {
message: 'Favorite letter?',
choices: ['a', 'b', 'c']
});
Create a "list" question from an array of values.
Params
key
{String}: Question keymsg
{String}: Question messagelist
{Array}: List itemsqueue
{String|Array}: Name or array of question names.options
{Object|Function}: Question options or callback functioncallback
{Function}: callback functionExample
questions.list('foo', ['a', 'b', 'c']);
// or
questions.list('foo', {
message: 'Favorite letter?',
choices: ['a', 'b', 'c']
});
Create a "rawlist" question from an array of values.
Params
key
{String}: Question keymsg
{String}: Question messagelist
{Array}: List itemsqueue
{String|Array}: Name or array of question names.options
{Object|Function}: Question options or callback functioncallback
{Function}: callback functionExample
questions.rawlist('foo', ['a', 'b', 'c']);
// or
questions.rawlist('foo', {
message: 'Favorite letter?',
choices: ['a', 'b', 'c']
});
Create an "expand" question from an array of values.
Params
key
{String}: Question keymsg
{String}: Question messagelist
{Array}: List itemsqueue
{String|Array}: Name or array of question names.options
{Object|Function}: Question options or callback functioncallback
{Function}: callback functionExample
questions.expand('foo', ['a', 'b', 'c']);
// or
questions.expand('foo', {
message: 'Favorite letter?',
choices: ['a', 'b', 'c']
});
Create a "choices" question from an array of values.
Params
queue
{String|Array}: Name or array of question names.options
{Object|Function}: Question options or callback functioncallback
{Function}: callback functionExample
questions.choices('foo', ['a', 'b', 'c']);
// or
questions.choices('foo', {
message: 'Favorite letter?',
choices: ['a', 'b', 'c']
});
Get question name
, or group name
if question is not found. You can also do a direct lookup using quesions.cache['foo']
.
Params
name
{String}returns
{Object}: Returns the question object.Example
var name = questions.get('name');
//=> question object
Returns true if questions.cache
or questions.groups
has question name
.
returns
{String}: The name of the question to checkExample
var name = questions.has('name');
//=> true
Delete the given question or any questions that have the given namespace using dot-notation.
returns
{String}: The name of the question to deleteExample
questions.del('name');
questions.get('name');
//=> undefined
// using dot-notation
questions.del('author');
questions.get('author.name');
//=> undefined
Clear all cached answers.
Example
questions.clearAnswers();
Clear all questions from the cache.
Example
questions.clearQuestions();
Clear all cached questions and answers.
Example
questions.clear();
Ask one or more questions, with the given options
and callback.
Params
queue
{String|Array}: Name or array of question names.options
{Object|Function}: Question options or callback functioncallback
{Function}: callback functionExample
questions.ask(['name', 'description'], function(err, answers) {
console.log(answers);
});
Normalize the given value to return an array of question keys.
Params
returns
{[type]}See the working examples.
Qestions may be cached using object-path notatation (e.g. a.b.c
).
Example
All of the following will be cached on the name
object:
questions
.set('name.first', 'What is your first name?')
.set('name.middle', 'What is your middle name?')
.set('name.last', 'What is your last name?')
Dot notation usage
When cached using dot-notation, there are a few different ways questions that may be asked.
Ask a single name
question:
questions.ask('name.first', function (err, answers) {
console.log(answers);
});
Ask all name
questions, first
, middle
and last
:
questions.ask('name', function (err, answers) {
console.log(answers);
});
Ask specific questions on name
:
questions.ask(['name.first', 'name.last'], function (err, answers) {
console.log(answers);
});
Ask specific questions on name
:
questions
.set('name.first', {
message: 'What is your first name?',
})
.set('name.last', {
message: 'What is your last name?',
})
.set('foo', {
message: 'Any thoughts about foo?',
})
questions.ask(['name', 'foo'], function (err, answers) {
console.log(answers);
});
Ask one question at a time, based on feedback:
questions.ask('name.first', function (err, answers) {
console.log(answers);
//=> {name: { first: 'Brian' }}
questions.ask('name.last', function (err, answers) {
console.log(answers);
//=> {name: { last: 'Woodward' }}
});
});
Given you have the following questions:
questions
.set('name.first', 'What is your first name?')
.set('name.last', 'What is your last name?')
.set('foo', 'Any thoughts about foo?')
.set('bar', 'Any thoughts about bar?')
The following will ask questions: name.first
, name.last
and foo
questions.ask(['name', 'foo'], function (err, answers) {
console.log(answers);
});
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)
To generate the readme and API documentation with verb:
$ npm install -g verb verb-generate-readme && verb
Install dev dependencies:
$ npm install -d && npm test
Jon Schlinkert
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb-generate-readme, v0.1.30, on August 17, 2016.
FAQs
A wrapper around inquirer that makes it easy to create and selectively reuse questions.
The npm package question-cache receives a total of 41,218 weekly downloads. As such, question-cache popularity was classified as popular.
We found that question-cache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.