Build a Dynamic CLI with Inquirer and Node.js: A Step-by-Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video I'm going to talk about inquiry package basically if you're going to build some node.js CLI you're going to find this package very useful and handy if you've been working with for example eslint or typescript or other packages you notice when you're going to initialize them they're going to ask a bunch of questions and based on those questions they're going to give you some feedback and they're going to build your project as well so this is what we're going to actually build here so without wasting any time we're going to install it so you can just install it with npm installer I Inquirer and basically you're going to get a version nine and with a version 9 because it is native esm modules you are going to use import inquiry from Inquirer or if you want to use for example the common JS the common JS version or like the required version you need to install version 8. so assuming you're going to use the latest version whatever version is that which is as of today it's Version 9 and Beyond then you're gonna get the esm modules with the esm modules you can see you have to do just a bit of things on your package.json in a package.json you need to add type modules and if you add this one all good to go so we have Inquirer and then well obviously here is a Dev dependency it doesn't matter depends on your project and also you're gonna have the type module once you have the type module you can use it in a way that explained in the documentation so in order to use it it's very simple you're gonna just import it like this and then you're going to have a bunch of questions whatever you want to ask and then the rest would be as follow so I'm going to zoom in a little bit you can see it now here so with the inquiry you're going to prompt questions and then you're going to have the answers and then we're gonna just do for example console log one of the items in the answers or all of them you're going to use console.dir to just look at all of them but is the console would be an object which contains all the answers okay so now I'm here and if I want to just show all my questions I'm going to ask two questions basically one would be a list the type of the question would be a list so they can choose one of these two JavaScript or typescript and the message would be this one there's a default one the other one would be a normal input so we're going to ask the questions and they're going to answer both of them has default you're gonna see it you're gonna see them and both of them has name so with the name this is what exactly you're going to use with your answers as you can see we have answers.jsts this name is this one so if you want to know what answer they chose for that the specific question or then this is how you're gonna get it but enough talking let's see it in example so I'm gonna just do note and then simple one here we're going to ask the question do you prefer JavaScript or typescript it comes from the message and then we have choices because the type is list obviously we have choices JavaScript and typescript the reason that typescript is already selected is because we have default and then I'm going to just press enter Because that's the default one and then what do you prefer why do you prefer this one so we can say so it could be anything I'm going to press enter now you have an object your object will have jsts attributes and reason attributes there we go so we have reason and we have jsts and also we have default one which you can see and if you want to have access one of them obviously it's a plain JavaScript you can just say answer dot jsts I'm going to get rid of this so to have a better or clear one we're going to comment out the typescript as a default one and the default for the input and run it one more time so now you can see the default one is Javascript I'm going to choose JavaScript I don't have any answer let's say I don't know why I chose JavaScript you're going to press enter now because we didn't have any default then this is going to be something like this still the same thing but you can provide default if you wanted so we have a type list and then also we have type input normal input and the rest would be pretty much everywhere the same thing but let's just build up on this on top of this and talk about the rest so with the pizza example everything is exactly the same thing so we have inquiry prompt questions then we're going to just log out all the answers and then we have array of questions to just prove the points so we're going to say node.js then Pisa then we're gonna get a little bit different one so we can choose for example a toppings so I'm going to choose for example this one and this one and this one with the cheese we're going to choose both of them why not and then we they don't have olives that's not good so we're not going to add pineapple but we might add extra cheese I'll press enter now we can see what toppings are we gonna get so everything will be pretty much simple as this you have interactive CLI that you can really play and do a lot of things but if now we look at the questions there we go the type of the question was checkbox instead of list or input and the message was choose stopping the name of this answer would be toppings that's why you get array here and then there are choices where the choices you can see we had separator now if I run it one more time we have a separator so that's a separator here they call it meat and then we have some number some options and then we have another separator cheese as you could see the mozzarella was chosen because the checked was true so if you scroll if you are here and scroll down on the left side you can see mozzarella is chosen and also we have another one you the usual one and also we have extras extras because this is disabled so we couldn't choose it and then the rest would be pretty much the same thing so so expand is exactly the same thing I'm gonna just get out of this one and then do node.js dot expand I'll open this one so basically the question is saying there's a conflict on a file what do you want to do I have no idea what do they mean I assume H's help but let's say we don't know anything we're gonna press enter and now it's going to show all the possible answers that we can choose I'm going to choose r r doesn't exist press enter it says please enter enter a valid command and now I'm going to choose for example D I want to see the different and then now you can see the answer was diff in order to get all of these things obviously now we have type expand where the type expand we have a message and then the name is override so the overwrite is the name of the answer and with this type expand we have a bunch of choices so you can see this is array if I just open it now we have a key which is y and then it's going to choose the value overwrite key a it's going to give us the override all D is for the div and then we have a separator and we have X for a bird so just run it one more time you can see if I press enter now we have these three we have some messages or the name but the actual value is different one and then when we continue we have a separator so here and then we have X4 about so it depends what I do I'm going to choose X now we're gonna get about and we can do anything that we want so now we have recursive one with a recursive one you're going to ask the questions continuously what do I mean is simple so if I just do note then recursive it's going to ask what's my favorite TV show my favorite TV show let's say is friends I want to enter another TV favorite why not let's call yes and then I'm gonna call Spartacus it's gonna ask again and if I just press yes and just say Big Bang Theory then again yes Vikings actually here I'm gonna press enter Vikings so it's not going to stop until I just press n then it's going to give me the list of all of them okay how we achieve this is very simple so we are asking a question basically let's let's see the questions so we have two questions one is a normal input so it's going to ask it and the other one is the confirm so with the confirm is yes and no the default was yes as you could see and the Y was just a little bit bigger so if I just press this one so the default one is yes because that's why it says just hit enter for yes and I'm gonna just put X and then press n we're going to get out so because we had default yes then it was continuing next thing that we can see here is a little bit interesting because first thing first it's going to just prompt the questions then the next thing that is going to do is just add it to the array so we have an array on the top so all the answers answers.tvshow which is this bit and then it's going actually to check for the ask again ask again if it is true which is default is true then it's going to call the function one more time the whole functions run it every time and then once we are done we're just saying these are your favorite one so that's how you're going to achieve the recursive one the next so the next thing that we're going to do we're going to put everything we learned in one project and see what do we get so here with the questions we're going to answer the email we're going to ask for the password ID numbers and quite a few other things but I think before just explaining them let's see the demo I'm going to just say node and then let's run it complex the complex one is going to ask for the email password is something very strong and then the user ID I'm gonna just put 200 why not bill number will be three and then where do you want to log in assuming for example we are building a CLI to login into a system so we're going to log into development we have username and password obviously on the top uh everything we have confirmed yes they're all good do you want to log let's say no we don't want to log and then we actually want to deploy logging system and auth system and API I'm going to press enter spell checker let's just run the spell checker as well why not and any comment we can just press enter and then here I will just start typing so oh good and I am happy okay so then press Escape column WQ so we can write and quit we just get out and everything now is here so we have email we have the password we have user ID build number bit number Target and everything else okay how did we achieve all of these things very simple so the name is just a normal input we're going to just put the email one the password is type password that's why we would see we could see the asterisks or stars because we were masking everything and the type was password the next thing is the input which is the user ID obviously here we have default which was thousand so if I just do one more time here that's why you you see the number thousand because there's a default here and also if it is not a number there is a validate function it's going to validate the answer and if it's not what we show a message otherwise it's true so as long as the validate function doesn't return true we're not going to go to next one and remember something here we are checking the answer answer is not it's very different from the one which we have at the very bottom that's an object but here this answer belonged to these questions so whatever is this question it could be anything it could be for example a and then we can just do something like that but we're going to use answer just for the Simplicity but here validate function receive an answer for the current question just remember this one so now if I just put something which is not an integer I'm going to put x y z now we're gonna go to the validate it's not an integer we're going to get this result and now if I just remove it to one two three you're gonna go to the next one so with the next one actually it's a number so the type is number so I need to press I need to provide number and if I don't provide number I'm going to get none 2 here is a number but if I say ABC we're gonna get none because it wasn't a number that's a type and again the next thing is the type list so what do you want to login uh where do you want to log in because we have username and password let's just choose local and then have you checked that's the next question you can see here if you have checked the default is true that's why Y is bigger so I'm going to press enter and then should I log in again that's another confirm questions so we'll continue here would you like to do uh would you like what the call do you want to run the spell checker so something very interesting he didn't ask this question it jumped to this one the reason is very simple so here obviously we have the services that we wanted to deploy but we are going to ask these questions only when this condition is true so only when the answers the target is not local so because here we chose local so if I just let me actually close these things here there you go so the target answers.target so when the answers the target is not local then it's going to ask this question so if the moment I choose local it's not going to ask it because this is going to return false so I'm going to stop it one more time and run it again here I'm going to choose development for example yes and no as you can see now we can choose a series because now this condition is definitely true the target is not local and it's true and it's going to ask the questions so I'm going to choose for example all of them why not and the next thing is going to be a raw list again choosing one of them so I'm going to press enter let's choose for example engv and then any comment so I'm going to press enter Then uh for inset and I'm going to say oh good again then we're gonna just say Escape colon WQ press enter now we're gonna get all the results so this is basically how you're going to do it and all of these things are also available here just make sure if you want to know more you can just have a look at the documentation but pretty much we cover almost everything the only thing that I need to mention one more time if you're going to use any version bigger than 8 then it would be esm modules and then how you can just import them will be with import but if you're going to use common.js or this version require version then you need to use you need to install version 8. I think here at this stage we are at the end of the video and if you like this type of content and you want me to just talk about more npm packages interesting packages please don't forget to share like subscribe and let me know in the comment section and based on the YouTube analytics I can see most of you are not subscribed please if you don't mind press the Subscribe button which is going to support the channel thank you so much and I'm going to see you in the next video
Info
Channel: Dev tips by MoHo
Views: 4,678
Rating: undefined out of 5
Keywords: npm, npm packages, node packge, inquirer, enquirer
Id: CfS6eOBe8AY
Channel Id: undefined
Length: 17min 8sec (1028 seconds)
Published: Wed Mar 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.