-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (132 loc) · 3.29 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
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
// 获取当前系统home目录
// const homedir = require('os').homedir()
// 获取当前系统home环境变量
// const home = process.env.HOME || homedir
// const fs = require('fs')
//拼路径
// const p = require('path')
// const dbPath = p.join(home, '.todo')
const inquirer = require('inquirer');
const db = require('./db.js')
module.exports.add = async (title) => {
// 读取本地任务
const list = await db.read()
// 添加任务
list.push({ title, done: false })
//存储任务到文件
await db.write(list)
}
module.exports.clear = async (title) => {
const list = await db.read()
list.push({ title, done: false })
//清空
await db.write([])
}
function quit () {
console.log('退出')
}
function markAsDone (list, index) {
list[index].done = true
db.write(list)
}
function updateTitle (list, index) {
inquirer.prompt({
type: 'input',
name: 'title',
message: '请填写新的标题',
default: list[index].title
}).then((data) => {
list[index].title = data.title
db.write(list)
})
}
function remove (list, index) {
list.splice(index, 1)
db.write(list)
}
function askAction (list, index) {
inquirer.prompt([{
type: 'list',
name: 'action',
message: '请选择操作',
choices: [
{ name: '退出', value: 'quit' },
{ name: '已完成', value: 'markAsDone' },
{ name: '未完成', value: 'markAsUnDone' },
{ name: '改标题', value: 'updateTitle' },
{ name: '删除', value: 'remove' },
]
}]).then(answers2 => {
const actions = {
quit,
markAsDone,
updateTitle,
remove,
}
actions[answers2.action] && actions[answers2.action](list, index)
// if (answers2.action) {
// actions[answers2.action](list, index)
// }
// console.log(actions[answers2.action])
})
}
function askAndAddTask (list) {
inquirer.prompt({
type: 'input',
name: 'title',
message: '请输入新的任务标题',
}).then((data) => {
list.push({
title: data.title,
done: false
})
db.write(list)
})
}
function printTask (list) {
inquirer
.prompt([
{
type: 'list',
name: 'index',
message: 'What size do you need?',
choices: [{ name: '退出', value: '-1' }, ...list.map((element, index) => {
return { name: `${element.done ? '[√]' : '[x]'} ${index + 1} - ${element.title}`, value: index.toString() }
}), { name: '+ 新建任务', value: '-2' }]
}
])
.then(answers => {
const index = parseInt(answers.index)
if (index >= 0) {
askAction(list, index)
} else if (index === -2) {
//新建任务
askAndAddTask(list)
}
});
}
module.exports.showAll = async () => {
const list = await db.read()
printTask(list)
}
// fs.readFile(dbPath, { flag: 'a+' }, (error, data) => {
// if (error) return console.log(error)
// let list
// try {
// list = JSON.parse(data.toString())
// } catch (error2) {
// list = []
// }
// console.log(list)
// const task = {
// title: title,
// done: false
// }
// list.push(task)
// const string = JSON.stringify(list)
// fs.writeFile(dbPath, string + '\n', (error3) => {
// if (error3) {
// console.log(error3)
// }
// })
// })