-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (97 loc) · 2.76 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
const home = process.env.Home || require('os').homedir();
const path = require('path');
const dbpath = path.join(home,'.todo')
const db = require('./db')
const inquirer = require('inquirer')
module.exports.add = async (title) => {
// 读取之前的任务
const list = await db.read(dbpath)
// 往里面添加一个title任务
list.push({title:title,done: false})
// 存储任务到文件
db.write(list,dbpath)
}
module.exports.clear = async () => {
// 重写文件 传入空数组覆盖里面的值
db.write(list=[],dbpath)
}
module.exports.showAll = async() => {
// 读取之前的任务
const list = await db.read(dbpath)
// 打印之前的任务
printTasks(list)
}
function printTasks(list) {
inquirer
.prompt({
type: 'list',
name: 'index',
message: '请选择你想操作的任务',
choices: [{name: '退出', value: '-1'}, ...list.map((task, index) => {
return {name: `${task.done ? '[x]' : '[_]'} ${index + 1} - ${task.title}`, value: index.toString()}
}), {name: '+ 创建任务', value: '-2'}]
})
.then(answer => {
const index = parseInt(answer.index)
if (index >= 0) {
// 查看以创建的任务
askForAction(list, index)
} else if (index === -2) {
// 创建任务
askForCreateTask(list)
}
})
}
function askForAction(list, index) {
const actions = {markAsUndone, markAsDone, remove, updateTitle}
inquirer.prompt({
type: 'list', name: 'action',
message: '请选择操作',
choices: [
{name: '退出', value: 'quit'},
{name: '已完成', value: 'markAsDone'},
{name: '未完成', value: 'markAsUndone'},
{name: '改标题', value: 'updateTitle'},
{name: '删除', value: 'remove'},
]
}).then(answer2 => {
const action = actions[answer2.action]
action && action(list, index)
})
}
function askForCreateTask(list) {
inquirer.prompt({
type: 'input',
name: 'title',
message: '输入任务标题'
}).then(answer => {
list.push({
title: answer.title,
done: false
})
db.write(list,dbpath)
})
}
function markAsDone(list, index) {
list[index].done = true
db.write(list,dbpath)
}
function markAsUndone(list, index) {
list[index].done = false
db.write(list,dbpath)
}
function updateTitle(list, index) {
inquirer.prompt({
type: 'input',
name: 'title',
message: '新的标题',
default: list[index].title
}).then(answer => {
list[index].title = answer.title
db.write(list,dbpath)
})
}
function remove(list, index) {
list.splice(index, 1)
db.write(list,dbpath)
}