A Task Manager is a very useful tool to keep track of your tasks, whether it's for personal use or a work-related project. It helps you organize your activities, set priorities, and monitor progress efficiently.
Features of Task Manager
A CLI task manager that allows users to manage tasks through the terminal. The task manager will have the following features:
- Add a Task: Users can add a task by providing a description.
- List Tasks: View a list of all tasks with their status (completed or not).
- Mark a Task as Completed: Mark a specific task as completed.
- Remove a Task: Remove a task from the task list.
- Persistent Data: Save tasks to a file, so they persist even after closing the application.
Approach
Design and implement a CLI-based task manager using Node.js with file-based persistence.
- Initialize a Node.js project using npm init.
- Define a task model with properties such as id, description, and completed.
- Utilize the built-in fs module for reading and writing tasks to a file (JSON storage).
- Capture command-line arguments using process.argv for user interaction.
- Implement core operations (add, list, complete, remove) to manage tasks efficiently.
Setting Up Node.js Task Manager CLI
Initialize the project workspace and prepare the development environment.
Step 1: Create Project Directory
Open your terminal and create a new directory for the task manager project:
mkdir task-manager-cli
cd task-manager-cliStep 2: Initialize a Node.js Project
Run the following command to initialize the Node.js project:
npm init -yStep 3: Install Required Modules
- No external dependencies are required; the application uses Node.js built-in fs module.
- Optional: Install a CLI parser (e.g., yargs) for improved argument handling.
npm install yargsCurrent implementation proceeds without external libraries.
Step 4: Implementing the Task Manager
Create the following features in the index.js file:
const fs = require('fs');
const yargs = require('yargs');
const tasksFile = 'tasks.json';
// Helper function to read tasks from the file
const readTasks = () => {
try {
const dataBuffer = fs.readFileSync(tasksFile);
const dataJSON = dataBuffer.toString();
return JSON.parse(dataJSON);
} catch (error) {
return [];
}
};
// Helper function to save tasks to the file
const saveTasks = (tasks) => {
const dataJSON = JSON.stringify(tasks, null, 2);
fs.writeFileSync(tasksFile, dataJSON);
};
// Command to add a task
yargs.command({
command: 'add',
describe: 'Add a new task',
builder: {
description: {
describe: 'Task description',
demandOption: true,
type: 'string'
}
},
handler(argv) {
const tasks = readTasks();
const newTask = {
id: tasks.length + 1,
description: argv.description,
completed: false
};
tasks.push(newTask);
saveTasks(tasks);
console.log(`Task "${argv.description}" added successfully!`);
}
});
// Command to list all tasks
yargs.command({
command: 'list',
describe: 'List all tasks',
handler() {
const tasks = readTasks();
if (tasks.length === 0) {
console.log('No tasks available.');
return;
}
console.log('Task List:');
tasks.forEach((task) => {
console.log(`${task.id}. ${task.description} - ${task.completed ? 'Completed' : 'Not Completed'}`);
});
}
});
// Command to mark a task as completed
yargs.command({
command: 'complete',
describe: 'Mark a task as completed',
builder: {
id: {
describe: 'Task ID to mark as completed',
demandOption: true,
type: 'number'
}
},
handler(argv) {
const tasks = readTasks();
const task = tasks.find((task) => task.id === argv.id);
if (!task) {
console.log('Task not found!');
return;
}
task.completed = true;
saveTasks(tasks);
console.log(`Task ${argv.id} marked as completed!`);
}
});
// Command to remove a task
yargs.command({
command: 'remove',
describe: 'Remove a task',
builder: {
id: {
describe: 'Task ID to remove',
demandOption: true,
type: 'number'
}
},
handler(argv) {
const tasks = readTasks();
const updatedTasks = tasks.filter((task) => task.id !== argv.id);
if (updatedTasks.length === tasks.length) {
console.log('Task not found!');
return;
}
saveTasks(updatedTasks);
console.log(`Task ${argv.id} removed successfully!`);
}
});
// Parse command-line arguments
yargs.parse();
Running the Application
To run the application, use the following commands in your terminal:
Add a Task:
node index.js add --description "Buy groceries"List All Tasks:
node index.js listMark a Task as Completed:
node index.js complete --id 1Remove a Task:
node index.js remove --id 1
- Task File Operations: Uses fs to read and write tasks to a tasks.json file, allowing persistence of task data.
- Add Task: The add command lets users add a new task with a description, which is saved in the tasks.json file.
- List Tasks: The list command displays all tasks, showing their description and completion status.
- Complete Task: The complete command marks a specific task as completed by its ID and updates the tasks.json file.
- Remove Task: The remove command deletes a task by its ID from the task list and updates the file accordingly.