1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// import Vue from 'vue'
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
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] = []
}
})
}
})
return { finalData, allkey, obj }
}
function handleData(data) {
if (!data) return
const { finalData, obj } = findKey(data)
data.forEach(el => {
const comp = requireComponent(el)
const item = comp.default
const mfileName = el.split('/')[el.split('/').length - 1]
// 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'] = []
}
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]
}
})
}
})
return obj
}
export default resultComps