-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloader.js
107 lines (84 loc) · 2.13 KB
/
loader.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"use strict";
var _ = require("lodash");
var glob = require("glob");
var caller = require("caller");
var path = require("path");
var fs = require("fs");
module.exports = {
load: load
};
var loadedPaths = {};
function exists(filepath) {
try {
fs.accessSync(filepath);
return true;
} catch (_err_) {
return false;
}
}
function load() {
var cwd = path.dirname(caller());
if (arguments.length === 1 || arguments.length === 2) {
return loadWithPatterns(cwd, arguments[0], arguments[1]);
}
loadFromDir(cwd);
}
function loadFromDir(cwd) {
if (loadedPaths[cwd]) {
return;
}
loadedPaths[cwd] = true;
var configPath = path.join(cwd, ".simple-di.json");
if (loadFromConfig(cwd, configPath)) {
return;
}
var pkg = path.join(cwd, "package.json");
if (exists(pkg)) {
return;
}
loadFromDir(path.resolve(cwd, ".."));
}
function loadFromConfig(cwd, filepath) {
if (!exists(filepath)) {
return false;
}
var config = require(filepath);
var patterns = _.filter(config.load, function(item) {
return item[0] !== "!";
});
var patternsToIgnore = _(config.load)
.map(function(item) {
return item[0] === "!" ? item.slice(1) : "";
})
.compact()
.value();
loadWithPatterns(cwd, patterns, patternsToIgnore);
return config.root;
}
function loadWithPatterns(cwd, patterns, patternsToIgnore) {
if (!_.isArray(patterns)) {
patterns = [patterns];
}
if (!_.isArray(patternsToIgnore)) {
patternsToIgnore = [patternsToIgnore];
}
// Fix up all the ignore patterns.
patternsToIgnore = _.map(patternsToIgnore, function(pattern) {
return path.resolve(cwd + "/" + pattern);
});
var options = {
nodir: true,
cwd: cwd,
ignore: patternsToIgnore,
realpath: true,
symlinks: {},
statCache: {},
realpathCache: {},
cache: {}
};
_(patterns)
.flatMap(function(pattern) {
return glob.sync(pattern, options);
})
.each(require);
}