1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var _ = require('lodash');
|
11 | var Symbol = require('symbol');
|
12 | var LangUtils = require("./utils").LangUtils;
|
13 | var Args = require('./utils').Args;
|
14 | var TraceUtils = require('./utils').TraceUtils;
|
15 | var PathUtils = require('./utils').PathUtils;
|
16 | var AbstractClassError = require('./errors').AbstractClassError;
|
17 |
|
18 | var configProperty = Symbol('config');
|
19 | var currentConfiguration = Symbol('current');
|
20 | var configPathProperty = Symbol('configurationPath');
|
21 | var executionPathProperty = Symbol('executionPath');
|
22 | var strategiesProperty = Symbol('strategies');
|
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 inaccessible.');
|
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 inaccessible.');
|
70 | }
|
71 | else {
|
72 | TraceUtils.error('An error occurred 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 occurred 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 | Object.defineProperty(this, 'settings',{
|
95 | get: function() {
|
96 | return this[configProperty]['settings'];
|
97 | },
|
98 | enumerable:true,
|
99 | configurable:false});
|
100 |
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | ConfigurationBase.prototype.getSource = function() {
|
108 | return this[configProperty];
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | ConfigurationBase.prototype.getSourceAt = function(p) {
|
117 | return _.at(this[configProperty],p.replace(/\//g,'.'))[0];
|
118 | };
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | ConfigurationBase.prototype.hasSourceAt = function(p) {
|
126 | return _.isObject(_.at(this[configProperty],p.replace(/\//g,'.'))[0]);
|
127 | };
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | ConfigurationBase.prototype.setSourceAt = function(p, value) {
|
136 | return _.set(this[configProperty], p.replace(/\//g,'.'), value);
|
137 | };
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | ConfigurationBase.prototype.setExecutionPath = function(p) {
|
145 | this[executionPathProperty] = p;
|
146 | return this;
|
147 | };
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | ConfigurationBase.prototype.getExecutionPath = function() {
|
154 | return this[executionPathProperty];
|
155 | };
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | ConfigurationBase.prototype.getConfigurationPath = function() {
|
162 | return this[configPathProperty];
|
163 | };
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | ConfigurationBase.prototype.useStrategy = function(strategyBaseCtor, strategyCtor) {
|
172 | Args.notFunction(strategyBaseCtor,"Configuration strategy constructor");
|
173 | if (typeof strategyCtor === 'undefined') {
|
174 | this[strategiesProperty]["$".concat(strategyBaseCtor.name)] = new strategyBaseCtor(this);
|
175 | return this;
|
176 | }
|
177 | Args.notFunction(strategyCtor,"Strategy constructor");
|
178 | this[strategiesProperty]["$".concat(strategyBaseCtor.name)] = new strategyCtor(this);
|
179 | return this;
|
180 | };
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | ConfigurationBase.prototype.getStrategy = function(strategyBaseCtor) {
|
187 | Args.notFunction(strategyBaseCtor,"Configuration strategy constructor");
|
188 | return this[strategiesProperty]["$".concat(strategyBaseCtor.name)];
|
189 | };
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | ConfigurationBase.prototype.hasStrategy = function(strategyBaseCtor) {
|
196 | Args.notFunction(strategyBaseCtor,"Configuration strategy constructor");
|
197 | return typeof this[strategiesProperty]["$".concat(strategyBaseCtor.name)] !== 'undefined';
|
198 | };
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | ConfigurationBase.getCurrent = function() {
|
205 | if (_.isNil(ConfigurationBase[currentConfiguration])) {
|
206 | ConfigurationBase[currentConfiguration] = new ConfigurationBase();
|
207 | }
|
208 | return ConfigurationBase[currentConfiguration];
|
209 | };
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | ConfigurationBase.setCurrent = function(configuration) {
|
216 | if (configuration instanceof ConfigurationBase) {
|
217 | if (!configuration.hasStrategy(ModuleLoaderStrategy)) {
|
218 | configuration.useStrategy(ModuleLoaderStrategy, DefaultModuleLoaderStrategy);
|
219 | }
|
220 | ConfigurationBase[currentConfiguration] = configuration;
|
221 | return ConfigurationBase[currentConfiguration];
|
222 | }
|
223 | throw new TypeError('Invalid argument. Expected an instance of DataConfiguration class.');
|
224 | };
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | function ConfigurationStrategy(config) {
|
233 | Args.check(this.constructor.name !== ConfigurationStrategy, new AbstractClassError());
|
234 | Args.notNull(config, 'Configuration');
|
235 | this[configProperty] = config;
|
236 | }
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | ConfigurationStrategy.prototype.getConfiguration = function() {
|
242 | return this[configProperty];
|
243 | };
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | function ModuleLoaderStrategy(config) {
|
252 | ModuleLoaderStrategy.super_.bind(this)(config);
|
253 | }
|
254 | LangUtils.inherits(ModuleLoaderStrategy, ConfigurationStrategy);
|
255 |
|
256 | ModuleLoaderStrategy.prototype.require = function(modulePath) {
|
257 | Args.notEmpty(modulePath,'Module Path');
|
258 | if (!/^.\//i.test(modulePath)) {
|
259 | if (require.resolve && require.resolve.paths) {
|
260 | |
261 |
|
262 |
|
263 |
|
264 | var paths = require.resolve.paths(modulePath);
|
265 |
|
266 | var path1 = this.getConfiguration().getExecutionPath();
|
267 |
|
268 | while (path1) {
|
269 |
|
270 | if (paths.indexOf(PathUtils.join(path1,'node_modules'))<0) {
|
271 |
|
272 | paths.push(PathUtils.join(path1,'node_modules'));
|
273 |
|
274 | if (path1 === PathUtils.join(path1,'..')) {
|
275 |
|
276 | break;
|
277 | }
|
278 |
|
279 | path1 = PathUtils.join(path1,'..');
|
280 | }
|
281 | else {
|
282 |
|
283 | break;
|
284 | }
|
285 | }
|
286 | var finalModulePath = require.resolve(modulePath, {
|
287 | paths:paths
|
288 | });
|
289 | return require(finalModulePath);
|
290 | }
|
291 | else {
|
292 | return require(modulePath);
|
293 | }
|
294 | }
|
295 | return require(PathUtils.join(this.getConfiguration().getExecutionPath(),modulePath));
|
296 | };
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 | function DefaultModuleLoaderStrategy(config) {
|
305 | DefaultModuleLoaderStrategy.super_.bind(this)(config);
|
306 | }
|
307 | LangUtils.inherits(DefaultModuleLoaderStrategy, ModuleLoaderStrategy);
|
308 |
|
309 |
|
310 |
|
311 | if (typeof exports !== 'undefined') {
|
312 | module.exports.ConfigurationBase = ConfigurationBase;
|
313 | module.exports.ConfigurationStrategy = ConfigurationStrategy;
|
314 | module.exports.ModuleLoaderStrategy = ModuleLoaderStrategy;
|
315 | module.exports.DefaultModuleLoaderStrategy = DefaultModuleLoaderStrategy;
|
316 | }
|
317 |
|
318 |
|