-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
56 lines (43 loc) · 1.4 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
/*!
* directory
* Copyright(c) Thomas Blobaum
* MIT Licensed
*/
var path = require('path')
, fs = require('fs')
function directory (dirname, callback) {
// dirname and callback are sent
if (typeof dirname === 'string' && typeof callback === 'function') {
return require_directory(dirname, callback)
}
// only dirname is sent
if (typeof dirname === 'string' && typeof callback === 'undefined') {
return require_directory(dirname, function () {})
}
// only callback is sent
if (typeof dirname === 'function' && typeof callback === 'undefined') {
// 12 is the length of node_modules
return require_directory(module.parent.paths[0].slice(0, -12), dirname)
}
// no arguments
if (typeof dirname === 'undefined' && typeof callback === 'undefined') {
return require_directory(module.parent.paths[0].slice(0, -12), function () {})
}
}
function require_directory (dirname, callback) {
var paths = fs.readdirSync(dirname)
, fns = {}
for (var l = paths.length, a=0; a < l; a++) {
var path = dirname + paths[a]
if (!path.match(module.parent.id) || module.parent.id === '.') {
var filename = path.split(dirname)[1].split(".js")[0]
, fn = require(path)
fns[filename] = fn
callback(fn, filename)
}
}
return fns
}
// delete 'directory' (this file) from the cache
delete require.cache[__filename]
module.exports = directory