index.js 1.97 KB
Newer Older
wangdanlei's avatar
wangdanlei committed
1
// import Vue from 'vue'
wangdanlei's avatar
wangdanlei committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15
let resultComps = []
const requireComponent = require.context(
  './', // 在当前目录下查找
  true, // 遍历子文件夹
  /\.vue$/ // 正则匹配 以 .vue结尾的文件
)
resultComps = handleData(requireComponent.keys())
function findKey(data) {
  const allkey = []
  const finalData = []
  const obj = {}
  data.forEach(el => {
    const comp = requireComponent(el)
    const item = comp.default
wangdanlei's avatar
wangdanlei committed
16 17 18 19 20 21 22 23 24 25 26
    if (!(item.modelRelationObjs.length === 0 || (item.modelRelationObjs.length === 1 && item.modelRelationObjs[0] === ''))) {
      item.modelRelationObjs.forEach(key => {
        if (allkey.indexOf(key) <= -1) {
          allkey.push(key)
          finalData.push({
            [key]: []
          })
          obj[key] = []
        }
      })
    }
wangdanlei's avatar
wangdanlei committed
27 28 29 30 31
  })
  return { finalData, allkey, obj }
}
function handleData(data) {
  if (!data) return
wangdanlei's avatar
wangdanlei committed
32
  const { finalData, obj } = findKey(data)
wangdanlei's avatar
wangdanlei committed
33 34 35 36
  data.forEach(el => {
    const comp = requireComponent(el)
    const item = comp.default
    const mfileName = el.split('/')[el.split('/').length - 1]
wangdanlei's avatar
wangdanlei committed
37 38 39 40
    // Vue.component(mfileName.slice(0, -4), comp.default)
    if (item.modelRelationObjs.length === 0 || (item.modelRelationObjs.length === 1 && item.modelRelationObjs[0] === '')) {
      if (!obj['commonComponent']) {
        obj['commonComponent'] = []
wangdanlei's avatar
wangdanlei committed
41
      }
wangdanlei's avatar
wangdanlei committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      obj['commonComponent'].push({
        name: mfileName.slice(0, -4),
        displayName: item.displayName,
        modelRelationObjs: item.modelRelationObjs,
        codePath: el.slice(1)
      })
    } else {
      item.modelRelationObjs.forEach(key => {
        const index = finalData.findIndex(y => y[key])
        if (index > -1) {
          finalData[index][key].push({
            name: mfileName.slice(0, -4),
            displayName: item.displayName,
            modelRelationObjs: item.modelRelationObjs,
            codePath: el.slice(1)
          })
          obj[key] = finalData[index][key]
        }
      })
    }
wangdanlei's avatar
wangdanlei committed
62 63 64 65
  })
  return obj
}
export default resultComps