Simple Task Manager CLI using Node.js

19 Jan, 2021
Before creating a simple Task Manager CLI using NodeJS, let’s run our first basic Node.js application with the following simple steps:
Note: You can download and install the Node.js application from href=”https://nodejs.org/en/”Step 1: Create a Directory for our Task Manager CLI named TODO-CLI, you can give the name of your choice.
write  :  mkdir <filename>
 Step 2: In that directory make a JavaScript file named index.js as shown below:
Make  an index.js file 
Step 3: This file contains some JavaScript code to run a program, Now we can write some sample code as shown below:
 
Step 4: After writing the code in the index.js file run this file using the following command:
node index.js
After running the command you will the following output So, now we confirm that our index.js file is running without any problem.
Step 5: Install the following modules which are used for this application using the following command:
npm install yargs
npm install lodash
loadsh: A modern JavaScript utility library that helps to work with Array, Numbers, String, Objects, and Strings.
yargs: It is an interactive command-line module that helps to read the user input. 
Step 6: Create a new file called todo.js and write down the following code in it:
Filename: todo.js
Javascript
console.log("todo.js is running !!");  const fs = require('fs');  // Add a todo item var addTodo = (title) => {    var todos = fetchTodos();    var todo = {        title    };      var duplicatetodos = todos.filter(        (todo) => todo.title === title);      if (duplicatetodos.length === 0) {        todos.push(todo);        saveTodos(todos);        return todo;    }};  // Delete a todo item var deleteTodo = (title) => {    var todos = fetchTodos();    var filteredtodos = todos.filter(        (todo) => todo.title !== title);    saveTodos(filteredtodos);      return todos.length !== filteredtodos.length;};    // Read a todo item var readTodo = (title) => {    var todos = fetchTodos();    var filteredTodos = todos.filter(        (todo) => todo.title === title);    return filteredTodos[0];};    // List all todo items var listTodos = () => {    return fetchTodos();};    // Utility functionsvar fetchTodos = () => {    try {        var todosString =             fs.readFileSync('tasks-data.json');        return JSON.parse(todosString);    } catch (e) {        return [];    }};  var saveTodos = (todos) => {    fs.writeFileSync('tasks-data.json',         JSON.stringify(todos));};  var logTodo = (todo) => {    console.log('## ---## --- ##');    console.log(`It's title is: ${todo.title}`);};  // Exporting functionmodule.exports = {    addTodo,    deleteTodo,    readTodo,    listTodos,    logTodo};
In the above file included in the utility section which handles all the NodeJS file management system like shown below: 
 readFileSync: https://www.geeksforgeeks.org/node-js-fs-readfilesync-method/
 writeFileSync: https://www.geeksforgeeks.org/node-js-fs-writefilesync-method/
 JSON.parse: https://www.geeksforgeeks.org/javascript-json-parse-method/
 JSON.stringify: https://www.geeksforgeeks.org/javascript-json-stringify-method/
Step 7: Now update our previous index.js file code with the following complete code:
Filename: index.js
Javascript
console.log("index.js is running");  const fs = require('fs');const _ = require('lodash');const yargs = require('yargs');  const tasks = require('./todo');  const argv = yargs.argv;  var command = argv._[0];console.log('Running Command is :', command);  if (command === 'addTodo') {    tasks.addTodo(argv.title);} else if (command === 'deleteTodo') {    var todoDeleted = tasks.deleteTodo(argv.title);    var message = todoDeleted ?         'Todo was deleted' : 'Todo can not found';    console.log(message);} else if (command === 'readTodo') {    var todo = tasks.readTodo(argv.title);    if (todo) {        console.log('yahoo! The todo was found.');        tasks.logTodo(todo);    } else {        console.log('Oops! The todo was not found.');    }} else if (command === 'listTodos') {    var allTodos = tasks.listTodos();    console.log(`${allTodos.length} tasks available`);    allTodos.forEach((todo) => tasks.logTodo(todo));} else {    console.log('It is a Invalid command.');}
Step 8: Following are some command for our Task Application:
1. Add Tasks: Use the following command to add tasks for our application.
node index.js addTodo --title="your tasks"  
When you add all the tasks by the use of the command line, if you look at the project folder you can see a task-data.json file is create and all the tasks are listed as JSON structure in that file as shown below:
2. Delete Tasks: Delete task is necessary when completed the tasks and you wanted to delete from your tasks list, you can delete task using the following command:
node index.js deleteTodo --title="your task"  
When you run this command, if you point out in the task-data.json you see which data you typed to delete it remove from the tasks list as shown below:
3. Read Tasks: If you wanted to check your all tasks are present or not, use the following command:
node index.js readTodo --title="your task"  
4. Display List of Tasks: If you want to see all tasks which not yet completed, then use the following command:
node index.js listTodos  
Node.js-Misc
Technical Scripter 2020
Node.js
Technical Scripter
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.