1.0.0 • Published 8 years ago

nifty-setup v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

Nifty-setup

Nifty setup provides an easy way to request data from a user.

Example:

To demonstrate, here is a program which will ask the user to submit their full name and email.

const niftySetup = require('nifty-setup');

niftySetup({
    questions: {
        firstName: {
            text: 'What is your first name?',
        },
        lastName: {
            text: 'What is your last name?',
        },
        email: {
            text: 'What is your email?',
            validator: (answer) => {
                return answer.includes('@'); // I think we can all agree that this is the best way to validate email input.
            },
        }
    },
    result: (answers) => {
        console.log('first name: ' + answers.firstName);
        console.log('last name: ' + answers.lastName);
        console.log('email: ' + answers.email);
    },
}, {
    input: process.stdin,
    output: process.stdout,
});

The 'niftySetup' function takes one or two arguments. The first argument is required and describes the setup process. It must be an object with fields, questions and results. The second parameter is optional and allows you to specify which streams the setup reads and writes to. These default to stdin and stdout.

Questions

The questions field should be an object with a sub object for each question. Each of these defines a question and they are executed in the order defined.

Each question can have text and a validator.

The text should be the question prompt and is displayed to the user.

The validator is a function which is called when the user gives an answer where the answer is passed into the validator, if the validator returns true the next question will be asked, if the validator returns false the question will be asked again.

Result

The result function is called once the user has completed the setup, with the answers that the user provided. To get an answer, simply use the key on the answer object that is the same as the key of the question.

Options

The optional options object simply lets you define what streams you want to read and write to, see the example.

1.0.0

8 years ago