-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
170 lines (150 loc) · 4.14 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
const { Command } = require("commander");
const program = new Command();
const fs = require("fs");
const path = "./data.json";
function base() {
// Check if the file exists
if (fs.existsSync(path)) {
// File exists, read and parse it
const data = fs.readFileSync(path, "utf8");
let jsonData;
try {
jsonData = JSON.parse(data);
} catch (error) {
console.error("Error parsing JSON:", error);
jsonData = []; // Fallback to an empty object
}
return jsonData;
} else {
// File does not exist, create an empty JSON object
fs.writeFileSync(path, "[]");
return [];
}
}
function add(taskName) {
const now = new Date().toLocaleString();
const jsonData = base();
const len = jsonData.length + 1;
const task = {
id: len,
description: taskName,
status: "todo",
createdAt: now,
updatedAt: now,
};
try {
jsonData.push(task);
fs.writeFileSync(path, JSON.stringify(jsonData, null, 2), "utf8");
console.log(`Task added successfully (ID: ${task.id})`);
} catch (error) {
console.log(error);
}
}
function update(id, taskName) {
const tasks = base();
const task = tasks.find((obj) => obj.id === Number(id));
if (task) {
task.description = taskName;
const now = new Date().toLocaleString();
task.updatedAt = now;
fs.writeFileSync(path, JSON.stringify(tasks, null, 2), "utf8");
console.log("Object found and updated:", task);
} else {
console.log("Task with the specified id not found.");
}
}
function deleteTask(id) {
const tasks = base();
const task = tasks.find((obj) => obj.id === Number(id));
if (task) {
const newTasks = tasks.filter((obj) => obj.id !== Number(id));
fs.writeFileSync(path, JSON.stringify(newTasks, null, 2), "utf8");
console.log("Object found and deleted:");
} else {
console.log("Task with the specified id not found.");
}
}
function markInProgress(id) {
const tasks = base();
const task = tasks.find((obj) => obj.id === Number(id));
if (task) {
task.status = "in-progress";
const now = new Date().toLocaleString();
task.updatedAt = now;
fs.writeFileSync(path, JSON.stringify(tasks, null, 2), "utf8");
console.log("Object found and marked in-progress:", task);
} else {
console.log("Task with the specified id not found.");
}
}
function markDone(id) {
const tasks = base();
const task = tasks.find((obj) => obj.id === Number(id));
if (task) {
task.status = "done";
const now = new Date().toLocaleString();
task.updatedAt = now;
fs.writeFileSync(path, JSON.stringify(tasks, null, 2), "utf8");
console.log("Object found and marked Done:", task);
} else {
console.log("Task with the specified id not found.");
}
}
function list(status) {
if (!status) {
const tasks = base();
console.log(tasks);
} else {
const tasks = base().filter((task) => task.status === status);
console.log(tasks);
}
}
program
.name("task-cli")
.description("A simple CLI tool in Node.js")
.version("1.0.0");
// Define the "add" command
program
.command("add <taskName>")
.description("Add task")
.action((taskName) => {
add(taskName);
});
// Define the "update" command
program
.command("update <id> <taskName>")
.description("Update task")
.action((id, taskName) => {
update(id, taskName);
});
// Define the "delete" command
program
.command("delete <id>")
.description("Delete task")
.action((id) => {
deleteTask(id);
});
// Define the "mark-in-progress" command
program
.command("mark-in-progress <id>")
.description("Mark task in progress")
.action((id) => {
markInProgress(id);
});
// Define the "mark-done" command
program
.command("mark-done <id>")
.description("Mark task done")
.action((id) => {
markDone(id);
});
// Define the "list" commands
program
.command("list [status]")
.description("List tasks that done")
.action((status) => {
list(status);
});
// Parse the arguments
program.parse(process.argv);