12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- const fs = require('fs')
- const uri = './json/mapping.json'
- const saveUri = './json/tree.json'
- const _done = () => {
- const configs = _getMappingConfig()
- const configTree = _tree(configs)
- _saveTree(configTree)
- }
- const _getMappingConfig = () => {
- const configs = JSON.parse(fs.readFileSync(uri)).data
- return configs
- }
- const _tree = (configs) => {
- const tree = {}, map = {}
- for(const config of configs) {
- const {key, pKey, type} = config
- // 根路径
- if(pKey == 0) {
- const _key = key
- const _config = {
- _config: config,
- _key,
- }
- tree[key] = _config
- map[key] = _config
- continue
- }
- const pConfig = map[pKey]
- const _key = pConfig['_key'] + '_' + key
- const _config = {
- _config: config,
- _key: _key,
- }
- pConfig[key] = _config
- if(type === 'object' || type === 'Array') {
- map[key] = _config
- continue
- }
- }
- return tree
- }
- const _saveTree = (data) => {
- fs.writeFileSync(saveUri, JSON.stringify(data))
- }
- _done()
|