-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (79 loc) · 2.84 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const fs = require("fs");
const path = require("path");
function gatherAllFiles(folderPath, selectionFilter) {
let files = [];
const fullPath = path.resolve(require.main.path, folderPath);
const isFile = (f) => {
const filePath = fullPath + "\\" + f;
return fs.lstatSync(filePath).isFile();
};
let folderContent = fs.readdirSync(fullPath);
for (const f of folderContent) {
if (isFile(f)) {
files.push(folderPath + "/" + f);
} else {
const subFolderFiles = gatherAllFiles(
folderPath + "/" + f,
selectionFilter
);
if (subFolderFiles.length > 0) files.push(subFolderFiles);
}
}
if (selectionFilter) files = files.filter(selectionFilter);
return files.flat();
}
module.exports.addRoutes = function (app, routesPath, options = {}) {
const routesFiles = gatherAllFiles(routesPath, options.selectionFilter);
const routesUrls = [];
const baseUrl = options.baseUrl ? options.baseUrl : "/";
for (const routeFile of routesFiles) {
const routeUrl = makeRouteUrl(baseUrl, routesPath, routeFile);
const route = require.main.require(routeFile);
app.use(routeUrl, route);
routesUrls.push(routeUrl);
}
return routesUrls;
};
module.exports.addMiddlewares = function (app, filesPath, options = {}) {
const middlewaresFiles = gatherAllFiles(filesPath, options.selectionFilter);
const middlewares = [];
if (options.sortFunction) middlewaresFiles.sort(options.sortFunction);
else middlewaresFiles.sort();
for (const middlewareFile of middlewaresFiles) {
const moduleExports = require.main.require(middlewareFile);
if (typeof moduleExports === "function") {
app.use(moduleExports);
middlewares.push(path.basename(middlewareFile, ".js"));
} else if (typeof moduleExports === "object") {
for (const middlewareName of Object.keys(moduleExports)) {
app.use(moduleExports[middlewareName]);
middlewares.push(middlewareName);
}
}
}
return middlewares;
};
module.exports.addTo = function (filesPath, callback, options = {}) {
const files = gatherAllFiles(filesPath, options.selectionFilter);
const output = [];
if (options.sortFunction) files.sort(options.sortFunction);
else files.sort();
for (const file of files) {
const moduleExports = require.main.require(file);
if (typeof moduleExports === "function") {
callback(moduleExports);
output.push(path.basename(file));
} else if (typeof moduleExports === "object") {
for (const key of Object.keys(moduleExports)) {
callback(moduleExports[key]);
output.push(key);
}
}
}
return output;
};
function makeRouteUrl(baseUrl, routesPath, routeFile) {
const fileName = path.basename(routeFile, ".js");
const fileUrl = path.dirname(routeFile).replace(routesPath, "");
return path.posix.join(baseUrl, fileUrl, fileName);
}