2022-11-12 21:35:46 +00:00
|
|
|
import { cfgSchema } from '../../config/system/cfg_system.js'
|
|
|
|
import lodash from 'lodash'
|
|
|
|
import { Data } from '../index.js'
|
|
|
|
import fs from 'node:fs'
|
2024-05-21 18:05:37 +00:00
|
|
|
import { miaoPath } from '#miao.path'
|
2022-11-12 21:35:46 +00:00
|
|
|
|
|
|
|
let cfgData = {
|
|
|
|
saveCfg (cfg) {
|
|
|
|
let ret = []
|
|
|
|
lodash.forEach(cfgSchema, (cfgGroup) => {
|
|
|
|
ret.push(`/** ************ 【${cfgGroup.title}】 ************* */`)
|
|
|
|
lodash.forEach(cfgGroup.cfg, (cfgItem, cfgKey) => {
|
|
|
|
ret.push(`// ${cfgItem.desc || cfgItem.title}`)
|
|
|
|
let val = Data.def(cfg[cfgKey], cfgItem.def)
|
|
|
|
if (cfgItem.input) {
|
|
|
|
val = cfgItem.input(val)
|
|
|
|
}
|
2023-11-27 20:18:06 +00:00
|
|
|
if (cfgItem.type === 'str') {
|
|
|
|
ret.push(`export const ${cfgKey} = '${val.toString()}'`, '')
|
|
|
|
} else {
|
|
|
|
ret.push(`export const ${cfgKey} = ${val.toString()}`, '')
|
|
|
|
}
|
2022-11-12 21:35:46 +00:00
|
|
|
})
|
|
|
|
})
|
2024-05-21 18:05:37 +00:00
|
|
|
fs.writeFileSync(`${miaoPath}/config/cfg.js`, ret.join('\n'), 'utf8')
|
2022-11-12 21:35:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async getCfg () {
|
2023-03-07 17:52:11 +00:00
|
|
|
let ret = lodash.toPlainObject(await Data.importModule('/config/cfg.js', 'miao'))
|
2023-02-06 18:08:12 +00:00
|
|
|
lodash.forEach(cfgSchema, (cfgGroup) => {
|
|
|
|
lodash.forEach(cfgGroup.cfg, (cfgItem, cfgKey) => {
|
|
|
|
ret[cfgKey] = Data.def(ret[cfgKey], cfgItem.def)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return ret
|
2022-11-12 21:35:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getCfgSchemaMap () {
|
|
|
|
let ret = {}
|
|
|
|
lodash.forEach(cfgSchema, (cfgGroup) => {
|
|
|
|
lodash.forEach(cfgGroup.cfg, (cfgItem, cfgKey) => {
|
|
|
|
ret[cfgItem.key] = cfgItem
|
|
|
|
cfgItem.cfgKey = cfgKey
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return ret
|
|
|
|
},
|
|
|
|
getCfgSchema () {
|
|
|
|
return cfgSchema
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export default cfgData
|