-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.helpers.js
82 lines (75 loc) · 1.89 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const dotenv = require("dotenv");
const fs = require("fs");
const entryFiles = [];
const readDirectory = (path) => {
if (!path) {
return [];
}
const children = [];
fs.readdirSync(path).forEach((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;
}
const 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;
}
});
const regex = new RegExp(`\\${ext}`, "g");
if (fileStr.search(regex) > 0) {
return path + fileStr;
}
return "";
});
const filteredfiles = splittedFilenames.filter((file) => file.length > 0);
return filteredfiles;
};
const env = dotenv.config().parsed ? dotenv.config().parsed : {};
const envKeys = Object.keys(env).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(env[next]);
return prev;
}, {});
module.exports = {
generateFS,
generateEntryPaths,
getFiles,
envKeys,
};