_mapping.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const fs = require('fs')
  2. const uri = './json/mapping.json'
  3. const saveUri = './json/tree.json'
  4. const _done = () => {
  5. const configs = _getMappingConfig()
  6. const configTree = _tree(configs)
  7. _saveTree(configTree)
  8. }
  9. const _getMappingConfig = () => {
  10. const configs = JSON.parse(fs.readFileSync(uri)).data
  11. return configs
  12. }
  13. const _tree = (configs) => {
  14. const tree = {}, map = {}
  15. for(const config of configs) {
  16. const {key, pKey, type} = config
  17. // 根路径
  18. if(pKey == 0) {
  19. const _key = key
  20. const _config = {
  21. _config: config,
  22. _key,
  23. }
  24. tree[key] = _config
  25. map[key] = _config
  26. continue
  27. }
  28. const pConfig = map[pKey]
  29. const _key = pConfig['_key'] + '_' + key
  30. const _config = {
  31. _config: config,
  32. _key: _key,
  33. }
  34. pConfig[key] = _config
  35. if(type === 'object' || type === 'Array') {
  36. map[key] = _config
  37. continue
  38. }
  39. }
  40. return tree
  41. }
  42. const _saveTree = (data) => {
  43. fs.writeFileSync(saveUri, JSON.stringify(data))
  44. }
  45. _done()