forked from SUI-Components/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
70 lines (61 loc) · 1.73 KB
/
config.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
const path = require('path')
const readdirSync = require('fs').readdirSync
const statSync = require('fs').statSync
const basePath = process.cwd()
const packageConfig = require(path.join(basePath, 'package.json')).config
function getOrDefault(key, defaultValue) {
return (
(packageConfig &&
packageConfig['sui-mono'] &&
packageConfig['sui-mono'][key]) ||
defaultValue
)
}
const packagesFolder = getOrDefault('packagesFolder', 'src')
const deepLevel = getOrDefault('deepLevel', 1)
const customScopes = getOrDefault('customScopes', [])
const rootPath = path.join(basePath, packagesFolder)
module.exports = {
getScopes: function() {
const folders = cwds(rootPath, deepLevel)
const scopes = folders.map(folder => {
const reversedPath = folder.split(path.sep)
const scope = Array.apply(null, Array(deepLevel)).map(
Number.prototype.valueOf,
0
)
return scope
.map(() => reversedPath.pop())
.reverse()
.join(path.sep)
})
return flatten(scopes, customScopes)
},
getPackagesFolder: function() {
return packagesFolder
},
hasRootFiles: () =>
Boolean(
readdirSync(rootPath)
.map(file => path.join(rootPath, file))
.find(filePath => statSync(filePath).isFile())
)
}
const getFolders = dir =>
readdirSync(dir)
.map(file => path.join(dir, file))
.filter(onlyFolders)
const onlyFolders = filePath => statSync(filePath).isDirectory()
const flatten = (x, y) => x.concat(y)
const cwds = (rootDir, deep) => {
const baseFolders = Array.apply(null, Array(deep)).map(
Number.prototype.valueOf,
0
)
return baseFolders.reduce(
acc => {
return acc.map(getFolders).reduce(flatten, [])
},
[rootDir]
)
}