promptsify v1.1.0
promptsify
Configure prompts with just a json.
❯ Install
npm install --save promptsify
❯ Usage
const { promptsify } = require('promptsify');
const json = [
{
prompt: [
{ type: 'text', name: 'name', message: 'What is your name?' },
{ type: 'text', name: 'surname', message: 'What is your surname?' },
],
},
{ variables: { name: { $concat: ['$name', ' ', '$surname'] } } },
{ prompt: { type: 'number', name: 'age', message: 'What is your age?' } },
{
cond: {
if: { $gte: ['$age', 18] },
then: [
{
prompt: {
type: 'select',
name: 'drivingLicense',
message: 'Do you have a driving license?',
choices: [
{ title: 'Yes', value: true },
{ title: 'No', value: false },
],
},
},
],
else: [{ variables: { drivingLicense: false } }],
},
},
{
variables: {
i: 0,
sportsQuestions: [
{ question: 'Do you like football?', value: 'football' },
{ question: 'Do you like basketball?', value: 'basketball' },
{ question: 'Do you like tennis?', value: 'tennis' },
],
sports: [],
},
},
{
loop: {
while: { $lt: ['$i', { $size: '$sportsQuestions' }] },
do: [
{
variables: {
sportQuestion: { $arrayElemAt: ['$sportsQuestions', '$i'] },
},
},
{
prompt: {
type: 'select',
name: 'sport',
message: '$sportQuestion.question',
choices: [
{ title: 'Yes', value: true },
{ title: 'No', value: false },
],
},
},
{
cond: {
if: { $eq: ['$sport', true] },
then: [
{
variables: {
sports: {
$concatArrays: ['$sports', ['$sportQuestion.value']],
},
},
},
],
else: [],
},
},
{ variables: { i: { $add: ['$i', 1] } } },
],
},
},
{
return: {
name: '$name',
age: '$age',
drivingLicense: '$drivingLicense',
sports: '$sports',
},
},
];
promptsify(json).then(result => console.log(result));
❯ Examples
Prompts
Asks for the name and surname and returns the full name.
const { promptsify } = require('promptsify');
const json = [
{
prompt: [
{ type: 'text', name: 'name', message: 'What is your name?' },
{ type: 'text', name: 'surname', message: 'What is your surname?' },
],
},
{ return: { $concat: ['$name', ' ', '$surname'] } },
];
promptsify(json).then(result => console.log(result));
Variables
Saves data in variables to use them in the next step.
const { promptsify } = require('promptsify');
const json = [
{
prompt: [
{ type: 'text', name: 'name', message: 'What is your name?' },
{ type: 'text', name: 'surname', message: 'What is your surname?' },
],
},
{ variables: { name: { $concat: ['$name', ' ', '$surname'] } } },
{ return: { name: '$name' } },
];
promptsify(json).then(result => console.log(result));
Conditions
Asks the user if he has a driving license, if he is older than 18.
const { promptsify } = require('promptsify');
const json = [
{
prompt: [
{ type: 'text', name: 'name', message: 'What is your name?' },
{ type: 'text', name: 'surname', message: 'What is your surname?' },
],
},
{ variables: { name: { $concat: ['$name', ' ', '$surname'] } } },
{ prompt: { type: 'number', name: 'age', message: 'What is your age?' } },
{
cond: {
if: { $gte: ['$age', 18] },
then: [
{
prompt: {
type: 'select',
name: 'drivingLicense',
message: 'Do you have a driving license?',
choices: [
{ title: 'Yes', value: true },
{ title: 'No', value: false },
],
},
},
],
else: [{ variables: { drivingLicense: false } }],
},
},
{ return: { name: '$name', age: '$age', drivingLicense: '$drivingLicense' } },
];
promptsify(json).then(result => console.log(result));
Loops
Asks the user what sports he likes.
const { promptsify } = require('promptsify');
const json = [
{
prompt: [
{ type: 'text', name: 'name', message: 'What is your name?' },
{ type: 'text', name: 'surname', message: 'What is your surname?' },
],
},
{ variables: { name: { $concat: ['$name', ' ', '$surname'] } } },
{ prompt: { type: 'number', name: 'age', message: 'What is your age?' } },
{
cond: {
if: { $gte: ['$age', 18] },
then: [
{
prompt: {
type: 'select',
name: 'drivingLicense',
message: 'Do you have a driving license?',
choices: [
{ title: 'Yes', value: true },
{ title: 'No', value: false },
],
},
},
],
else: [{ variables: { drivingLicense: false } }],
},
},
{
variables: {
i: 0,
sportsQuestions: [
{ question: 'Do you like football?', value: 'football' },
{ question: 'Do you like basketball?', value: 'basketball' },
{ question: 'Do you like tennis?', value: 'tennis' },
],
sports: [],
},
},
{
loop: {
while: { $lt: ['$i', { $size: '$sportsQuestions' }] },
do: [
{
variables: {
sportQuestion: { $arrayElemAt: ['$sportsQuestions', '$i'] },
},
},
{
prompt: {
type: 'select',
name: 'sport',
message: '$sportQuestion.question',
choices: [
{ title: 'Yes', value: true },
{ title: 'No', value: false },
],
},
},
{
cond: {
if: { $eq: ['$sport', true] },
then: [
{
variables: {
sports: {
$concatArrays: ['$sports', ['$sportQuestion.value']],
},
},
},
],
else: [],
},
},
{ variables: { i: { $add: ['$i', 1] } } },
],
},
},
{
return: {
name: '$name',
age: '$age',
drivingLicense: '$drivingLicense',
sports: '$sports',
},
},
];
promptsify(json).then(result => console.log(result));
❯ How To Use
To use this library, you just have to import the promptsify
function and introduce a json object. The json object defines all the information about the prompts, the associated logic and the return of the function. This package uses prompts and mongu under the hood.
The json that promptsify
receives as argument has to be an array in which every element has to be of a specific type. There are different types that can be used, and each one of them is represented as an object with a key that has the name of the type.
prompt
The prompt element is used to define the prompts. Its value is a mongu expression that resolves to an object that you would insert inside the function of the prompts package:
{ "prompt": "expression" }
This is an example:
const json = [
{
prompt: {
type: 'text',
name: 'name',
message: 'What is your name?',
},
},
{ return: '$name' },
];
return
The return element is used to define the return of the function. Its value is a mongu expression that resolves to anything you want to return:
{ "return": "expression" }
This is an example:
const json = [
{
prompt: [
{ type: 'text', name: 'name', message: 'What is your name?' },
{ type: 'text', name: 'surname', message: 'What is your surname?' },
],
},
{ return: { $concat: ['$name', ' ', '$surname'] } },
];
variables
The variables element is used to define variables. Its value is a mongu expression that resolves to an object in which each key will be a variable:
{ "variables": "expression" }
This is an example:
const json = [
{
prompt: [
{ type: 'text', name: 'name', message: 'What is your name?' },
{ type: 'text', name: 'surname', message: 'What is your surname?' },
],
},
{ variables: { name: { $concat: ['$name', ' ', '$surname'] } } },
{ return: { name: '$name' } },
];
cond
The cond element is used to define a condition. Its value is an object with a condition, a list of elements when the condition is met and a list of elements when the condition is not met:
{
"cond": {
"if": "condition",
"then": ["element", "..."],
"else": ["element", "..."]
}
}
This is an example:
const json = [
{ prompt: { type: 'number', name: 'age', message: 'What is your age?' } },
{
cond: {
if: { $gte: ['$age', 18] },
then: [
{
prompt: {
type: 'select',
name: 'drivingLicense',
message: 'Do you have a driving license?',
choices: [
{ title: 'Yes', value: true },
{ title: 'No', value: false },
],
},
},
],
else: [{ variables: { drivingLicense: false } }],
},
},
{
return: {
age: '$age',
drivingLicense: '$drivingLicense',
},
},
];
loop
The loop element is used to define a loop. Its value is a condition and a list of elements:
{
"loop": {
"while": "expression",
"do": ["element", "..."]
}
}
This is an example:
const json = [
{
variables: {
i: 0,
sportsQuestions: [
{ question: 'Do you like football?', value: 'football' },
{ question: 'Do you like basketball?', value: 'basketball' },
{ question: 'Do you like tennis?', value: 'tennis' },
],
sports: [],
},
},
{
loop: {
while: { $lt: ['$i', { $size: '$sportsQuestions' }] },
do: [
{
variables: {
sportQuestion: { $arrayElemAt: ['$sportsQuestions', '$i'] },
},
},
{
prompt: {
type: 'select',
name: 'sport',
message: '$sportQuestion.question',
choices: [
{ title: 'Yes', value: true },
{ title: 'No', value: false },
],
},
},
{
cond: {
if: { $eq: ['$sport', true] },
then: [
{
variables: {
sports: {
$concatArrays: ['$sports', ['$sportQuestion.value']],
},
},
},
],
else: [],
},
},
{ variables: { i: { $add: ['$i', 1] } } },
],
},
},
{
return: {
sports: '$sports',
},
},
];