-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (39 loc) · 1.18 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
'use strict';
const fs = require('fs');
const path = require('path');
const set = require('lodash.set');
const camelCase = require('lodash.camelcase');
const defaults = require('lodash.defaults');
function processDir(modules, options) {
return function loop(_path, str) {
const files = fs.readdirSync(_path);
files.forEach((file) => {
const filePath = path.resolve(_path, file);
if (fs.statSync(filePath).isDirectory()) {
loop(filePath, `${str}.${file}`);
}
else if (~options.extensions.indexOf(path.extname(file))) {
const objectPath = `${str}.${camelCase(file.replace(/\.[^/.]+$/, ''))}`;
set(modules, objectPath, require(filePath));
}
});
}
}
function requireDir(_path, _options) {
let modules = {};
const defaultOptions = {
path: __dirname,
extensions: ['.json', '.js', '.jsx'],
};
const options = defaults(_options || {}, defaultOptions);
const dir = path.join(path.dirname(module.parent.id), _path || options.path);
try {
fs.statSync(dir);
}
catch(e) {
throw new Error('path does not exist');
}
processDir(modules, options)(dir, '');
return modules;
}
module.exports = requireDir;