-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
86 lines (70 loc) · 1.98 KB
/
cli.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
const fs = require('fs')
const { Command } = require('commander');
const program = new Command();
const file = './todo.json'
function loadTodos(){
if (!fs.existsSync(file)){
fs.writeFileSync(file, JSON.stringify([]));
return []
}
return JSON.parse(fs.readFileSync(file, 'utf8'))
}
function saveTodos(todos){
fs.writeFileSync(file, JSON.stringify(todos, null, 2), 'utf8')
}
program
.name('todo')
.description('made a todo usng cli')
.version('0.1.0');
program
.command('add <task> ')
.description('add items to the todo')
.action((task) => {
const todos = loadTodos();
todos.push({task, done: false})
saveTodos(todos)
console.log(`✅Added: ${task}`)
})
program
.command('list')
.description('List the all the tasks')
.action(() => {
const todos = loadTodos();
if (todos.length === 0) {
console.log("😩Kuch to krlo make it productive")
}
else {
console.log("📃Todos: ")
todos.forEach((todo, index) => {
const status = todo.done ? "[✔]" : "[ ]";
console.log(`${index}. ${status} ${todo.task}`);
})
}
})
program
.command('done <index>')
.description("mark it as done")
.action((index) => {
const todos = loadTodos()
if (index < 0 || index >= todos.length){
console.log("❌Todo doesnt exist baby")
return;
}
todos[index].done = true;
saveTodos(todos)
console.log(`🌞You completed a task sunshine, ${todos[index].task} `)
})
program
.command('delete <index>')
.description('delete the todo')
.action((index) => {
const todos = loadTodos()
if (index < 0 || index >= todos.length){
console.log("❌Todo doesnt exist baby")
return;
}
const remove = todos.splice(index, 1)
saveTodos(todos)
console.log(`🗑️ Bhaad mei gya ${remove[0].task}`)
})
program.parse(process.argv)