This repository was archived by the owner on Mar 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (56 loc) · 1.97 KB
/
Copy pathindex.js
File metadata and controls
61 lines (56 loc) · 1.97 KB
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
var fs = require('fs'), path = require('path')
async function getFilesAsync(path = "./") {
const entries = await fs.promises.readdir(path, { withFileTypes: true });
const files = entries
.filter(file => !file.isDirectory())
.map(file => ({ name: file.name, path: path + file.name }));
const folders = entries.filter(folder => folder.isDirectory());
for (const folder of folders) {
files.push(...await getFilesAsync(`${path}${folder.name}/`));
}
return files
}
function getDirectoryTree(folderPath) {
var stats = fs.lstatSync(folderPath),
info = {
path: folderPath,
name: path.basename(folderPath)
};
if (stats.isDirectory()) {
info.type = "folder";
info.children = fs.readdirSync(folderPath).map(function(child) {
return getDirectoryTree(folderPath + '/' + child);
});
} else {
info.type = "file";
}
return info;
}
function requireAuto(folderPath) {
var info = {}
if (fs.lstatSync(folderPath).isDirectory()) {
Object.values(fs.readdirSync(folderPath)).forEach(child => {
var key = child.includes('.') ? child.split('.')[0] : child
info[key] = requireAuto(folderPath + '/' + child)
})
} else {
info = require(folderPath)
}
return info;
}
// Example:
// Just console.log around, it's simple as it looks
// Promise.resolve(getFilesAsync('./folder1/')).then(result => console.log(result))
// console.log(getDirectoryTree('./folder1/'))
// console.log(requireAuto('./folder1/'))
// Using ES6 module
var something = {}
const requireModule = require.context('./', true, /\.js$/)
requireModule.keys().forEach(requireModule)
requireModule.keys().forEach((fileName) => {
if (!fileName.includes('index.js')) {
const temp = fileName.replace(/(\.\/|\.js$)/g, '').split('/')
const moduleName = temp.length > 1 ? temp[temp.length - 2] : temp[0]
something[moduleName] = requireModule(fileName).default
}
})