Closed kitchen-boy closed 2 years ago
Create the .gitignore file during the initial setup of the project to avoid accidentally tracking the folder with the git add -A command. If the node_modules folder was accidentally tracked by git, use the following command to remove the tracking: $ git rm -r --cached node_modules.
const inquirer = require('inquirer'); // Verify that inquirer is available by adding console.log(inquirer) in the app.js file. console.log(inquirer)
Not for the functionality of the application, but remember you also need to code for humans to read. Try to group by source: npm packages, personal modules, and core library modules often get grouped together.
Use inquirer to formulate the profile questions
Objects/Question/ - a hash containing question related values:
message: (String|Function) The question to print. If defined as a function, the first parameter will be the current inquirer session answers. Defaults to the value of name (followed by a colon).
inquirer .prompt([ { type: 'input', name: 'name', message: 'What is your name?' } ]) .then(answers => console.log(answers));
Notice that
The
Save and run the app. You should see a prompt like the following image in the terminal:
Place the call to inquirer.prompt() in a function so that it can be invoked on demand within the flow of the application.
In app.js, wrap <inquirer.prompt()> inside a
promptUser().then(answers => console.log(answers));
Notice that the function returns a running of inquire.prompt(), thus returning what it returns, which is a Promise. Just like fetch(), which we covered previously, the Promise will resolve with a .then() method.
So, here we're calling a function that returns the result of inquire.prompt, which is a Promise. We therefore append the .then() method to the function call, since it returns a Promise, and we put into .then() whatever we wish to take place after the Promise is resolved.
This allows the function expression to have a single responsibility: to prompt the user. The Promise from inquirer can now be handled by the function call, which helps maintain best practices—in contrast to how callbacks dealt with asynchronous behavior.
const promptUser = () => { return inquirer.prompt([ { type: 'input', name: 'name', message: 'What is your name?' }, { type: 'input', name: 'github', message: 'Enter your GitHub Username' }, { type: 'input', name: 'about', message: 'Provide some information about yourself:' } ]); };
const promptProject = () => {
console.log( Add a New Project
);
return inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'What is the name of your project?'
},
{
type: 'input',
name: 'description',
message: 'Provide a description of the project (Required)'
},
{
type: 'checkbox',
name: 'languages',
message: 'What did you build this project with? (Check all that apply)',
choices: ['JavaScript', 'HTML', 'CSS', 'ES6', 'jQuery', 'Bootstrap', 'Node']
},
{
type: 'input',
name: 'link',
message: 'Enter the GitHub link to your project. (Required)'
},
{
type: 'confirm',
name: 'feature',
message: 'Would you like to feature this project?',
default: false
},
{
type: 'confirm',
name: 'confirmAddProject',
message: 'Would you like to enter another project?',
default: false
}
]);
};
promptUser() .then(answers => console.log(answers)) .then(promptProject) .then(projectAnswers => console.log(projectAnswers));
const promptProject = portfolioData => {
Add a New Project
& initialize it: portfolioData.projects = [];
// If there's no 'projects' array property, create one. if (!portfolioData.projects) { portfolioData.projects = []; }
.then(projectData => { portfolioData.projects.push(projectData); });
.then(projectData => { portfolioData.projects.push(projectData); if (projectData.confirmAddProject) { return promptProject(portfolioData); } else { return portfolioData; } });
else { return portfolioData; }
promptUser() .then(promptProject) .then(portfolioData => { console.log(portfolioData); });
{ type: 'input', name: 'name', message: 'What is your name? (Required)', validate: nameInput => { if (nameInput) { return true; } else { console.log('Please enter your name!'); return false; } } },
The <validate> method receives an argument.
This argument is the the user's input, <nameInput>.
The conditional statement in the function block of the <validate> method: if the condition evaluates to <true>, the validation has passed successfully. if the condition evaluates to <false**>, the user receives a message & is prompted with the same question until an answer is received.
Add this <validation> property to the following questions to ensure that the user doesn't skip these questions.
Label the questions as req'd: -- GitHub username -- Project name -- Project description -- Project GitHub link
Last step: Ask user if they'd like to add an About section. -- Then prompt user for info, but only after they confirm yes.
Must add a confirmation type question & then ask for the info.
Add the following confirmation question to the <promptUser()> function just below the GitHub username prompt in the <app.js> file:
{ type: 'confirm', name: 'confirmAbout', message: 'Would you like to enter some information about yourself for an "About" section?', default: true },
{ type: 'confirm', name: 'confirmAbout', message: 'Would you like to enter some information about yourself for an "About" section?', default: true }, { type: 'input', name: 'about', message: 'Provide some information about yourself:', when: ({ confirmAbout }) => { if (confirmAbout) { return true; } else { return false; } } }
The <inquirer> method automatically passes an object containing the user's answers to the <when> function. -- This allows us to write conditional code based on the answers the user has supplied thus far.
When the <when> function gets this object, it looks something like this:
{ "name": "lernantino", "github": "lernantino", "confirmAbout": true }
Learned the following things:
Search the npm registry to find stable long-term solutions.
Install packages from npm and learn the purpose of the <package.json>, <package-lock.json>, and <node_modules> assets.
Research documentation to find out how to use packages like <inquirer>.
Chain <inquirer>'s Promises to control the flow of the application when prompting users.
Validate user answers with <inquirer> and conditionally prompt users based on answers.
End of Lesson 3: Code status:
Beginning of Lesson 4: Code status:
Description
Profile questions
Name
GitHub account name
About me
Project questions
Project name
Project description
Programming Languages
Project link