-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.helpers.js
67 lines (67 loc) · 1.81 KB
/
webpack.helpers.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
const fs = require('fs');
var entryFiles = []
const readDirectory = (path) => {
if (!path) {
return []
}
let children = []
fs.readdirSync(path).forEach(function (file) {
const childFile = generateFS(`${path}/${file}`, file)
children.push(childFile)
})
return children
}
const generateFS = (path = '', name) => {
if (!path) { return {} }
let updatedName = name
let isDirectory = false
if (!name) {
const splittedPath = path.split("/")
updatedName = splittedPath[splittedPath.length - 1]
}
const stat = fs.statSync(path)
if (stat.isDirectory()) {
isDirectory = true
}
let fileTree = {
name: updatedName,
path,
isDirectory,
children: isDirectory ? readDirectory(path) : []
}
return fileTree
}
const generateEntryPaths = (files = []) => {
files.forEach(file => {
if(!file.isDirectory){
entryFiles.push(file.path)
}
else {
generateEntryPaths(file.children)
}
})
return entryFiles
}
const getFiles = ( files = [], ext) => {
const splittedFilenames = files.map( file => {
const splittedpath = file.split('actions')
const fileStr = splittedpath[splittedpath.length - 1]
const path = splittedpath.filter((path , index) => {
if( index > 0 && index < (splittedpath.length - 1)){
return path
}
})
if( fileStr.lastIndexOf(ext) === fileStr.length - ext.length){
return path + fileStr
} else {
return ""
}
})
const filteredfiles = splittedFilenames.filter(file => (file.length > 0))
return filteredfiles
}
module.exports = {
generateFS : generateFS,
generateEntryPaths : generateEntryPaths,
getFiles: getFiles
}