How to create a Todo CLI App with Node JS.
Tags: node, todo, yargs, cli
Published: 2020-11-24 13:34:53

Node JS is very versatile and can be used to develop application on the web, mobile and even on the command line interface.

This tutorial will be focusing on building a Todo App on the command line interface.

In order for us to us develop CLI Application, we need to be able to collect input from the CLI, we can collect inputs in nodejs by using the process.argv.

Step 1.
Open your preferred code terminal and navigate to the folder that will contain your application.

Run npm init -y to initiate your node app and get your started.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i2vxls6xe07fr713vij8.png)

Your folder should now contain a package.json.

Lets test how process.argv works

Create a file, name the file app.js, inside the file, write console.log(process.argv).

Run the node app by typing node app.js Your Name on the terminal.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0g4n8wn7nfx3stco1zhv.png)

The result we got is an array that contains 3 things
1. Path to the node executable file
2. Path to the file, we are trying to run
3. The argument we added (Your Name)

This means that our first argument from the command line can be gotten by typing process.arg[2], subsequent argument will be 3, 4 and so on.

We would not be using process.argv to build our CLI App, i just showed you so you can know what happens behind the scene in the package we are about to use.

We would make use of the yargs package - https://www.npmjs.com/package/yargs

Step 2:

Install Yargs -run npm install yargs

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2xnz5u75878wdbfuaiei.png)

Yargs is now up, lets set it up.

We can delete the console.log(process.argv).

Add yargs to app.js

const yargs = require("yargs");

The yargs object has a method command which takes in an object, the object has properties such as command, describe, builder, and handler.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lweezv9q0xsot2lig8ad.png)

The command property is the argument that triggers the handler function.
The describe property explains what our command is trying to do. The builder property helps us to describe our other argument, from the image above, we have two additional argument, title and todo, and lastly the handler contains the function we want to run, in this case it is the add a new todo.

The next question is how do we run it.

To run a command in our CLI APP, we type node app.js add --title="coding" --todo="code a new app".

For this to run successfully, we need to add a yargs.parse(); at the end of the our file app.js.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bt7sfhzz124i2noe83k1.png)

We were able to trigger the handler function using the command in the image below and inside the handler function, we have a console.log(argv), which logs out the title and body, now that we have access to them we can start implementing our logic.

We need to create another file which will contain all the business logic needed for this application to work. lets name it utils.js

What do we need 
1. Create todo 
2. List todos
3. Display one todo 
4. Delete todo

We also need to store the todos somewhere, we would store them in a json file, this will make it possible for us to save and retrieve all the todos. We would also need the node fs module in order to write and read from the json file.

{% gist https://gist.github.com/Josh4324/b220ad04f14b6e3f926b3b7551c594c8 %}
we can actually abstract the code above to make it more clean but i am trying to limit the number of functions i will write for the purpose of this tutorial.

Up next, we need to create another function to List all the available todos.

{% gist https://gist.github.com/Josh4324/eb59c7a6b7b0825fe4a51c3e19af0b70
%}

Up next, we need to create a function to display a specific todo.

{% gist 
https://gist.github.com/Josh4324/f1fd695558dd458e978e99a5f2feb64d
%}

Up, next, we need to create a function to delete a todo.

{% gist 
https://gist.github.com/Josh4324/409a995cab81b1e27542cf7b06968cde
%}


We have all of our four function ready, it is time to integrate them into our CLI Application.

Step 3

In order for us to have access to our functions in app.js, we need to export our functions.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l48ipfq3dq1i3i3s51yn.png)

- Setup Yargs for create todo

{% gist 
https://gist.github.com/Josh4324/6aa0a58b8e2a0db6fc8ea4465c9c6fca
%}

The createTodo function has been added to the handler.

To test this - run node app.js add --title="code" --todo="code by night"

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k4wxwegjiwilpmrqya5a.png)


- Setup Yargs to get all todos

{% gist 
https://gist.github.com/Josh4324/f16a5293ec78acb5b6cb983e62e30c05
%}

The listTodo function has been added to the handler.

To test this - run node app.js list 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ip1ahj88z1x9v63hnkxl.png)

- Setup Yargs to get one todo

{% gist 
https://gist.github.com/Josh4324/8a974ba1438a22f7109fab7e500980f5
%}

The getOneTodo function has been added to the handler.

To test this - run node app.js read --title="code" 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dsh6pb4199jka3it5vy1.png)

- Setup Yargs to delete todo

{% gist 
https://gist.github.com/Josh4324/3383737722dc3f92cf5f632388ec908d
%}
The deleteTodo function has been added to the handler.

To test this - run node app.js delete --title="code" 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gq37igvj9et1gmn8herq.png)


How to improve the app.

1. Abstract the code, their is a lot of code repetition, extrat them and make it a function.
2. Make the console better by decorating it, instead of consoling objects, you can just console the todos.
3. Be creative.

Link to full code on github
https://github.com/Josh4324/Todo-CLI-APP-NodeJs