forked from travis4all/dependable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (161 loc) · 4.66 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Generated by CoffeeScript 1.3.3
(function() {
var existsSync, fs, path, _ref;
path = require('path');
fs = require('fs');
existsSync = (_ref = fs.existsSync) != null ? _ref : path.existsSync;
exports.container = function(opts) {
var argList, container, factories, get, haveVisited, load, loaddir, loadfile, modules, notEmpty, options, register, registerOne, resolve, toFactory;
factories = {};
modules = {};
options = opts || {};
register = function(name, func) {
var hash, _results;
if (name === Object(name)) {
hash = name;
_results = [];
for (name in hash) {
func = hash[name];
_results.push(registerOne(name, func));
}
return _results;
} else {
return registerOne(name, func);
}
};
registerOne = function(name, func) {
if (!(func != null)) {
throw new Error("cannot register null function");
}
return factories[name] = toFactory(func);
};
load = function(file) {
var exists, stats;
exists = existsSync(file);
if (exists) {
stats = fs.statSync(file);
if (stats.isDirectory()) {
return loaddir(file);
}
}
return loadfile(file);
};
loadfile = function(file) {
var module, name;
module = file.replace(/\.\w+$/, "");
name = path.basename(module).replace(/\-(\w)/g, function(match, letter) {
return letter.toUpperCase();
});
return modules[name] = module;
};
loaddir = function(dir) {
var file, filenames, files, stats, _i, _len, _results;
filenames = fs.readdirSync(dir);
files = filenames.map(function(file) {
return path.join(dir, file);
});
_results = [];
for (_i = 0, _len = files.length; _i < _len; _i++) {
file = files[_i];
if (!file.match(/\.(js|coffee)$/)) {
continue;
}
stats = fs.statSync(file);
if (stats.isFile()) {
_results.push(loadfile(file));
} else {
_results.push(void 0);
}
}
return _results;
};
toFactory = function(func) {
if (typeof func === "function" && (!options.useFnAnnotations || func.injectDependencies)) {
return {
func: func,
required: argList(func)
};
} else {
return {
func: function() {
return func;
},
required: []
};
}
};
argList = function(func) {
var match, required;
match = func.toString().match(/function.*?\(([\s\S]*?)\)/);
if (!(match != null)) {
throw new Error("could not parse function arguments: " + (func != null ? func.toString() : void 0));
}
required = match[1].split(",").filter(notEmpty).map(function(str) {
return str.trim();
});
return required;
};
notEmpty = function(a) {
return a;
};
get = function(name, overrides, visited) {
var dependencies, factory, instance, isOverridden, module;
if (visited == null) {
visited = [];
}
isOverridden = overrides != null;
if (haveVisited(visited, name)) {
throw new Error("circular dependency with '" + name + "'");
}
visited = visited.concat(name);
factory = factories[name];
if (!(factory != null)) {
module = modules[name];
if (module != null) {
register(name, require(module));
factory = factories[name];
} else {
throw new Error("dependency '" + name + "' was not registered");
}
}
if ((factory.instance != null) && !isOverridden) {
return factory.instance;
}
dependencies = factory.required.map(function(name) {
if ((overrides != null ? overrides[name] : void 0) != null) {
return overrides != null ? overrides[name] : void 0;
} else {
return get(name, overrides, visited);
}
});
instance = factory.func.apply(factory, dependencies);
if (!isOverridden) {
factory.instance = instance;
}
return instance;
};
haveVisited = function(visited, name) {
var isName;
isName = function(n) {
return n === name;
};
return visited.filter(isName).length;
};
resolve = function(overrides, func) {
if (!func) {
func = overrides;
overrides = null;
}
register("__temp", func);
return get("__temp", overrides);
};
container = {
get: get,
resolve: resolve,
register: register,
load: load
};
container.register("_container", container);
return container;
};
}).call(this);