<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Christian Dimas</title>
    <description>The latest articles on DEV Community by Christian Dimas (@_christiandimas).</description>
    <link>https://dev.to/_christiandimas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F420223%2F0be28e79-a137-4b02-a884-dcf9e6bfaca0.jpeg</url>
      <title>DEV Community: Christian Dimas</title>
      <link>https://dev.to/_christiandimas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/_christiandimas"/>
    <language>en</language>
    <item>
      <title>Lighter Web Scraping Using NodeJS</title>
      <dc:creator>Christian Dimas</dc:creator>
      <pubDate>Tue, 04 May 2021 15:29:14 +0000</pubDate>
      <link>https://dev.to/_christiandimas/lighter-web-scraping-using-nodejs-3p3a</link>
      <guid>https://dev.to/_christiandimas/lighter-web-scraping-using-nodejs-3p3a</guid>
      <description>&lt;h3&gt;
  
  
  An alternative way for doing web scraping using NodeJS
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---2Njv8Bu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://images.unsplash.com/photo-1607446045657-959e0cccb4fc%3Fcrop%3Dentropy%26cs%3Dtinysrgb%26fit%3Dmax%26fm%3Djpg%26ixid%3DMnwxMTc3M3wwfDF8c2VhcmNofDJ8fHNjcmFwZXxlbnwwfHx8fDE2MjAxNDIwMTA%26ixlib%3Drb-1.2.1%26q%3D80%26w%3D2000" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---2Njv8Bu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://images.unsplash.com/photo-1607446045657-959e0cccb4fc%3Fcrop%3Dentropy%26cs%3Dtinysrgb%26fit%3Dmax%26fm%3Djpg%26ixid%3DMnwxMTc3M3wwfDF8c2VhcmNofDJ8fHNjcmFwZXxlbnwwfHx8fDE2MjAxNDIwMTA%26ixlib%3Drb-1.2.1%26q%3D80%26w%3D2000" alt="Lighter Web Scraping Using NodeJS"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you search for Web Scrapping using NodeJS, probably Puppeteer examples/articles will come up. It is an awesome library to use for complex web scraping because you are actually automating a browser when using Puppeteer. With that said, I think it’s an overkill library to use for a simpler web scrapping. So in this article, we’ll look into how we can scrape data from the web without using Puppeteer&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article is originally published in my blog here: &lt;a href="https://www.christiandimas.com/lighter-web-scraping-using-nodejs/"&gt;https://www.christiandimas.com/lighter-web-scraping-using-nodejs/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;To do this we need to solve two problems. The first one is, how we can get the website HTML code. After that’s solved, the second problem is how to get the actual data that we need from the HTML code.&lt;/p&gt;

&lt;p&gt;Let’s start coding! First, scaffold a new Node project by running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now that we have a project ready to use, let’s install some dependencies&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn install axios cheerio
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Axios
&lt;/h3&gt;

&lt;p&gt;You might be familiar with this package because it’s quite a popular package to use for doing HTTP requests. Nowadays we usually use this to interact with API and get the result as JSON, but there’s a setting that we can tweak so the response will be an HTML instead of JSON.&lt;/p&gt;
&lt;h3&gt;
  
  
  Cheerio
&lt;/h3&gt;

&lt;p&gt;Taken from their NPM Package description, it’s a “&lt;em&gt;Fast, flexible &amp;amp; lean implementation of core jQuery designed specifically for the server&lt;/em&gt;” I think that explains it really well. Basically, with this package, we can run jQuery commands on the server.&lt;/p&gt;


&lt;h2&gt;
  
  
  Building The Scraper
&lt;/h2&gt;

&lt;p&gt;We'll be using &lt;a href="https://books.toscrape.com/"&gt;https://books.toscrape.com/&lt;/a&gt; website to test our scraper. First off, create a file called &lt;code&gt;index.js&lt;/code&gt; in your project folder root, we’ll use this file to build our scraper.&lt;/p&gt;

&lt;p&gt;From the list of books on the website we'll grab a couple of things including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Title&lt;/li&gt;
&lt;li&gt;Price&lt;/li&gt;
&lt;li&gt;Cover Image&lt;/li&gt;
&lt;li&gt;Rating&lt;/li&gt;
&lt;li&gt;Availability&lt;/li&gt;
&lt;li&gt;URL&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's get coding!&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;First, we import both &lt;code&gt;axios&lt;/code&gt; and &lt;code&gt;cheerio&lt;/code&gt; and then we create an async function called &lt;code&gt;scrape&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's grab the HTML code from the website using &lt;code&gt;axios&lt;/code&gt; and load it to &lt;code&gt;cheerio&lt;/code&gt; so we can query the data, to do this we'll do it like this&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;After inspecting the website we can see that the book listing looks like this. This will help us get the data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PjgfWc2---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.christiandimas.com/content/images/2021/05/CleanShot-2021-05-04-at-9.14.44%402x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PjgfWc2---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.christiandimas.com/content/images/2021/05/CleanShot-2021-05-04-at-9.14.44%402x.png" alt="Lighter Web Scraping Using NodeJS"&gt;&lt;/a&gt;HTML Structure for the Book Item&lt;/p&gt;

&lt;p&gt;With that information, let's grab the book elements first. We can do that by using cheerio like this&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Alright, we got the books. Now it's time to grab the simple data first, these are something that we can directly see in the element&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;After that's done, now we can also grab the data that's a bit more complicated like &lt;code&gt;rating&lt;/code&gt;, &lt;code&gt;availability&lt;/code&gt;, and &lt;code&gt;url&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;First off, for &lt;code&gt;rating&lt;/code&gt; we can grab the &lt;code&gt;p&lt;/code&gt; element and check the class because it contains how many ratings the book has (e.g. Three). Next up, for the availability we can just check is there any div with a class of &lt;code&gt;.instock.availability&lt;/code&gt;, we query for both classes to make sure that the &lt;code&gt;.instock&lt;/code&gt; class is really for the availability, and the &lt;code&gt;.availability&lt;/code&gt; has &lt;code&gt;.instock&lt;/code&gt; class to show that it is available.&lt;/p&gt;

&lt;p&gt;All done! This is what the complete code looks like&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I think this is the simplest way to do web scraping, and there are some pros and cons of doing it this way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pros
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Simpler to build&lt;/li&gt;
&lt;li&gt;Fewer resources needed (library like Puppeteer needs to install Chromium to run)&lt;/li&gt;
&lt;li&gt;Smaller package size&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cons
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Cannot scrape a website where navigation is needed (sign in, scroll, etc.)&lt;/li&gt;
&lt;li&gt;Cannot take a screenshot of the page&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the end, it depends on what website do you want to scrape and what data that you want to get. If you want to get something from a complex website then yes, use something like Puppeteer! It has a powerful API and you can interact with a complex website. But if you need something simple, then &lt;code&gt;axios&lt;/code&gt; and &lt;code&gt;cheerio&lt;/code&gt; might be a better choice&lt;/p&gt;




&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;Here are some resources for all the things that I've mentioned in this tutorial&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Axios:  &lt;a href="https://github.com/axios/axios"&gt;https://github.com/axios/axios&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Cheerio: &lt;a href="https://github.com/cheeriojs/cheerio"&gt;https://github.com/cheeriojs/cheerio&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Puppeteer: &lt;a href="https://github.com/puppeteer/puppeteer"&gt;https://github.com/puppeteer/puppeteer&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>node</category>
      <category>webscraping</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Build an Interactive CLI Using TypeScript</title>
      <dc:creator>Christian Dimas</dc:creator>
      <pubDate>Mon, 20 Jul 2020 14:21:29 +0000</pubDate>
      <link>https://dev.to/_christiandimas/build-an-interactive-cli-using-typescript-11fl</link>
      <guid>https://dev.to/_christiandimas/build-an-interactive-cli-using-typescript-11fl</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This article is originally published on &lt;a href="https://www.christiandimas.com/build-interactive-cli-using-typescript/"&gt;My Blog&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this article, we're going to build a CLI using Typescript and a framework called OCLIF. We'll make it interactive so that it's really easy to pick up and use for the first time.&lt;/p&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note: In this part, I'm going to explain how a CLI is structured. Feel free to skip if you already know this.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before we continue let's take a look at how a CLI is constructed. I'm going to use the npm &lt;code&gt;CLI&lt;/code&gt; here as an example. We usually call the npm command like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save package_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;A complete CLI is usually made of four parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Command&lt;/strong&gt;: This is the first word that we type when using a CLI in this case, it's the word &lt;code&gt;npm&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sub-Command&lt;/strong&gt;: This is an optional word that comes after the command. In this case, it's the word &lt;code&gt;install&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flags&lt;/strong&gt;: This is one of the ways to send an option to the CLI. It is started with the dash (&lt;code&gt;-&lt;/code&gt;) symbol. In this case, it's the &lt;code&gt;--save&lt;/code&gt; or a shorter version of it, the &lt;code&gt;-S&lt;/code&gt;. The flag can also contain a value; when it needs a value it will be added like this: &lt;code&gt;--foo=bar&lt;/code&gt; or &lt;code&gt;-f=bar&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Arguments&lt;/strong&gt;: This is the other way to send an option to the CLI. The difference from using flags is that the argument doesn't start with a dash and must be added in the correct order. In this case, it's the &lt;code&gt;package_name&lt;/code&gt; - you might notice the package_name argument is the first one to be added. If you call it, like &lt;code&gt;npm install foo package_name&lt;/code&gt;, then the install process will get &lt;code&gt;foo&lt;/code&gt; as it's package_name.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that's clear, let's get started with the actual project!&lt;/p&gt;


&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;We are going to need two NPM libraries for our core functionality. The first one is called &lt;code&gt;OCLIF&lt;/code&gt;, which stands for Open CLI Framework. This library provides us with a complete workflow of building a CLI. The other library we need is called &lt;code&gt;Inquirer&lt;/code&gt;, this will help us make the CLI interactive and user friendly.&lt;/p&gt;

&lt;p&gt;There are two ways to create a new project with OCLIF.&lt;br&gt;
The first is by installing it globally, then running the command like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn global add oclif 
oclif multi pizza-cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The other way is to simply use &lt;code&gt;npx&lt;/code&gt;, like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx oclif multi pizza-cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note: We are calling the oclif command with &lt;code&gt;multi&lt;/code&gt; as an argument, this will tell oclif to create a multi command CLI. Think of it like the &lt;code&gt;npm&lt;/code&gt; command where you can pass in subcommands like &lt;code&gt;npm install&lt;/code&gt;, &lt;code&gt;npm uninstall&lt;/code&gt;, etc.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;OCLIF also supports creating a single command CLI. Something like the &lt;code&gt;ls&lt;/code&gt; command, where it only has one functionality&lt;/p&gt;

&lt;p&gt;This command will give us a few questions, which will impact how the project scaffold will be laid out&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OvYH-H92--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dz50ys8qrwvoy66izcm8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OvYH-H92--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dz50ys8qrwvoy66izcm8.png" alt="OCLIF Init"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most of the questions are self-explanatory and will be added to your package.json file. Two questions that you should note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NPM Package Name&lt;/strong&gt;: This will be used when you are publishing the CLI to NPM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Command bind name the CLI will export&lt;/strong&gt;: This is the command that you type on the Terminal to use this CLI like npm, ls, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the scaffolding process is completed, move to your project directory and open it in your code editor (I'll be using VSCode in this article):&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd pizza-cli
code .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Project Structure
&lt;/h2&gt;

&lt;p&gt;The project structure will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TjCYgNxp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mk3kfs0o169gb2nv1xxm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TjCYgNxp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mk3kfs0o169gb2nv1xxm.png" alt="Project Structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, you already have a file inside the command folder called &lt;code&gt;hello.ts&lt;/code&gt;. This file is the only thing we need to have a hello command.&lt;/p&gt;

&lt;p&gt;Let's try it out! Back in your terminal, type this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./bin/run hello     # This will call the hello subcommand
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You can also run:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./bin/run --version   # This will show the cli version
./bin/run --help      # This will show a generated help for the CLI
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Cool! You just created your first CLI!&lt;br&gt;
Now, let's see what's inside the &lt;code&gt;hello.ts&lt;/code&gt; file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Your &lt;code&gt;hello.ts&lt;/code&gt; file will look something like the snippet above. Let's look at a couple of interesting things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Description and Examples&lt;/strong&gt;: This will show up when you run the help subcommand, and is used to provide more info for the user that's using it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flags&lt;/strong&gt;: This is where you define all your available flags for the subcommand. This will be parsed as JSON in the code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Args&lt;/strong&gt;: This is where you define all your available arguments. One thing to note here is that the order of the argument matters because it will affect how the CLI is used.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run&lt;/strong&gt; method: The run() method is the one that's executed when you call the CLI. There are no parameters to this method but we can get all the arguments and flags by using the this.parse() method, as you can see at line 23.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we've understood the content of the file. Let's modify it a little, so that it matches our needs.&lt;/p&gt;

&lt;p&gt;First, let's change the filename from &lt;code&gt;hello.ts&lt;/code&gt; to &lt;code&gt;create.ts&lt;/code&gt;, and the Class name from Hello to Create. This will change the subcommand from hello to create, and we can call it like this:&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./bin/run create.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now let's modify the description and examples to look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Next, we add some more flags and arguments. It should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The last step is updating the &lt;code&gt;run()&lt;/code&gt; method so we can see what the args and flags look like. The updated &lt;code&gt;run()&lt;/code&gt; method should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;With everything updated, the whole file should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now, when you go back to the terminal, you can call the command like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./bin/run create 2 -t=pepperoni -c=thin -x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Or if you prefer the more verbose way, you can also do this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./bin/run create 2 --toppings=pepperoni --crust=thin --extraSauce
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You will see all the flags and arguments that we passed in formatted as a nice JSON object that's easy to work with.&lt;br&gt;
Now that we have all the core functionality implemented, it's time to make it more interactive!&lt;/p&gt;


&lt;h2&gt;
  
  
  Making It Interactive
&lt;/h2&gt;

&lt;p&gt;To make the CLI more interactive and user friendly, we'll need an additional NPM package called Inquirer. You can install it like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add inquirer
yarn add -D @types/inquirer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After that's installed, let's modify our run method to look something like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;In line 1 we're importing the prompt() method from inquirer, then in the run() method, instead of using &lt;code&gt;this.parse()&lt;/code&gt; to get all the arguments and flags that are passed in, we call the &lt;code&gt;prompt()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;prompt()&lt;/code&gt; method takes an array of questions that the user is asked when they run the CLI subcommand. The most basic question contains a type and message key, for the full options that you can use in the question please go here.&lt;/p&gt;

&lt;p&gt;With everything now set up, now you can execute the CLI like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./bin/run create
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now, instead of adding all the arguments and flags when executing the CLI, it will ask you interactively for the data that it needs.&lt;/p&gt;

&lt;p&gt;Congratulations! You just built your first, super user-friendly, interactive CLI!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9zq4xLIe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/njrt9y7v5fcd55bx2gbf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9zq4xLIe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/njrt9y7v5fcd55bx2gbf.png" alt="Interactive CLI"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Further Improvements
&lt;/h2&gt;

&lt;p&gt;In this part of the article, I want to discuss some improvements that, in my opinion, will make the CLI better.&lt;/p&gt;
&lt;h3&gt;
  
  
  Make the interactive prompt optional
&lt;/h3&gt;

&lt;p&gt;This might sound a bit weird. Why would I make the optional prompt optional when it has a better user experience than the usual CLI?&lt;br&gt;
My argument is that for a power user who's already familiar with the CLI it's actually faster to just add all the arguments and flags they need, instead of going through the interactive prompt.&lt;br&gt;
To do this, we need to modify the run() method slightly, and make it look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;I'm moving the prompt process to a new method, and in the run method, we are checking the arguments and the flags. If it exists then we use that - but if not, we run the prompt process.&lt;br&gt;
With this implementation, the user now has two ways of using the CLI.&lt;/p&gt;
&lt;h3&gt;
  
  
  Adding color and loading bar
&lt;/h3&gt;

&lt;p&gt;The next improvement I want to make is to make the CLI nicer to look at and use. Firstly, by adding color to the this.log method, so it's not just white. Secondly, by showing a loading bar when a process is running to give a better user experience.&lt;br&gt;
To do those, we need to install two packages. We need chalk, for adding color to the &lt;code&gt;this.log&lt;/code&gt; and we need &lt;code&gt;cli-progress&lt;/code&gt; to show a loading bar. &lt;/p&gt;

&lt;p&gt;We can install it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add cli-progress chalk
yarn add -D @types/cli-progress @types/chalk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;With these packages installed, let's update our code again:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;First, I introduce a new method called &lt;code&gt;makePizza()&lt;/code&gt;.&lt;br&gt;
This is just to simulate a process running.&lt;br&gt;
Inside that method, I'm calling a &lt;code&gt;sleep()&lt;/code&gt; method. This is just a simple helper method to make sure the process doesn't finish too quickly.&lt;/p&gt;

&lt;p&gt;Then use the chalk package to add color to our logging is actually pretty simple, we just need to import the color method that we need. In this case, we are using yellow, green, and cyan. Then we can just wrap the text with that method. As Simple as that, we get a colored log!&lt;/p&gt;

&lt;p&gt;The next thing we do is add the loading bar. &lt;br&gt;
First, we import the SingleBar and Presets from &lt;code&gt;cli-progress&lt;/code&gt;.&lt;br&gt;
Then, on line 20, we initialize the loading bar and giving it a custom format. On line 24 we call the &lt;code&gt;progressBar.start(length, startFrom)&lt;/code&gt; method, this is used to set the loading bar length and start value.&lt;/p&gt;

&lt;p&gt;To simulate a process, we loop for each pizza to make a topping, to increment the loading bar value by one. With all of this now set up, our CLI looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZKPAru6e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t0sua57gu0decoay9yq5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZKPAru6e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t0sua57gu0decoay9yq5.gif" alt="Final Result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty cool!&lt;/p&gt;

&lt;p&gt;To learn more about all of the things that we've used, visit the links below. Thanks for reading this far and see you in the next article!&lt;/p&gt;




&lt;p&gt;Resources:&lt;br&gt;
OCLIF: &lt;a href="https://oclif.io/"&gt;https://oclif.io/&lt;/a&gt;&lt;br&gt;
Inquirer: &lt;a href="https://github.com/SBoudrias/Inquirer.js/"&gt;https://github.com/SBoudrias/Inquirer.js/&lt;/a&gt;&lt;br&gt;
Chalk: &lt;a href="https://github.com/chalk/chalk"&gt;https://github.com/chalk/chalk&lt;/a&gt;&lt;br&gt;
CLI-Progress: &lt;a href="https://github.com/AndiDittrich/Node.CLI-Progress"&gt;https://github.com/AndiDittrich/Node.CLI-Progress&lt;/a&gt;&lt;br&gt;
Project Repo: &lt;a href="https://github.com/kenanchristian/pizza-cli"&gt;https://github.com/kenanchristian/pizza-cli&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>cli</category>
    </item>
  </channel>
</rss>
