-
Notifications
You must be signed in to change notification settings - Fork 16
/
cli.ts
146 lines (138 loc) · 3.56 KB
/
cli.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
#!/usr/bin/env node
import { checkIn } from 'cea-check-in'
import { log, sstore, UserConfOpts, UsersConf } from 'cea-core'
import enquirer from 'enquirer'
import { confSet } from './conf-set.js'
import { UserAction } from './constants.js'
const { prompt } = enquirer
;(async () => {
const argv = process.argv[2] || ''
const argv2 = process.argv[3]
switch (argv) {
case 'user': {
const users = await promptToGetConf()
await confSet(users)
break
}
case 'rm': {
if (argv2 === 'all') {
sstore.clear()
} else {
sstore.del(argv2)
}
break
}
case 'sign': {
await checkIn()
break
}
case 'load': {
await confSet()
break
}
default: {
console.log(`
Usage: cea <command>
All Commands:
user create|delete user
school config your school info
sign campusphere check in
load load config info from conf.toml
rm remove stored config feilds
`)
break
}
}
sstore.close()
})()
async function promptToGetConf(): Promise<UsersConf | null> {
const loadedUsers = (sstore.get('users') as UsersConf) || []
const actionNaire = {
type: 'select',
name: 'actionType',
message: `用户编辑(已有用户:${
loadedUsers.reduce((s, e) => {
const userInfo = e.alias
return s + ' ' + userInfo
}, '')
})`,
choices: [
{
name: UserAction.CREATE,
value: UserAction.CREATE,
},
{
name: UserAction.DELETE,
value: UserAction.DELETE,
},
{
name: UserAction.CANCEL,
value: UserAction.CANCEL,
},
],
}
const { actionType } = (await prompt([actionNaire]).catch((_) => _)) as {
actionType: UserAction
}
switch (actionType) {
case UserAction.CREATE: {
const form = {
type: 'form',
name: 'addUser',
message: '请填写如下信息:',
choices: [
{ name: 'username', message: '用户名', initial: '1913030099' },
{ name: 'password', message: '密码', initial: '081312' },
{
name: 'alias',
message: '用户别名',
initial: 'foo',
},
{
name: 'school',
message: '学校简称',
initial: 'whu',
},
],
}
const { addUser } = (await prompt([form]).catch((_) => _)) as {
addUser: UserConfOpts
}
const list = {
type: 'list',
name: 'addr',
message: '签到地址',
initial: [''],
}
const { addr } = (await prompt([list]).catch((_) => _)) as {
addr: Array<string>
}
addUser.addr = addr
if (!loadedUsers.some((e) => e.alias === addUser.alias)) {
log.success({ message: '成功加载用户', suffix: `@${addUser.alias}` })
return Array.from(new Set([...loadedUsers, addUser]))
} else {
log.error({ message: '用户已存在' })
return null
}
}
case UserAction.DELETE: {
if (loadedUsers.length === 0) {
log.error({ message: '无用户可删除' })
return null
}
const deleteUserNaire = {
type: 'select',
name: 'deleteUser',
message: '请选择删除用户',
choices: loadedUsers.map((e) => e.alias),
}
const res = (await prompt([deleteUserNaire]).catch((_) => _)) as {
deleteUser: string
}
return [...loadedUsers.filter((val) => val.alias !== res.deleteUser)]
}
default:
return null
}
}