-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.js
More file actions
115 lines (112 loc) · 2.56 KB
/
api.js
File metadata and controls
115 lines (112 loc) · 2.56 KB
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
const path = require('path')
const inquirer = require('inquirer')
const io = require('./io')
const homedir = require('os').homedir()
const home = process.env.HOME || homedir
const filePath = path.join(home, '.todo')
const add = async (command) => {
const result = await io.read(filePath)
const newContent = io.add(command, result)
const wirteStatus = await io.write(filePath, newContent)
}
const clear = () => {
io.write(filePath, [])
}
const showAll = async () => {
const result = await io.read(filePath)
inquirer
.prompt([
{
type: 'list',
name: 'index',
message: 'What do you want to do?',
choices: [{value: '-2', name: '退出'}, ...result.map((task, index) => ({
name: `[${task.done ? '_' : 'X'}] ${index}.${task.title}`,
value: index.toString()
})), {name: '+ 添加任务', value: '-1'}]
}
])
.then(answer => {
handleTask(parseInt(answer.index))
});
}
const handleTask = async (index) => {
const result = await io.read(filePath)
const childChoice = [{
value: 'quit',
name: '退出'
}, {
value: 'markAsDone',
name: '已完成'
},{
value: 'markAsUnDone',
name: '未完成'
},{
value: 'modifyDetail',
name: '更改任务'
},{
value: 'delete',
name: '删除',
}]
if (index > -1) {
inquirer
.prompt([
{
type: 'list',
name: 'action',
message: 'What do you want to do?',
choices: childChoice
}
])
.then(answer => {
handleTaskAction(answer.action, index)
});
} else if (index === -1) {
inquirer.prompt({
type: 'input',
name: 'title',
message: '新的标题'
}).then(answers => {
result.push({
title: answers.title,
done: false
})
io.write(filePath, result)
})
}
}
const handleTaskAction = async (action, index) => {
const result = await io.read(filePath)
switch (action) {
case 'quit':
break
case 'markAsDone':
result[index].done = true
io.write(filePath, result)
break
case 'markAsUnDone':
result[index].done = false
io.write(filePath, result)
break
case 'modifyDetail':
inquirer.prompt({
type: 'input',
name: 'title',
message: '新的标题',
default: result[index].title
}).then(answers => {
result[index].title = answers.title
io.write(filePath, result)
})
break
case 'delete':
result.splice(index, 1)
io.write(filePath, result)
break
}
}
module.exports = {
add,
clear,
showAll
}