feat(shell): add config svc

This commit is contained in:
Il Harper 2024-03-04 02:05:25 +08:00
parent 218efb22ef
commit c3871fbeb8
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC

View File

@ -0,0 +1,61 @@
// https://github.com/import-js/eslint-plugin-import/issues/2802
// eslint-disable-next-line import/no-unresolved
import { app } from 'electron'
import { load } from 'js-yaml'
import { mkdir, readFile, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { env } from 'node:process'
import defaultConfig from '../../../../docs/static/chronocat.yml'
import { exists } from '../../utils/fs'
import { generateToken } from '../../utils/token'
import { getAuthData } from '../authData'
import { baseDir } from '../baseDir'
import { l } from '../logger'
import { validate } from '../validate'
import type { ChronocatConfig, ChronocatCurrentConfig } from './configEntity'
let config: ChronocatCurrentConfig | undefined = undefined
const loadConfig = async () => {
const configDir = join(baseDir, 'config')
const configPath = join(configDir, 'chronocat.yml')
if (!(await exists(configPath))) {
// 配置文件不存在,生成新配置
const newConfig = defaultConfig.replaceAll(
'DEFINE_CHRONO_TOKEN',
`'${
app.commandLine.getSwitchValue('chrono-default-token') ||
env['CHRONO_DEFAULT_TOKEN'] ||
generateToken()
}'`,
)
await mkdir(configDir, {
recursive: true,
})
await writeFile(configPath, newConfig)
}
const parsedConfig = load(
await readFile(configPath, 'utf-8'),
) as ChronocatConfig
const { uin } = await getAuthData()
const validateResult = await validate('ChronocatConfig')(parsedConfig)
if (validateResult) {
l.error(new Error(`解析配置时出现问题。${validateResult}`), {
code: 2154,
throw: true,
})
}
const result = Object.assign({}, parsedConfig, parsedConfig.overrides?.[uin])
if ('overrides' in result) delete result.overrides
return result
}
export const getConfig = async () => {
if (!config) config = await loadConfig()
return config
}