-
Notifications
You must be signed in to change notification settings - Fork 42
/
environment-vars.js
93 lines (79 loc) · 2.06 KB
/
environment-vars.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
"use strict";
var properties = require ("../../lib");
/*
The external vars can be used to dynamically assign the value of a property
using environment vars.
The basic idea is to have one file where we define all the properties that need
our system to work. Then we need two more files, one for the development
environment an another for the production. These two files contain just the
minimum properties that differenciate the development from the production
environment.
NODE_ENV=development node environment-vars.js
NODE_ENV=production node environment-vars.js
*/
var file;
if (process.env.NODE_ENV === "production"){
file = "environment-vars-production";
}else{
file = "environment-vars-development";
}
//First load the specific environment data
properties.parse (file, { path: true, sections: true }, function (error, env){
if (error) return console.error (error);
//Load the main configuration using as external vars the specific environment
//properties
//Tip: If the namespaces option is enabled you can read a value like this:
//${web.hostname}, so you can pass the env object as an external var and read
//from it using the dot separated keys nomenclature
var options = {
path: true,
variables: true,
sections: true,
namespaces: true,
vars: env
};
properties.parse ("environment-vars", options, function (error, p){
if (error) return console.error (error);
console.log (p);
/*
NODE_ENV=development
{
app: {
name: "App",
version: "0.0.1"
},
web: {
hostname: "localhost",
port: 1337
},
db: {
hostname: "10.10.10.10",
port: 1234,
pool: {
min: 5,
max: 10
}
}
}
NODE_ENV=production
{
app: {
name: "App",
version: "0.0.1"
},
web: {
hostname: "0.0.0.0",
port: 80
},
db: {
hostname: "10.10.10.20",
port: 1234,
pool: {
min: 5,
max: 10
}
}
}
*/
});
});