-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.js
More file actions
317 lines (291 loc) · 10.8 KB
/
Copy pathconfig.js
File metadata and controls
317 lines (291 loc) · 10.8 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
const isObjectLike = require('lodash/isObjectLike');
const isObject = require('lodash/isObject');
const at = require('lodash/at');
const set = require('lodash/set');
const {TraceUtils, Args, PathUtils} = require('./utils');
const currentConfiguration = Symbol('current');
const isNode = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
class ConfigurationBase {
/**
* @class Represents an application configuration
* @param {(string|*)=} configPathOrSource
* @property {*} settings
* @constructor
*/
constructor(configPathOrSource) {
//init strategies
const strategies = new Map();
Object.defineProperty(this, '_strategies', {
enumerable: false,
configurable: false,
get: () => { return strategies; }
});
// set current configuration
let source = {
settings: {},
};
Object.defineProperty(this, '_source', {
enumerable: false,
configurable: false,
get: () => { return source; }
});
// use default module loader strategy
this.useStrategy(ModuleLoaderStrategy, DefaultModuleLoaderStrategy);
// if configPathOrSource is not defined, then use the current working directory
if (isNode) {
// set configuration path to the current working directory
this.configurationPath = PathUtils.join(process.cwd(), 'config');
// set execution path to the current working directory
this.executionPath = process.cwd();
}
if (typeof configPathOrSource === 'undefined') {
return;
}
if (isObjectLike(configPathOrSource)) {
// set configuration source
source = configPathOrSource;
// and return
return;
}
this.configurationPath = configPathOrSource || PathUtils.join(process.cwd(), 'config');
TraceUtils.debug(`Initializing configuration under ${this.configurationPath}`);
this.executionPath = PathUtils.join(this.configurationPath, '..');
TraceUtils.debug(`Set execution path under ${this.executionPath}`);
//get configuration source
var configSourcePath;
try {
var env = 'production';
//node.js mode
if (process && process.env) {
env = process.env['NODE_ENV'] || 'production';
}
//browser mode
else if (window && window.env) {
env = window.env['BROWSER_ENV'] || 'production';
}
configSourcePath = PathUtils.join(this.configurationPath, 'app.' + env + '.json');
TraceUtils.debug(`Validating environment configuration source on ${configSourcePath}`);
source = require(configSourcePath);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
TraceUtils.debug('The environment specific configuration cannot be found or is inaccessible.');
try {
configSourcePath = PathUtils.join(this.configurationPath, 'app.json');
TraceUtils.debug(`Validating application configuration source on ${configSourcePath}.`);
source = require(configSourcePath);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
TraceUtils.debug('The default application configuration cannot be found or is inaccessible.');
} else {
TraceUtils.error('An error occurred while trying to open default application configuration.');
TraceUtils.error(err);
}
TraceUtils.debug('Initializing empty configuration');
source = {};
}
} else {
TraceUtils.error('An error occurred while trying to open application configuration.');
TraceUtils.error(err);
}
}
//initialize settings object
source.settings = source.settings || {};
}
get settings() {
return this._source.settings;
}
/**
* Returns the configuration source object
* @returns {*}
*/
getSource() {
return this._source;
}
/**
* Returns the source configuration object based on the given path (e.g. settings.auth.cookieName or settings/auth/cookieName)
* @param {string} p - A string which represents an object path
* @returns {Object|Array}
*/
getSourceAt(p) {
return at(this._source, p.replace(/\//g, '.'))[0];
}
/**
* Returns a boolean which indicates whether the specified object path exists or not (e.g. settings.auth.cookieName or settings/auth/cookieName)
* @param {string} p - A string which represents an object path
* @returns {boolean}
*/
hasSourceAt(p) {
return isObject(at(this._source, p.replace(/\//g, '.'))[0]);
}
/**
* Sets the config value to the specified object path (e.g. settings.auth.cookieName or settings/auth/cookieName)
* @param {string} p - A string which represents an object path
* @param {*} value
* @returns {Object}
*/
setSourceAt(p, value) {
return set(this._source, p.replace(/\//g, '.'), value);
}
/**
* Sets the current execution path
* @param {string} p
* @returns ConfigurationBase
*/
setExecutionPath(p) {
this.executionPath = p;
return this;
}
/**
* Gets the current execution path
* @returns {string}
*/
getExecutionPath() {
return this.executionPath;
}
/**
* Gets the current configuration path
* @returns {string}
*/
getConfigurationPath() {
return this.configurationPath;
}
/**
* Register a configuration strategy
* @param {Function} strategyBaseCtor
* @param {Function=} strategyCtor
* @returns ConfigurationBase
*/
useStrategy(strategyBaseCtor, strategyCtor) {
Args.notFunction(strategyBaseCtor, 'Configuration strategy constructor');
if (typeof strategyCtor === 'undefined') {
this._strategies.set('$'.concat(strategyBaseCtor.name), new strategyBaseCtor(this));
return this;
}
Args.notFunction(strategyCtor, 'Strategy constructor');
this._strategies.set('$'.concat(strategyBaseCtor.name), new strategyCtor(this));
return this;
}
/**
* Gets a configuration strategy
* @param {Function} strategyBaseCtor
*/
getStrategy(strategyBaseCtor) {
Args.notFunction(strategyBaseCtor, 'Configuration strategy constructor');
return this._strategies.get('$'.concat(strategyBaseCtor.name));
}
/**
* Gets a configuration strategy
* @param {Function} strategyBaseCtor
*/
hasStrategy(strategyBaseCtor) {
Args.notFunction(strategyBaseCtor, 'Configuration strategy constructor');
return this._strategies.has('$'.concat(strategyBaseCtor.name));
}
/**
* Gets the current configuration
* @returns ConfigurationBase - An instance of DataConfiguration class which represents the current data configuration
*/
static getCurrent() {
if (ConfigurationBase[currentConfiguration] == null) {
ConfigurationBase[currentConfiguration] = new ConfigurationBase();
}
return ConfigurationBase[currentConfiguration];
}
/**
* Sets the current configuration
* @param {ConfigurationBase} configuration
* @returns ConfigurationBase - An instance of ApplicationConfiguration class which represents the current configuration
*/
static setCurrent(configuration) {
if (configuration instanceof ConfigurationBase) {
if (!configuration.hasStrategy(ModuleLoaderStrategy)) {
configuration.useStrategy(ModuleLoaderStrategy, DefaultModuleLoaderStrategy);
}
ConfigurationBase[currentConfiguration] = configuration;
return ConfigurationBase[currentConfiguration];
}
throw new TypeError('Invalid argument. Expected an instance of DataConfiguration class.');
}
}
class ConfigurationStrategy {
/**
* @class
* @param {ConfigurationBase} config
* @constructor
* @abstract
*/
constructor(config) {
Args.notNull(config, 'Configuration');
Object.defineProperty(this, '_configuration', {
value: config,
writable: false,
enumerable: false,
configurable: false
});
}
/**
* @returns {ConfigurationBase}
*/
getConfiguration() {
return this._configuration;
}
}
class ModuleLoaderStrategy extends ConfigurationStrategy {
/**
* @constructor
* @param {ConfigurationBase} config
*/
constructor(config) {
super(config);
}
require(modulePath) {
Args.notEmpty(modulePath, 'Module Path');
if (!/^.\//i.test(modulePath)) {
if (require.resolve && require.resolve.paths) {
/**
* get require paths collection
* @type string[]
*/
var paths = require.resolve.paths(modulePath);
//get execution
var path1 = this.getConfiguration().getExecutionPath();
//loop directories to parent (like classic require)
while (path1) {
//if path does not exist in paths collection
if (paths.indexOf(PathUtils.join(path1, 'node_modules')) < 0) {
//add it
paths.push(PathUtils.join(path1, 'node_modules'));
//and check the next path which is going to be resolved
if (path1 === PathUtils.join(path1, '..')) {
//if it is the same with the current path break loop
break;
}
//otherwise get parent path
path1 = PathUtils.join(path1, '..');
} else {
//path already exists in paths collection, so break loop
break;
}
}
var finalModulePath = require.resolve(modulePath, {
paths: paths
});
return require(finalModulePath);
} else {
return require(modulePath);
}
}
return require(PathUtils.join(this.getConfiguration().getExecutionPath(), modulePath));
}
}
class DefaultModuleLoaderStrategy extends ModuleLoaderStrategy {
constructor(config) {
super(config);
}
}
module.exports = {
ConfigurationBase,
ConfigurationStrategy,
ModuleLoaderStrategy,
DefaultModuleLoaderStrategy
};