Skip to content

Commit

Permalink
feat(conf): make toml file path configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Dec 5, 2021
1 parent 644dcf1 commit f660294
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
13 changes: 8 additions & 5 deletions core/src/conf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs'
import { resolve } from 'node:path'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import * as toml from '@iarna/toml'
import fetch from 'node-fetch'
Expand All @@ -11,15 +12,17 @@ import type { SchoolConf, UsersConf } from './types/conf'
import type { SchoolEdgeCase } from './types/edge-case'
import type { StringKV } from './types/helper'

export function loadConfFromToml(): UsersConf | null {
const path = resolve('./conf.toml')
if (fs.existsSync(path)) {
const usersConf = parse(fs.readFileSync(path, 'utf8')) as UsersConf
export function loadConfFromToml(customPath?: string): UsersConf | null {
const resolvedPath = path.join(process.cwd(), customPath ?? './conf.toml')
if (fs.existsSync(resolvedPath)) {
const usersConf = parse(fs.readFileSync(resolvedPath, 'utf8')) as UsersConf
log.success({
message: '成功加载用户',
suffix: `${usersConf.users.map((u) => `@${u.alias}`).join(' ')}`,
})
return usersConf
} else {
log.error('配置文件不存在')
}
return null
}
Expand Down
49 changes: 27 additions & 22 deletions docs/config.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
## 配置文件的创建

首先我们需要在当前工作目录下创建名为 `conf.toml` 的配置文件,该配置文件将在加载后得到如下的数据结构:

```ts
type UsersConf = {
readonly notifier?: [`${number}`, string, string]
readonly users: Array<UserConfOpts>
}

type UserConfOpts = {
addr: [''] | [string, string, string]
readonly username: string
readonly password: string
readonly alias: string
readonly school: string
readonly retry?: number
readonly captcha?: 'MANUAL' | 'OCR'
readonly signedDataMonth?: `${number}-${number}`
}
```
## 配置文件的路径

Cea 默认配置文件在当前工作根目录下,并命名为 `conf.toml`

- **使用命令行加载配置**:可以通过 `cea load [path]``path` 指定配置文件地路径
- **使用代码执行签到**:无需手动配置,Cea 会自动加载默认路径下的配置文件(以防配置文件变动更新),此方式不可自定义路径

## 配置字段说明

配置文件采用如下的数据结构:

```ts
type UsersConf = {
readonly notifier?: [`${number}`, string, string]
readonly users: Array<UserConfOpts>
}

type UserConfOpts = {
addr: [''] | [string, string, string]
readonly username: string
readonly password: string
readonly alias: string
readonly school: string
readonly retry?: number
readonly captcha?: 'MANUAL' | 'OCR'
readonly signedDataMonth?: `${number}-${number}`
}
```
可以很清晰地看到,我们主要配置的是一个用户数组,每个用户有不同地配置项,下面我们就来看看各个配置项:
- `addr`: 配置签到地址,有两种可能的格式
Expand Down Expand Up @@ -82,7 +87,6 @@ school = "whpu"
+school = "whu"
```


跟上面的模式一样,`[[users]]` 表示在用户数组中添加一个 ,然后开始各字段的配置

最重要的用户组配置完毕,我们还可以选择**日志推送服务**,目前推送是基于微信的,配合 [pushplus](http://pushplus.hxtrip.com/message) 可在几秒钟内完成配置。具体操作是,去 [pushplus](http://pushplus.hxtrip.com/message) 微信登录,获取 `token`,然后修改我们的配置文件,加上 `notifier` 字段:
Expand All @@ -102,6 +106,7 @@ school = "whu"
```

除此之外,对于签到需要上传图片的情况,请确保至少成功签到过一次,然后配置好 signedDataMouth 字段:

```diff
# 填入成功历史签到中存在成功签到的月份,格式严格遵循 YYYY-MM,默认值为 2020-11,如果你在此月有成功签到记录,可以省此字段的配置
+signedDataMouth = "2021-10"
Expand Down
6 changes: 3 additions & 3 deletions internal/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const { prompt } = enquirer
break
}
case 'load': {
await confSet()
await confSet(null, argv2)
break
}

Expand All @@ -60,8 +60,8 @@ const { prompt } = enquirer
sign tasks check in
attendance
attendance tasks check in
load <path>
load config info from conf.toml path
load [path]
load config info from path, or default to \`./conf.json\`
rm <schools | users | all> remove stored config feilds
get <schools | users> display stored config feilds
`)
Expand Down
7 changes: 4 additions & 3 deletions internal/src/conf-set.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getSchoolInfos, loadConfFromToml, sstore } from '@ceajs/core'
import type { UsersConf } from '@ceajs/core'

export async function confSet(
usersConf: UsersConf | null = loadConfFromToml(),
) {
export async function confSet(usersConf?: UsersConf | null, path?: string) {
if (!usersConf) {
usersConf = loadConfFromToml(path)
}
if (usersConf) {
const { users, notifier } = usersConf
const schoolConf = await getSchoolInfos(usersConf)
Expand Down

0 comments on commit f660294

Please sign in to comment.