- Setting up
- Object Constructor
- Prompting the user
- Secondary functions
- Write and read JSON
- Running CLI
Installing all necessaries libraries and importing them into index.js
.
npm i inquirer
uuid
const inquirer = require('inquirer');
Creating Inquirer questions.js
to prompt the user all questions.
const start = [
{
type: 'list',
name: 'options',
message: 'Hello, what would you like to do?',
choices: [
{
name: 'Register a new pet',
value: 0
},
{
name: 'List existing pets',
value: 1
},
{
name: 'Search pet by name',
value: 2
}
]
}
];
After all questions have been registered with Inquirer's format, export all modules.
module.exports = {start, register, search};
Create a pets.json
that receives a results"
property.
{
"results": []
}
Then import into index.js
.
const {start, register, search} = require('./questions');
const {results} = require('./pets.json');
Using JavaScript's Object Constructor method to create a new pet from user's input.
function Pet(id, name, animal, breed, tutor){
this.id = id,
this.name = name,
this.animal = animal,
this.breed = breed,
this.tutor = tutor
};
Accessing prompt
method, returning imported questions.
inquirer.prompt('question').then('answers => { }');
Showing start
questions and accessing user's input inside a switch case
.
- If input
value = 0
, then create a new pet and write it to the JSON file.
inquirer.prompt(start).then(answers => {
switch(answers.options){
case 0:
inquirer.prompt(register).then(answers => {
const newPet = new Pet(v4(), answers.name_pet.toLowerCase(), answers.animal.toLowerCase(), answers.breed.toLowerCase(), answers.user.toLowerCase());
createFile(newPet);
});
break;
- If input
value = 1
, destructuring JSON to retrieve data and show on list.
case 1:
let petsList = [];
for(let pet in results){
let {name} = results[pet];
petsList.push(`${name.charAt(0).toUpperCase() + name.slice(1)}`);
};
inquirer.prompt([
{
type: 'list',
name: 'list_pets',
message: 'Search pet by list of names',
choices: removeDuplicates(petsList)
}
]).then(answers => {
let searchList = answers.list_pets.toLowerCase();
find(searchList);
});
break;
- If input
value = 2
, returnfind()
.
case 2:
inquirer.prompt(search).then(answers => {
let searchName = answers.search_pet.toLowerCase();
find(searchName);
});
break;
};
}
);
Some functions are required to populate objects' properties and receive user's input.
find()
to return search query.
function find(query){
for(let pet in results){
let {name} = results[pet];
if(query == name){
console.table(results[pet]);
};
};
};
removeDuplicates()
to ensure no repeated names are shown on the list.
function removeDuplicates(array) {
let setArray = new Set(array);
let noDuplicates = setArray.values();
return Array.from(noDuplicates);
};
Importing Node's File System.
const fs = require('fs');
A function to read the existing JSON file and register new pets.
File System's method read and parse
data, push
the new object into the array and write a new file.
function createFile(object) {
fs.readFile('./pets.json', 'utf-8', (err, data) => {
if (err) throw err;
let petObj = JSON.parse(data);
petObj.results.push(object);
fs.writeFile('./pets.json', JSON.stringify(petObj, null, 2), 'utf-8', err => {
if (err) throw err;
console.log('Successfully registered a new pet!');
});
});
};
Run node index.js
from terminal command.