Socket
Book a DemoInstallSign in
Socket

ask-once

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ask-once

Only ask a question one time and store the answer.

Source
npmnpm
Version
0.3.1
Version published
Weekly downloads
11
83.33%
Maintainers
2
Weekly downloads
 
Created
Source

ask-once NPM version Build Status

Only ask a question one time and store the answer.

Install

Install with npm

$ npm i ask-once && question-cache --save

Usage

1. Pass an instance of question-cache

var questions = require('question-cache')();
var ask = require('ask-once')(questions);

2. Ask a question!

ask('May I have your username?', function (err, answer) {
  console.log(answer);
});

The user's answer is saved, and the question won't be asked again unless:

  • force: true is passed on the options, or
  • the answer is deleted directly

FAQ

Where are the answers stored?

The user's answers are saved on a global config store that is uniquely identified to the application using ask-once.

Can I change where answers are stored?

Yes, you can use the cwd option. Here's an example:

var questions = require('question-cache')();
// pass your own instance of data-store, so you can use
// whatever storage location you want
var store = require('data-store');

var ask = require('ask-once')({
  store: store('foo', {cwd: 'bar'}),
  questions: questions
});

ask('May I have your username?' function (err, answer) {
  console.log(answer);
});

Docs

options

To re-ask questions or reset the stored values:

  • options.force: will re-ask the given question or questions, regardless of whether or not previously stored values exists.
  • options.init: will delete the entire store and start over again.

API

askOnce

Returns a question-asking function that only asks a question if the answer is not already stored.

Params

  • questions {Object}: Pass your instance of [question-cache][] on the questions parameter.
  • store {Object}: Pass your instance of [data-store][] on the store parameter.
  • returns {Function}: Function to use when asking questions.

Example

var ask = require('ask-once')(questions, store);

ask

Ask a question only if the answer is not stored.

Params

  • question {String}: Key of the question in the questions cache to ask.
  • options {Object}: Options to control re-initializing the answer or forcing the question.
  • cb {Function}: Callback function with the err and answer parameters.

Example

ask('username', function (err, answer) {
  if (err) return console.error(err);
  console.log(answer);
  //=> doowb
});

Examples

First time the program is run, the user is prompted to answer a question:

image

Additional runs of the program will skip prompting the user:

image

Passing the init option will delete all the stored answers and prompt the user to answer the question again:

image

Additional runs after clearing the stop will return the newly saved answer:

image

Passing the force option will force the question to be asked:

image

Additional runs after forcing the question, will return the newly saved answer:

image

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Author

Brian Woodward

License

Copyright © 2015 Brian Woodward Released under the MIT license.

This file was generated by verb-cli on August 30, 2015.

FAQs

Package last updated on 31 Aug 2015

Did you know?

Socket

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.

Install

Related posts