-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
163 lines (152 loc) · 5.46 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
// Generated by CoffeeScript 1.11.1
(function() {
var injector,
slice = [].slice;
injector = require('def-type').Module(function() {
var ENV, _getModuleName, _import, _importGlobal, _wrapperImport, config, cwd, definedModule, path;
definedModule = null;
config = {
bypassInjection: true
};
ENV = 'production';
path = require('path');
cwd = process.cwd();
/**
* @public
* Sets a wrapper to add variables that use @import inside the passed fn to require modules,
* this allows to inject dependencies via the fn wrapper instead of the actually required
* modules.
* @param {function} wrapperFn - Function where the return value is what is exported later
*/
this.set = function(wrapperFn) {
wrapperFn["import"] = _wrapperImport;
wrapperFn.importGlobal = _importGlobal;
if (config.bypassInjection) {
definedModule = wrapperFn.call(wrapperFn);
} else {
definedModule = function(dependencies) {
wrapperFn.dependencies = dependencies;
return wrapperFn.call(wrapperFn);
};
definedModule.__injectorWrapper__ = true;
}
return definedModule;
};
/**
* @public
* Returns the defined module, which could be the wrapper fn defined in the @set method
* or what ever was returned from that function, depending of the setting of the byPassInjection option
* in the config object.
*/
this.get = function() {
var t;
t = definedModule;
definedModule = null;
return t;
};
/**
* @public
* When defined as true (default), whenever you set an injector wrapper, you set the defined module
* to whatever is returned by that wrapper, and when set to false you get the wrapper instead. This
* wrapper function is what allows you to inject dependencies by passing a single object to it
*/
this.bypassInjection = function(boolean) {
config.bypassInjection = boolean;
return this;
};
/**
* @public
* Lets you set in a more declarative way that you want to bypass or not the injection system.
* @example require('commonjs-injector').setEnv('testing') should be used before running your
* test, and a regular require without .setEnv or require('commonjs-injector').setEnv('production')
* will set to true the bypassing.
*/
this.setEnv = function(environment) {
var errMsg;
ENV = environment.toLowerCase();
switch (ENV) {
case "production":
config.bypassInjection = true;
break;
case "testing":
config.bypassInjection = false;
break;
default:
errMsg = 'You should set the environment of the injector as either testing or production';
throw new Error(errMsg);
}
return this;
};
this.getEnv = function() {
return ENV;
};
/**
* @public
* Mimics node require behavior, with some subtle differences:
* Files are required relative to the cwd
*/
this["import"] = function() {
var pathFragments;
pathFragments = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return _import(pathFragments);
};
/**
* @private
* This function is added to the wrapperFn (i.e wrapperFn.import) to use instead of the node.js
* require function, this allows to bypass the actual module requiring by injecting the module
* in the dependencies obj of the wrapperFn.
*/
_wrapperImport = function() {
var moduleName, pathFragments, ref;
pathFragments = 1 <= arguments.length ? slice.call(arguments, 0) : [];
moduleName = _getModuleName(pathFragments);
if (((ref = this.dependencies) != null ? ref[moduleName] : void 0) != null) {
return this.dependencies[moduleName];
} else {
return _import(pathFragments);
}
};
_getModuleName = function(pathFragments) {
var moduleName;
return moduleName = pathFragments[pathFragments.length - 1].split('/').pop();
};
_import = function(pathFragments) {
var filePath, fragsLen, isNpmModule, module;
fragsLen = pathFragments.length;
isNpmModule = pathFragments[0].indexOf('/') === -1;
if (fragsLen === 1) {
if (isNpmModule) {
filePath = pathFragments[0];
} else {
pathFragments.splice(0, 0, cwd);
filePath = path.resolve.apply(this, pathFragments);
}
} else {
filePath = path.resolve.apply(this, pathFragments);
}
module = require(filePath);
if (module.__injectorWrapper__ != null) {
module = module();
}
return module;
};
/**
* @private
* This function is added to the wrapperFn (i.e wrapperFn.importGlobal) to define global variables
* as local ones, this allows to inject whatever we want in wrapperFn as long as we use the same name.
* @example @importGlobal('async') will call global.async internally or the injected value
* @TODO Should allow subobject keys, meaning that you should be able to call @importGlobal('someGloba.key.anotherKey'),
* which right now you can't. Just first lvl globals.
*/
return _importGlobal = function(globalName) {
var ref;
if (((ref = this.dependencies) != null ? ref[globalName] : void 0) != null) {
return this.dependencies[globalName];
} else {
return global[globalName];
}
};
});
module.exports = injector;
}).call(this);
//# sourceMappingURL=index.js.map