-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
83 lines (72 loc) · 2.06 KB
/
app.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
const chalk = require('chalk');
const yargs = require('yargs')
.command('create', 'Create New Todo', {
title: {
description: 'Todo Title',
type: 'string',
demandOption: true,
},
body: {
description: 'Todo Body',
type: 'string',
demandOption: true,
},
})
.command('list', 'List All Todos')
.command('show', 'List Single Todo By Index', {
index: {
description: 'Todo Index',
type: 'string',
demandOption: true,
}
})
.command('remove', 'Remove Single Todo By Index', {
index: {
description: 'Todo Index',
type: 'string',
demandOption: true,
}
})
.command('toggle', 'Toggle Single Todo Completed Status By Index', {
index: {
description: 'Todo Index',
type: 'string',
demandOption: true,
}
})
.demandCommand(1)
.argv;
console.log(`\n=== ${chalk.cyanBright('Todo App Lunched')} ===\n`);
const Todo = require('./todo');
// Get Command
const command = yargs._[0];
// If Command is Create
if (command === 'create') {
// Create Todo
Todo.create(yargs.title, yargs.body);
console.log(chalk.greenBright('Todo Created'));
}
if(command === 'list') {
// Fetch All Data
const db = Todo.list();
// Print All Data
db.forEach((todo, index) => {
let completeMessage = todo.completed ? 'Completed' : 'Not Completed';
let colorMethod = todo.completed ? 'greenBright' : 'redBright';
console.log(chalk[colorMethod](`[${index}]: ${todo.title} (${completeMessage})`))
});
}
if (command === 'show') {
// Print Todo
console.log(Todo.show(yargs.index));
}
if (command === 'remove') {
// Remove Todo
Todo.remove(yargs.index)
console.log(chalk.greenBright('Todo Deleted'));
}
if (command === 'toggle') {
// Toggle Todo Completed Status
Todo.toggle(yargs.index);
console.log(chalk.greenBright('Todo Completed Status Updated'));
}