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

Commit

Permalink
feat(cli): add cea as a cli pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed May 24, 2021
1 parent 20a5791 commit 3d56f33
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 240 deletions.
49 changes: 49 additions & 0 deletions cea/package-lock.json

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

19 changes: 19 additions & 0 deletions cea/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "cea",
"version": "0.0.1",
"description": "cea",
"main": "lib/src/index.js",
"types": "lib/src/index.d.ts",
"files": [
"lib/src/**/*.[tj]s"
],
"scripts": {},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cea-core": "^0.1.1",
"enquirer": "^2.3.6"
},
"devDependencies": {}
}
129 changes: 129 additions & 0 deletions cea/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/env node
import {
sstore,
UsersConf,
UserConfOpts,
getSchoolInfos,
loadConfFromToml,
} from 'cea-core'
import { UserAction } from './constants'
import { prompt } from 'enquirer'
;(async () => {
const argv = process.argv[2] || ''
const argv2 = process.argv[3]

switch (argv) {
case 'user': {
await promptToGetConf()
break
}
case 'rm': {
if (argv2 === 'all') {
sstore.clear()
} else {
sstore.del(argv2)
}
break
}
case 'sign': {
break
}
case 'load': {
loadConfFromToml()
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()
})()

export async function promptToGetConf(): Promise<void> {
const loadedUsers = sstore.get('users') as UsersConf

const actionNaire = {
type: 'list',
name: 'actionType',
message: `用户编辑(已有用户:${loadedUsers.reduce((s, e) => {
const userInfo = e.alias
return s + ' ' + userInfo
}, '')})`,
choices: [
{
name: UserAction.CREATE,
},
{
name: UserAction.DELETE,
},
{
name: UserAction.CANCEL,
},
],
}
const { actionType } = (await prompt([actionNaire])) as {
actionType: UserAction
}

switch (actionType) {
case UserAction.CREATE: {
const form = {
type: 'form',
name: 'addUser',
message: 'Please provide the following information:',
choices: [
{ name: 'username', message: '请输入用户名', initial: '1913030099' },
{ name: 'password', message: '请输入密码', initial: '081312' },
{
name: 'alias',
message: '请输入用户别名',
initial: 'foo',
},
{
name: 'school',
message:
'学校的英文简称(推荐,仅部分学校支持使用简称)\n请参阅 https://github.com/beetcb/cea/blob/master/docs/abbrList.sh 自行判断\n或中文全称(备用选项,所有学校均支持):',
initial: 'whu',
},
{
name: 'addr',
message:
'签到地址数组,留空使用学校地址签到,自定义签到地址:经度, 纬度, 中文地址',
initial: '',
},
],
}
const { addUser } = (await prompt([form])) as { addUser: UserConfOpts }
sstore.set('users', [...loadedUsers, addUser])
break
}
case UserAction.DELETE: {
const deleteUserNaire = {
type: 'select',
name: 'deleteUser',
message: '请选择删除用户',
choices: loadedUsers.map((e) => e.alias),
}
const res = (await prompt([deleteUserNaire])) as { deleteUser: string }
sstore.set('users', [
...loadedUsers.filter((val) => val.alias !== res.deleteUser),
])
break
}
case UserAction.CANCEL:
return
}
const schools = await getSchoolInfos(sstore.get('users'))
sstore.set('schools', schools!)
}
5 changes: 5 additions & 0 deletions cea/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum UserAction {
CREATE = '添加用户',
DELETE = '删除用户',
CANCEL = '取消',
}
6 changes: 6 additions & 0 deletions cea/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.base",
"compilerOptions": {
"outDir": "lib/"
}
}
2 changes: 0 additions & 2 deletions core/src/crawler/capcha.d.ts

This file was deleted.

64 changes: 0 additions & 64 deletions core/src/crawler/capcha.js

This file was deleted.

1 change: 0 additions & 1 deletion core/src/crawler/capcha.js.map

This file was deleted.

4 changes: 0 additions & 4 deletions core/src/utils/cookie-helper.d.ts

This file was deleted.

44 changes: 0 additions & 44 deletions core/src/utils/cookie-helper.js

This file was deleted.

1 change: 0 additions & 1 deletion core/src/utils/cookie-helper.js.map

This file was deleted.

15 changes: 0 additions & 15 deletions core/src/utils/fetch-helper.d.ts

This file was deleted.

Loading

0 comments on commit 3d56f33

Please sign in to comment.