-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
95 lines (92 loc) · 3.46 KB
/
index.ts
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
const path = require('path');
const fs = require('fs');
const __ = path.sep;
const regDir = new RegExp(`(${__}(?:[^${__}]+${__}?)+)(?=${__}[^${__}]+)`, 'g'); // get the root path
import _ from 'lodash';
import {DirectoryDeepModule, DirectoryModule, LayerModule, RequireDirectoryOpts} from './global';
function getFilePrefix (filename: string): string {
let matchRes = filename.match(/.*(?=\.[^\.]+)/);
if (matchRes && matchRes.length) {
return matchRes[0]
}
return '';
}
function initFilePath (baseDir: string, deepModule: DirectoryDeepModule, LayerModule: LayerModule, prefix: string, opts?: RequireDirectoryOpts) {
/* prefix: the base string of directory */
let dir: string[] = fs.readdirSync(baseDir);
let pro:Array<Promise<boolean>> = [];
dir.forEach(async (item: string) => {
let targetPath: string = path.resolve(baseDir, item);
let stat = fs.statSync(targetPath)
let pro = [], filenamePrefix = getFilePrefix(item);
let layerPrefix = prefix;
if (stat.isDirectory()) {
deepModule[item] = {};
if (layerPrefix.length) {
layerPrefix += `${__}${item}`;
} else {
layerPrefix += item;
}
if (opts?.recurse) {
initFilePath(targetPath, deepModule[item], LayerModule, layerPrefix);
}
} else {
// filter the default file
if (filenamePrefix !== 'index') {
layerPrefix += `${__}${filenamePrefix}`;
pro.push(
new Promise((resolve, reject) => {
try{
let module = require(targetPath);
deepModule[filenamePrefix] = module;
LayerModule[layerPrefix] = module;
resolve(true);
} catch(err) {
reject(err)
}
})
)
}
}
})
return Promise.all(pro)
}
async function makeModule (rootDir: string, opts?: RequireDirectoryOpts) {
let esModule = {deepModule: {}, layerModule: {}};
await initFilePath(rootDir, esModule.deepModule, esModule.layerModule, rootDir, opts)
return esModule;
}
async function requireDirectoryAsync (module: NodeModule, opts?: RequireDirectoryOpts): Promise<DirectoryModule> {
let directory = module.filename.match(regDir);
let resultModule: DirectoryModule, emptyModule = {
deepModule: {},
layerModule: {}
};
let defaultOpts = {
recurse: true
}
opts = _.merge(defaultOpts, opts);
if (directory && directory[0]) {
resultModule = await makeModule(directory[0], opts);
} else {
resultModule = emptyModule;
}
if (opts) {
if (opts.addPath && opts.addPath.length) {
await opts.addPath.forEach(async (item: string) => {
let addModule: DirectoryModule;
if (directory && directory[0]) {
addModule = await makeModule(path.resolve(directory[0], item), opts);
} else {
addModule = emptyModule;
}
resultModule = _.merge(resultModule, addModule);
})
}
if (opts.filter && typeof opts.filter === 'function') {
return opts.filter(resultModule);
}
}
return resultModule;
}
export default requireDirectoryAsync;