Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
fix(cli): error message
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed May 25, 2021
1 parent 3d56f33 commit 17426a8
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 137 deletions.
2 changes: 0 additions & 2 deletions .vscode/settings.json

This file was deleted.

129 changes: 0 additions & 129 deletions cea/src/cli.ts

This file was deleted.

2 changes: 1 addition & 1 deletion core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions core/src/conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export function loadConfFromToml(): UsersConf | null {
const path = resolve('./conf.toml')
if (fs.existsSync(path)) {
const usersConf = parse(fs.readFileSync(path, 'utf8'))!.users as UsersConf
log.success({
message: '成功加载用户',
suffix: `${usersConf.map((u) => `@${u.alias}`).join(' ')}`,
})
return usersConf
}
return null
Expand Down
1 change: 1 addition & 0 deletions cea/package-lock.json → internal/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cea/package.json → internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"cea-check-in": "^0.0.1",
"cea-core": "^0.1.1",
"enquirer": "^2.3.6"
},
"devDependencies": {}
}
}
159 changes: 159 additions & 0 deletions internal/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/usr/bin/env node
import {
log,
sstore,
UsersConf,
UserConfOpts,
getSchoolInfos,
loadConfFromToml,
} from 'cea-core'
import { checkIn } from 'cea-check-in'
import { prompt } from 'enquirer'
import { UserAction } from './constants'
;(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': {
const users = loadConfFromToml()
await confSet(users)
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 confSet(users: UsersConf | null) {
if (users) {
const schoolInfos = await getSchoolInfos(users)
if (schoolInfos) {
sstore.set('schools', schoolInfos)
}
sstore.set('users', users)
}
}

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
}
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"packages": ["core", "cea", "plugins/*"],
"packages": ["core", "internal", "plugins/*"],
"version": "independent"
}
3 changes: 2 additions & 1 deletion plugins/check-in/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
{
"path": "./core/"
}
},
{ "path": "./internal" }
]
}

0 comments on commit 17426a8

Please sign in to comment.