-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportModules.js
48 lines (38 loc) · 1 KB
/
importModules.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
import config from '../config';
import { readdirSync } from 'fs';
import path from 'path';
// Prevent caching of this module so module.parent is always accurate
const parentFile = module.parent.filename;
export default function importModules(dir, opts) {
dir = path.resolve(config.baseDir, dir || '');
opts = Object.assign({ camelize: true }, opts);
let files;
try {
files = readdirSync(dir);
}
catch (err) {
return {};
}
const done = new Set();
const ret = {};
// Adhere to the Node.js require algorithm by trying each extension in order
for (const file of files) {
const stem = path.basename(file).replace(/\.\w+$/, '');
const fullPath = path.join(dir, file);
if (
done.has(stem) ||
fullPath === parentFile ||
path.extname(file) !== '.js' ||
stem[0] === '_' ||
stem[0] === '.'
) {
continue;
}
const exportKey = opts.camelize ?
stem.replace(/-(\w)/g, (m, p1) => p1.toUpperCase()) :
stem;
ret[exportKey] = require(fullPath);
done.add(stem);
}
return ret;
}