-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (97 loc) · 3.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env node
import { Command } from "commander";
import fs, { read } from 'fs';
import path from "path";
import { fileURLToPath } from "url";
const program = new Command();
const _filename = fileURLToPath(import.meta.url);
const _dirname = path.dirname(_filename);
const tasksFilePath = path.join(_dirname, 'tasks.json');
function readTasks(){
const data = fs.readFileSync(tasksFilePath);
return JSON.parse(data);
}
function writeTasks(tasks){
fs.writeFileSync(tasksFilePath, JSON.stringify(tasks, null, 2), "utf-8");
}
// addition of task to tasks
program
.command('add <description> [status]')
.description("addition of task to database, description is required, status is optional and must choose from {'todo', 'in-progress', 'done'}.")
.action((description, status='todo') => {
const validStatuses = ['todo', 'in-progress', 'done'];
if(!validStatuses.includes(status)){
console.error(`Invalid status. Please use one of the following: ${validStatuses.join(', ')}`);
return;
}
const tasks = readTasks();
const newTask = {
id: Date.now().toString(),
description,
status,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
}
tasks.push(newTask);
writeTasks(tasks);
console.log("task added:", newTask);
});
// command to update a task;
program
.command('update <taskId> <status>')
.description("updates the specifies task, id & status is required and status must be from {'todo', 'in-progress', 'done'}")
.action((taskId, status)=>{
const tasks = readTasks();
const task = tasks.find((task)=>task.id === taskId);
const validStatuses = ['todo', 'in-progress', 'done'];
if(!validStatuses.includes(status)){
console.error(`Invalid status. Please use one of the following: ${validStatuses.join(', ')}`);
return;
}
if(task){
task.status = status;
task.updatedAt = new Date().toISOString();
writeTasks(tasks);
console.log('Task updated:', task);
}
else{
console.log(`Task with id ${taskId} not found`);
}
});
// command to delete the task;
program
.command('delete <taskId>')
.description('deletes the task with specified Id, id is required.')
.action((taskId)=>{
let tasks = readTasks();
tasks = tasks.filter((task)=> task.id !== taskId);
writeTasks(tasks);
console.log(`Task with id ${taskId} deleted.`);
});
// command to list all the tasks
program
.command('list [status]')
.description('List all tasks')
.action((status)=>{
const tasks = readTasks();
if(!status){
console.log('Tasks:', tasks);
}
else{
const validStatuses = ['todo', 'in-progress', 'done'];
if(!validStatuses.includes(status)){
console.error(`Invalid status. Please use one of the following: ${validStatuses.join(', ')}`);
return;
}
const t = tasks.filter((task)=>task.status===status);
console.log(`Tasks ${status}:`, t);
}
})
program
.version("1.0.0")
.description("My Node CLI")
.option("-n, --name <type>", "Add your name")
.action((options) => {
console.log(`Hey, ${options.name}!`);
});
program.parse(process.argv);