forked from krzysztofantczak/castor-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigurator.js
117 lines (99 loc) · 2.53 KB
/
configurator.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
/*jshint node:true,laxcomma:true*/
'use strict';
var path = require('path')
, extend = require('extend')
, objectPath = require("object-path")
, clone = require('clone')
, fs = require('fs')
;
function Configurator() {
if (!(this instanceof Configurator)) {
return new Configurator();
}
this.config = {};
}
Configurator.prototype.fix = function fix(name, value) {
this.config[name] = value;
};
Configurator.prototype.get = function get(path) {
return objectPath.get(this.config, path);
};
Configurator.prototype.copy = function copy(path) {
var val = this.get(path);
if (val) {
return clone(val);
}
else {
return val;
}
};
Configurator.prototype.has = function has(path) {
if (!objectPath.has(this.config, path)) {
return false;
}
else {
var v = objectPath.get(this.config, path);
if (v === undefined || v === null) {
return false;
}
else {
return true;
}
}
};
Configurator.prototype.set = function set(path, value) {
objectPath.set(this.config, path, value);
};
Configurator.prototype.unset = function unset(path) {
objectPath.del(this.config, path);
};
Configurator.prototype.load = function load(appname, customArgvParser) {
require('rc')(appname, this.config, customArgvParser);
};
Configurator.prototype.local = function local(filename) {
try {
if (fs.existsSync(filename)) {
this.merge(require(filename));
this.set('dateConfig', fs.statSync(filename).mtime);
return true;
}
}
catch(e) {
return e;
}
}
Configurator.prototype.replace = function merge(obj) {
var self = this
Object.keys(obj).forEach(function(key) {
self.config[key] = obj[key];
});
};
Configurator.prototype.merge = function merge(obj) {
var self = this
Object.keys(obj).forEach(function(key) {
var o = self.get(key) || {};
if (typeof obj[key] === 'object') {
if (Array.isArray(obj[key])) {
o = obj[key].concat(o);
}
else {
extend(true, o, obj[key]);
}
}
else {
o = obj[key];
}
self.config[key] = o;
});
};
Configurator.prototype.expose = function expose() {
var conf = clone(this.config);
var tohide = ['dataPath', 'viewPath', 'collectionName', 'connectionURI', 'connexionURI', 'configs', 'config', '$0', '_', 'browserifyModules', 'collectionsIndexName', 'tempPath', 'theme', 'h', 'd', 'debug', 'help', 'version', 'v', 'verbose', 'V'];
tohide.forEach(function(n) {
if (conf[n] !== undefined) {
delete conf[n];
}
});
return conf;
};
module.exports = Configurator;