-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
156 lines (140 loc) · 3.42 KB
/
index.ts
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
// * 引入 Database
import db from './db';
import consola from 'consola';
import inquirer from 'inquirer';
interface Task {
title: string;
done: boolean;
}
const getAllTasks = async (list: Task[]) => {
try {
const { index } = await inquirer.prompt({
// Question object
type: 'list',
name: 'index',
message: 'What do you want to do?',
choices: [
{ name: '+ Add', value: -2 },
{ name: '↵ Quit', value: -1 },
...list.map((task, index) => {
return {
name: `${task.done ? '[✓]' : '[_]'} ${index + 1} - ${task.title}`, // Display in the list
value: index, // Save in the answers hash
};
}),
],
});
if (index >= 0) {
chooseTask(list, index);
} else if (index === -2) {
createTask();
}
} catch (e) {
consola.error(e);
}
};
const chooseTask = async (list: Task[], index: number) => {
try {
const { action } = await inquirer.prompt({
type: 'list',
name: 'action',
message: 'What do you want to do?',
choices: [
{ name: '↵ Quit', value: 'quit' },
{ name: 'x Remove', value: 'removeTask' },
{ name: '- Title', value: 'updateTitle' },
{ name: '✓ Completed', value: 'markAsCompleted' },
{ name: '_ Uncompleted', value: 'markAsUncompleted' },
],
});
const actions = {
removeTask,
updateTitle,
markAsCompleted,
markAsUncompleted,
};
actions[action] && actions[action](list, index);
} catch (e) {
consola.error(e);
}
};
const createTask = async () => {
try {
const { task: newTask } = await inquirer.prompt({
type: 'input',
name: 'task',
message: "What's your new task?",
});
add(newTask);
} catch (e) {
consola.error(e);
}
};
const removeTask = async (list: Task[], index: number) => {
try {
list.splice(index, 1);
await db.write({ list });
} catch (e) {
consola.error(e);
}
};
const updateTitle = async (list: Task[], index: number) => {
try {
const { title: newTitle } = await inquirer.prompt({
type: 'input',
name: 'title',
message: "What's your new title?",
default() {
return list[index].title;
},
});
list[index].title = newTitle;
await db.write({ list });
} catch (e) {
consola.error(e);
}
};
const markAsCompleted = async (list: Task[], index: number) => {
try {
list[index].done = true;
await db.write({ list });
} catch (e) {
consola.error(e);
}
};
const markAsUncompleted = async (list: Task[], index: number) => {
try {
list[index].done = false;
await db.write({ list });
} catch (e) {
consola.error(e);
}
};
const add = async (title: string) => {
try {
await db.exists(); // * 确认文件是否存在
const { list } = await db.read(); // * 读取之前的任务
const task = { title, done: false };
list.push(task); // * 往里面添加一个 task
await db.write({ list }); // * 存储任务到文件
} catch (e) {
consola.error(e);
}
};
const clear = async () => {
try {
await db.write({ list: [] }); // Clear the list array
} catch (e) {
consola.error(e);
}
};
const showAll = async () => {
try {
await db.exists(); // * 确认文件是否存在
const { list } = await db.read();
getAllTasks(list);
} catch (e) {
consola.error(e);
}
};
export default { add, clear, showAll };