1 |
|
2 | var Container, clearCache, evaluateType, getFullPath, getPath, isRelative, populateContainer;
|
3 |
|
4 | Container = require('./container');
|
5 |
|
6 | isRelative = function(path) {
|
7 | return path.indexOf('.') === 0;
|
8 | };
|
9 |
|
10 | getFullPath = function(path, basePath) {
|
11 | if (typeof window !== "undefined" && window !== null) {
|
12 | return path;
|
13 | } else if (isRelative(path)) {
|
14 | return [basePath, path].join('/');
|
15 | } else {
|
16 | return [basePath, 'node_modules', path].join('/');
|
17 | }
|
18 | };
|
19 |
|
20 | evaluateType = function(moduleConfig, module) {
|
21 | var path;
|
22 | if (moduleConfig.type != null) {
|
23 | return moduleConfig.type;
|
24 | }
|
25 | path = moduleConfig.require || moduleConfig;
|
26 | if (typeof module === 'function' && isRelative(path)) {
|
27 | return 'factory';
|
28 | } else {
|
29 | return 'value';
|
30 | }
|
31 | };
|
32 |
|
33 | getPath = function(moduleConfig) {
|
34 | if (typeof moduleConfig === 'string') {
|
35 | return moduleConfig;
|
36 | } else {
|
37 | return moduleConfig.require;
|
38 | }
|
39 | };
|
40 |
|
41 | clearCache = function(modules, basePath) {
|
42 | return Object.keys(modules).forEach(function(key) {
|
43 | var fullPath, moduleConfig, path;
|
44 | moduleConfig = modules[key];
|
45 | path = getPath(moduleConfig);
|
46 | fullPath = getFullPath(path, basePath);
|
47 | return delete require.cache[require.resolve(fullPath)];
|
48 | });
|
49 | };
|
50 |
|
51 | populateContainer = function(di, modules, basePath) {
|
52 | if ((typeof options !== "undefined" && options !== null ? options.clearCache : void 0) === true) {
|
53 | clearCache(modules, basePath);
|
54 | }
|
55 | return Object.keys(modules).forEach(function(key) {
|
56 | var e, error, fullPath, module, moduleConfig, path, type;
|
57 | moduleConfig = modules[key];
|
58 | path = getPath(moduleConfig);
|
59 | fullPath = getFullPath(path, basePath);
|
60 | try {
|
61 | module = require(fullPath);
|
62 | } catch (error) {
|
63 | e = error;
|
64 | module = require(path);
|
65 | }
|
66 | type = evaluateType(moduleConfig, module);
|
67 | if (type === 'factory') {
|
68 | return di.factory(key, module);
|
69 | } else {
|
70 | return di.value(key, module);
|
71 | }
|
72 | });
|
73 | };
|
74 |
|
75 | module.exports = function(options) {
|
76 | var basePath, config, configParents, di, modules, opts, parents;
|
77 | config = options.config, basePath = options.basePath, opts = options.opts, parents = options.parents;
|
78 | if (config == null) {
|
79 | throw new TypeError('No configuration was provided for loadConfig');
|
80 | }
|
81 | di = new Container(opts, parents);
|
82 | modules = config.modules || {};
|
83 | configParents = config.parents || [];
|
84 | configParents.forEach(function(p) {
|
85 | var parentModules;
|
86 | parentModules = require([p.nodeModule, p.configFile].join('/'));
|
87 | return populateContainer(di, parentModules.modules, p.nodeModule);
|
88 | });
|
89 | populateContainer(di, modules, basePath);
|
90 | return di;
|
91 | };
|