1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var _ = require('lodash');
|
10 | var Symbol = require('symbol');
|
11 | var LangUtils = require("./utils").LangUtils;
|
12 | var Args = require('./utils').Args;
|
13 | var TraceUtils = require('./utils').TraceUtils;
|
14 | var PathUtils = require('./utils').PathUtils;
|
15 | var AbstractClassError = require('./errors').AbstractClassError;
|
16 |
|
17 | var configProperty = Symbol('config');
|
18 | var currentConfiguration = Symbol('current');
|
19 | var configPathProperty = Symbol('configurationPath');
|
20 | var executionPathProperty = Symbol('executionPath');
|
21 | var strategiesProperty = Symbol('strategies');
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function ConfigurationBase(configPath) {
|
31 |
|
32 | this[strategiesProperty] = { };
|
33 |
|
34 | this[configPathProperty] = configPath || PathUtils.join(process.cwd(),'config');
|
35 | TraceUtils.debug('Initializing configuration under %s.', this[configPathProperty]);
|
36 |
|
37 | this[executionPathProperty] = PathUtils.join(this[configPathProperty],'..');
|
38 | TraceUtils.debug('Setting execution path under %s.', this[executionPathProperty]);
|
39 |
|
40 |
|
41 | this.useStrategy(ModuleLoaderStrategy, DefaultModuleLoaderStrategy);
|
42 |
|
43 |
|
44 | var configSourcePath;
|
45 | try {
|
46 | var env = 'production';
|
47 |
|
48 | if (process && process.env) {
|
49 | env = process.env['NODE_ENV'] || 'production';
|
50 | }
|
51 |
|
52 | else if (window && window.env) {
|
53 | env = window.env['BROWSER_ENV'] || 'production';
|
54 | }
|
55 | configSourcePath = PathUtils.join(this[configPathProperty], 'app.' + env + '.json');
|
56 | TraceUtils.debug('Validating environment configuration source on %s.', configSourcePath);
|
57 | this[configProperty] = require(configSourcePath);
|
58 | }
|
59 | catch (err) {
|
60 | if (err.code === 'MODULE_NOT_FOUND') {
|
61 | TraceUtils.log('The environment specific configuration cannot be found or is inaccesible.');
|
62 | try {
|
63 | configSourcePath = PathUtils.join(this[configPathProperty], 'app.json');
|
64 | TraceUtils.debug('Validating application configuration source on %s.', configSourcePath);
|
65 | this[configProperty] = require(configSourcePath);
|
66 | }
|
67 | catch(err) {
|
68 | if (err.code === 'MODULE_NOT_FOUND') {
|
69 | TraceUtils.log('The default application configuration cannot be found or is inaccesible.');
|
70 | }
|
71 | else {
|
72 | TraceUtils.error('An error occured while trying to open default application configuration.');
|
73 | TraceUtils.error(err);
|
74 | }
|
75 | TraceUtils.debug('Initializing empty configuration');
|
76 | this[configProperty] = { };
|
77 | }
|
78 | }
|
79 | else {
|
80 | TraceUtils.error('An error occured while trying to open application configuration.');
|
81 | TraceUtils.error(err);
|
82 |
|
83 | this[configProperty] = { };
|
84 | }
|
85 | }
|
86 |
|
87 | this[configProperty]['settings'] = this[configProperty]['settings'] || { };
|
88 |
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | Object.defineProperty(this, 'settings',{
|
96 | get: function() {
|
97 | return this[configProperty]['settings'];
|
98 | },
|
99 | enumerable:true,
|
100 | configurable:false});
|
101 |
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | ConfigurationBase.prototype.getSource = function() {
|
109 | return this[configProperty];
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | ConfigurationBase.prototype.getSourceAt = function(p) {
|
118 | return _.at(this[configProperty],p.replace(/\//g,'.'))[0];
|
119 | };
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | ConfigurationBase.prototype.hasSourceAt = function(p) {
|
127 | return _.isObject(_.at(this[configProperty],p.replace(/\//g,'.'))[0]);
|
128 | };
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | ConfigurationBase.prototype.setSourceAt = function(p, value) {
|
137 | return _.set(this[configProperty], p.replace(/\//g,'.'), value);
|
138 | };
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | ConfigurationBase.prototype.setExecutionPath = function(p) {
|
146 | this[executionPathProperty] = p;
|
147 | return this;
|
148 | };
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | ConfigurationBase.prototype.getExecutionPath = function() {
|
155 | return this[executionPathProperty];
|
156 | };
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | ConfigurationBase.prototype.getConfigurationPath = function() {
|
163 | return this[configPathProperty];
|
164 | };
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | ConfigurationBase.prototype.useStrategy = function(configStrategyCtor, strategyCtor) {
|
173 | Args.notFunction(configStrategyCtor,"Configuration strategy constructor");
|
174 | Args.notFunction(strategyCtor,"Strategy constructor");
|
175 | this[strategiesProperty]["$".concat(configStrategyCtor.name)] = new strategyCtor(this);
|
176 | return this;
|
177 | };
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | ConfigurationBase.prototype.getStrategy = function(configStrategyCtor) {
|
184 | Args.notFunction(configStrategyCtor,"Configuration strategy constructor");
|
185 | return this[strategiesProperty]["$".concat(configStrategyCtor.name)];
|
186 | };
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | ConfigurationBase.prototype.hasStrategy = function(configStrategyCtor) {
|
193 | Args.notFunction(configStrategyCtor,"Configuration strategy constructor");
|
194 | return typeof this[strategiesProperty]["$".concat(configStrategyCtor.name)] !== 'undefined';
|
195 | };
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | ConfigurationBase.getCurrent = function() {
|
202 | if (_.isNil(ConfigurationBase[currentConfiguration])) {
|
203 | ConfigurationBase[currentConfiguration] = new ConfigurationBase();
|
204 | }
|
205 | return ConfigurationBase[currentConfiguration];
|
206 | };
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | ConfigurationBase.setCurrent = function(configuration) {
|
213 | if (configuration instanceof ConfigurationBase) {
|
214 | if (!configuration.hasStrategy(ModuleLoaderStrategy)) {
|
215 | configuration.useStrategy(ModuleLoaderStrategy, DefaultModuleLoaderStrategy);
|
216 | }
|
217 | ConfigurationBase[currentConfiguration] = configuration;
|
218 | return ConfigurationBase[currentConfiguration];
|
219 | }
|
220 | throw new TypeError('Invalid argument. Expected an instance of DataConfiguration class.');
|
221 | };
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | function ConfigurationStrategy(config) {
|
230 | Args.check(this.constructor.name !== ConfigurationStrategy, new AbstractClassError());
|
231 | Args.notNull(config, 'Configuration');
|
232 | this[configProperty] = config;
|
233 | }
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | ConfigurationStrategy.prototype.getConfiguration = function() {
|
239 | return this[configProperty];
|
240 | };
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | function ModuleLoaderStrategy(config) {
|
249 | ModuleLoaderStrategy.super_.bind(this)(config);
|
250 | }
|
251 | LangUtils.inherits(ModuleLoaderStrategy, ConfigurationStrategy);
|
252 |
|
253 | ModuleLoaderStrategy.prototype.require = function(modulePath) {
|
254 | Args.notEmpty(modulePath,'Module Path');
|
255 | if (!/^.\//i.test(modulePath)) {
|
256 | if (require.resolve && require.resolve.paths) {
|
257 | |
258 |
|
259 |
|
260 |
|
261 | let paths = require.resolve.paths(modulePath);
|
262 |
|
263 | let path1 = this.getConfiguration().getExecutionPath();
|
264 |
|
265 | while (path1) {
|
266 |
|
267 | if (paths.indexOf(PathUtils.join(path1,'node_modules'))<0) {
|
268 |
|
269 | paths.push(PathUtils.join(path1,'node_modules'));
|
270 |
|
271 | if (path1 === PathUtils.join(path1,'..')) {
|
272 |
|
273 | break;
|
274 | }
|
275 |
|
276 | path1 = PathUtils.join(path1,'..');
|
277 | }
|
278 | else {
|
279 |
|
280 | break;
|
281 | }
|
282 | }
|
283 | let finalModulePath = require.resolve(modulePath, {
|
284 | paths:paths
|
285 | });
|
286 | return require(finalModulePath);
|
287 | }
|
288 | else {
|
289 | return require(modulePath);
|
290 | }
|
291 | }
|
292 | return require(PathUtils.join(this.getConfiguration().getExecutionPath(),modulePath));
|
293 | };
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 | function DefaultModuleLoaderStrategy(config) {
|
302 | DefaultModuleLoaderStrategy.super_.bind(this)(config);
|
303 | }
|
304 | LangUtils.inherits(DefaultModuleLoaderStrategy, ModuleLoaderStrategy);
|
305 |
|
306 |
|
307 | if (typeof exports !== 'undefined') {
|
308 | module.exports.ConfigurationBase = ConfigurationBase;
|
309 | module.exports.ConfigurationStrategy = ConfigurationStrategy;
|
310 | module.exports.ModuleLoaderStrategy = ModuleLoaderStrategy;
|
311 | module.exports.DefaultModuleLoaderStrategy = DefaultModuleLoaderStrategy;
|
312 | }
|
313 |
|
314 |
|