-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
89 lines (76 loc) · 1.92 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
var fs = require('fs');
var path = require('path');
var indeks = module.exports = {};
function isArray(val) {
return (typeof val === 'object') && val.length;
}
/**
* Given a filename in the standard lower_snake_cased.ext format,
* return a mangled name in lowerCamelCase.
*/
indeks.getVarName = function(file) {
if(typeof file !== 'string') {
return false;
}
// Chops off extension(s).
file = file.split('.')[0];
// Splits name into words.
var words = file.split(/[_-]/);
var name = words[0];
// Upper camel cases remaining words.
for(var i = 1, len = words.length; i < len; i++) {
name += words[i].charAt(0).toUpperCase() + words[i].slice(1);
}
return name;
};
/**
* Given a directory, import all files within it.
* opts: {
* ext?:String|Array = '.js'
* ignore?:String|Array, = 'index.js'
* loader?:Function = require
* }
*/
indeks.index = function(dir, opts) {
if(!opts) {
opts = {};
}
opts.ext = opts.ext || '.js';
if(!isArray(opts.ext)) {
opts.ext = [opts.ext];
}
opts.ignore = (opts.ignore !== undefined) ? opts.ignore : 'index.js';
if(!opts.ignore) {
opts.ignore = [];
}
if(!isArray(opts.ignore)) {
opts.ignore = [opts.ignore];
}
opts.loader = opts.loader || require;
var imports = {};
fs.readdirSync(dir).forEach(function(file) {
// Bail if file is not of a whitelisted extension.
var validExt = opts.ext.reduce(function(memo, ext) {
if(file.indexOf(ext, file.length - ext.length) !== -1) {
return true;
}
return memo;
}, false);
if(!validExt) {
return;
}
// Bail if file is blacklisted.
var ignored = opts.ignore.reduce(function(memo, ignore) {
if(file === ignore) {
return true;
}
return memo;
}, false);
if(ignored) {
return;
}
var name = indeks.getVarName(file);
imports[name] = opts.loader(path.join(dir, file));
});
return imports;
};