-
Notifications
You must be signed in to change notification settings - Fork 0
/
getDeps.js
86 lines (76 loc) · 2.14 KB
/
getDeps.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// TODO: test using a regex to match after "cx()" call
const path = require('path')
const fs = require('fs')
const dirTree = require('directory-tree')
const {
getPackagePath,
sortKeys,
filterInvalidPaths,
filterInvalidKeyPaths,
flattenCircularRefs,
writeObjToFile
} = require('./utils')
/**
* Gathers import paths of Semantic UI React components from semantic-ui-react package folder.
* @returns {*} An object where the keys are Semantic UI React component
* names and the values are the corresponding
* import paths (relative to semantic-ui-react/dist/[import type]/ or
* semantic-ui-react/src/ (for es='src').
*/
function getDeps() {
const files = {}
const semanticUiReactPath = getPackagePath('semantic-ui-react')
const srcDirPath = path.resolve(semanticUiReactPath, 'src')
const searchFolders = [
'addons',
'behaviors',
'collections',
'elements',
'modules',
'views'
]
searchFolders.forEach((searchFolder) => {
const searchRoot = path.resolve(srcDirPath, searchFolder)
dirTree(searchRoot, { extensions: /\.js$/ }, (item) => {
const basename = path.basename(item.path, '.js')
// only parse files that start with an uppercase letter
if (/^[A-Z]/.test(basename[0])) {
files[basename] = item.path.substring(srcDirPath.length).replace(/\\/g, '/')
}
})
})
const f = {}
// load files & accumulate linked deps
Object.entries(files).forEach(([name, dep]) => {
const data = fs.readFileSync(path.resolve(semanticUiReactPath, 'dist/es/', dep.slice(1)), 'utf8')
const reg = /(?<=import )(\w*)(?=.*'\.)/mg
const match = data.match(reg)
const matchFilter = match && match.filter(x => x)
if (matchFilter) {
matchFilter.unshift(name) // add original file name
f[name] = matchFilter
}
})
return f
}
function getCleanedDeps() {
return (
sortKeys(
flattenCircularRefs(
filterInvalidPaths(
filterInvalidKeyPaths(
getDeps()
)
)
)
)
)
}
function writeDependencies(obj) {
writeObjToFile('dependencies.json', obj)
}
module.exports = {
getDeps,
getCleanedDeps,
writeDependencies
}