-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetNamespaces.js
executable file
·48 lines (41 loc) · 1.22 KB
/
getNamespaces.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
const fs = require('fs')
const path = require('path')
const YAML = require('yaml')
const parsers = {
'.json': JSON.parse,
'.yml': YAML.parse,
'.yaml': YAML.parse
}
const getFiles = (srcpath) => {
return fs.readdirSync(srcpath).filter((file) => {
return !fs.statSync(path.join(srcpath, file)).isDirectory()
}).filter((file) => Object.keys(parsers).includes(path.extname(file))).map((file) => path.join(srcpath, file))
}
const getDirectories = (srcpath) => {
return fs.readdirSync(srcpath).filter((file) => {
return fs.statSync(path.join(srcpath, file)).isDirectory()
}).map((dir) => path.join(srcpath, dir))
}
function getAllFiles (srcpath) {
let files = getFiles(srcpath)
const dirs = getDirectories(srcpath)
dirs.forEach((dir) => {
files = files.concat(getAllFiles(dir))
})
return files
}
module.exports = (p) => {
const allFiles = getAllFiles(p)
return allFiles.map((file) => {
const parse = parsers[path.extname(file)]
const namespace = parse(fs.readFileSync(file, 'utf-8'))
const sepFile = file.split(path.sep)
const fileName = sepFile[sepFile.length - 1]
const name = path.parse(fileName).name
return {
name,
path: file,
resources: namespace
}
})
}