miao-plugin/components/Cfg.js

53 lines
1.1 KiB
JavaScript
Raw Normal View History

import fs from 'fs'
import lodash from 'lodash'
import cfgData from './cfg-lib/cfg-data.js'
2022-04-09 21:33:21 +00:00
const _path = process.cwd()
const _cfgPath = `${_path}/plugins/miao-plugin/components/`
let cfg = {}
2022-04-09 21:33:21 +00:00
try {
if (fs.existsSync(_cfgPath + 'cfg.json')) {
cfg = await cfgData.loadOldData()
cfgData.saveCfg(cfg)
fs.unlinkSync(_cfgPath + 'cfg.json')
} else {
cfg = await cfgData.getCfg()
cfgData.saveCfg(cfg)
2022-04-09 21:33:21 +00:00
}
cfg = await cfgData.getCfg()
2022-04-09 21:33:21 +00:00
} catch (e) {
// do nth
}
let Cfg = {
get (rote) {
return lodash.get(cfg, rote)
2022-04-09 21:33:21 +00:00
},
set (rote, val) {
cfg[rote] = val
cfgData.saveCfg(cfg)
2022-04-09 21:33:21 +00:00
},
del (rote) {
lodash.set(cfg, rote, undefined)
fs.writeFileSync(_cfgPath + 'cfg.json', JSON.stringify(cfg, null, '\t'))
},
getCfg () {
return cfg
},
getCfgSchema () {
return cfgData.getCfgSchema()
},
getCfgSchemaMap () {
return cfgData.getCfgSchemaMap()
},
scale (pct = 1) {
let scale = Cfg.get('renderScale', 100)
scale = Math.min(2, Math.max(0.5, scale / 100))
pct = pct * scale
return `style=transform:scale(${pct})`
2022-04-09 21:33:21 +00:00
}
}
2022-04-09 21:33:21 +00:00
export default Cfg