-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.js
143 lines (109 loc) · 3.77 KB
/
Config.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
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
/**
* RwConfig module.
* Provides functionalities to handle configuration files in json
*
* @module RwConfig
*/
/**
* Module dependencies.
*/
var fs = require('fs')
var Objects = require('RwUtils').Objects;
/**
* Open a configuration. Different environments are managed.
* Shared environment is appended as configuration for each environment
*
*/
function RwConfig() {
/**
* Loaded configuration
*/
var configurations = {};
/**
* Load the configuration from the file
*
* @param {string} path the path to load
* @param {undefined} env should the configuration take care of the environments?
* @param {Function} callback the callback
* @return {object} the configuration object
*/
this.load = function (path, env, callback) {
// if there is no callback, throw error
if (typeof path !== "function" && typeof env !== "function" && typeof callback !== "function") throw new Error("Invalid argument");
// only the callback given
else if (typeof path === "function") {
// set the callback
callback = path;
// set the default path and env
path = env = undefined;
}
// the path and the callback given
else if (typeof path === "string" && typeof env === "function") {
// set the callback
callback = env;
// set the default env
env = undefined;
}
// the env and the callback are given
else if (typeof path == "boolean" && typeof env === "function") {
// set the callback
callback = env;
// set the env
env = path;
// set the default path
path = undefined;
}
// set the default parameters
path = path || __dirname+"/config.json";
env = env || false;
// if a configuration path is already loaded, return it calling the callback
if (configurations[path]) return callback(undefined, configurations[path]);
// read configuration file
fs.readFile(path, function (err, data) {
// is there an error?
if (err) return callback(err);
// set variables
var config = JSON.parse(data.toString());
var environment = process.env.NODE_ENV || config.environment;
var result;
// should we take care of the environment?
if (!env) {
// save this configuration
configurations[path] = config;
// done
return callback(undefined, config);
}
// if no environment set, returns error
if (!environment)
return callback(new Error("No configuration environment is set in the configuration file or NODE_ENV."));
// if no environment keys set in the config file, returns error
else if (!config[environment])
return callback(new Error("Environment '" + environment + "' is not defined in the configuration."));
// merge shared section with environment section of the config file
result = Objects.merge(config.shared, config[environment]) || {};
// set up the environment
result.environment = environment;
// save this configuration
configurations[path] = result;
// done
return callback(null, result);
});
}
};
// exports the module
module.exports = RwConfig
/**
* Initialize module
*/
/**
* Environment
*/
module.exports.env = process.env.NODE_ENV || 'dev';
/**
* Module name.
*/
module.exports.fullname = "RwConfig";
/**
* Module version.
*/
module.exports.version = '0.0.1';