-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.js
132 lines (94 loc) · 3.72 KB
/
ai.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
import fetch from "node-fetch";
import chalk from "chalk";
import { join, dirname } from "node:path"
import { fileURLToPath } from "node:url";
import { Low } from "lowdb";
import { JSONFile } from "lowdb/node"
const __dirname = dirname(fileURLToPath(import.meta.url));
const file = join(__dirname, 'db.json');
const adapter = new JSONFile(file);
const db = new Low(adapter);
export async function addTask(qa) {
await db.read()
// If db.json doesn't exist, db.data will be null
// Use the code below to set default data
// db.data = db.data || { posts: [] } // For Node < v15.x
db.data ||= { tasks: [] } // For Node >= 15.x
db.data.tasks.push({ "id": db.data.tasks.length + 1, "name": qa, "completed": false });
// Finally write db.data content to file
await db.write()
console.log(chalk.white.italic("Task "), chalk.white.bold(qa), chalk.white.italic(" created"));
}
export async function changeTaskStatus(id, status) {
await db.read()
// If db.json doesn't exist, db.data will be null
// Use the code below to set default data
// db.data = db.data || { posts: [] } // For Node < v15.x
db.data ||= { tasks: [] } // For Node >= 15.x
for (const key in db.data) {
if (Object.hasOwnProperty.call(db.data, key)) {
const taskList = db.data[key];
for (const key in taskList) {
if (Object.hasOwnProperty.call(taskList, key)) {
const task = taskList[key];
if (task.id == id) {
db.data.tasks.splice(key, 1); // 2nd parameter means remove one item only
db.data.tasks.push({ "id": id, "name": task.name, "completed": status });
}
}
}
}
}
// Finally write db.data content to file
await db.write()
if (status == true)
console.log(chalk.white.italic("Task "), chalk.white.bold("qa"), chalk.white.italic(" completed"));
else
console.log(chalk.white.italic("Task "), chalk.white.bold("qa"), chalk.white.italic(" uncompleted"));
}
export async function deleteTask(id) {
await db.read()
// If db.json doesn't exist, db.data will be null
// Use the code below to set default data
// db.data = db.data || { posts: [] } // For Node < v15.x
db.data ||= { tasks: [] } // For Node >= 15.x
for (const key in db.data) {
if (Object.hasOwnProperty.call(db.data, key)) {
const taskList = db.data[key];
for (const key in taskList) {
if (Object.hasOwnProperty.call(taskList, key)) {
const task = taskList[key];
if (task.id == id) {
db.data.tasks.splice(key, 1); // 2nd parameter means remove one item only
}
}
}
}
}
// Finally write db.data content to file
await db.write()
console.log(chalk.white.italic("Task "), chalk.white.bold("qa"), chalk.white.italic(" deleted"));
}
export async function listTask() {
await db.read()
// If db.json doesn't exist, db.data will be null
// Use the code below to set default data
// db.data = db.data || { posts: [] } // For Node < v15.x
db.data ||= { tasks: [] } // For Node >= 15.x
console.log(chalk.white.italic("Listing ..."));
for (const key in db.data) {
if (Object.hasOwnProperty.call(db.data, key)) {
const taskList = db.data[key];
for (const key in taskList) {
if (Object.hasOwnProperty.call(taskList, key)) {
const task = taskList[key];
if (task.completed)
console.log(chalk.blue.bold.underline(task.id) + ". " + chalk.bgGreenBright.italic.blue.bold(task.name));
else
console.log(chalk.blue.bold.underline(task.id) + ". " + chalk.bgWhite.bold(task.name));
}
console.log(chalk.blue("------------------------------------------------"))
}
}
}
}