UNPKG

2.59 kBJavaScriptView Raw
1/*jshint node:true,laxcomma:true*/
2'use strict';
3
4var path = require('path')
5 , extend = require('extend')
6 , objectPath = require("object-path")
7 , clone = require('clone')
8 , fs = require('fs')
9;
10function Configurator() {
11
12 if (!(this instanceof Configurator)) {
13 return new Configurator();
14 }
15 this.config = {};
16}
17
18Configurator.prototype.fix = function fix(name, value) {
19 this.config[name] = value;
20};
21
22Configurator.prototype.get = function get(path) {
23 return objectPath.get(this.config, path);
24};
25
26Configurator.prototype.copy = function copy(path) {
27 var val = this.get(path);
28 if (val) {
29 return clone(val);
30 }
31 else {
32 return val;
33 }
34};
35
36
37Configurator.prototype.has = function has(path) {
38 if (!objectPath.has(this.config, path)) {
39 return false;
40 }
41 else {
42 var v = objectPath.get(this.config, path);
43 if (v === undefined || v === null) {
44 return false;
45 }
46 else {
47 return true;
48 }
49 }
50};
51
52Configurator.prototype.set = function set(path, value) {
53 objectPath.set(this.config, path, value);
54};
55
56Configurator.prototype.unset = function unset(path) {
57 objectPath.del(this.config, path);
58};
59
60Configurator.prototype.load = function load(appname, customArgvParser) {
61 require('rc')(appname, this.config, customArgvParser);
62};
63
64Configurator.prototype.local = function local(filename) {
65 try {
66 if (fs.existsSync(filename)) {
67 this.merge(require(filename));
68 this.set('dateConfig', fs.statSync(filename).mtime);
69 return true;
70 }
71 }
72 catch(e) {
73 return e;
74 }
75}
76
77Configurator.prototype.replace = function merge(obj) {
78 var self = this
79 Object.keys(obj).forEach(function(key) {
80 self.config[key] = obj[key];
81 });
82};
83
84
85Configurator.prototype.merge = function merge(obj) {
86 var self = this
87 Object.keys(obj).forEach(function(key) {
88 var o = self.get(key) || {};
89 if (typeof obj[key] === 'object') {
90 if (Array.isArray(obj[key])) {
91 o = obj[key].concat(o);
92 }
93 else {
94 extend(true, o, obj[key]);
95 }
96 }
97 else {
98 o = obj[key];
99 }
100 self.config[key] = o;
101 });
102};
103
104
105
106Configurator.prototype.expose = function expose() {
107 var conf = clone(this.config);
108 var tohide = ['dataPath', 'viewPath', 'collectionName', 'connectionURI', 'connexionURI', 'configs', 'config', '$0', '_', 'browserifyModules', 'collectionsIndexName', 'tempPath', 'theme', 'h', 'd', 'debug', 'help', 'version', 'v', 'verbose', 'V'];
109 tohide.forEach(function(n) {
110 if (conf[n] !== undefined) {
111 delete conf[n];
112 }
113 });
114 return conf;
115};
116
117module.exports = Configurator;