This repository has been archived by the owner on Mar 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
163 lines (135 loc) · 4 KB
/
index.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var fs = require('fs')
, configFiles = {};
if (!process.env.ENV_TYPE) {
console.error('#### NoobConfig:#############################################################');
console.error('# The enviroment variable "ENV_TYPE" must be set according #');
console.error('# to your /config/*.ini for theses script to work correctly #');
console.error('#### NoobConfig:#############################################################');
process.exit(0);
}
function get (filename) {
if (configFiles.hasOwnProperty(filename)) {
return configFiles[filename].getEnv(process.env.ENV_TYPE);
}
var filepath = '/etc/noob/'+ filename +'.ini';
if (!fs.existsSync(filepath)) {
filepath = 'config/'+filename+'.ini';
if (!fs.existsSync(filepath)) {
return null;
}
}
var file = fs.readFileSync(filepath, 'utf-8'),
lines = file.split("\n"),
matches = [],
section = '',
isHeaderLine = false;
var config = new ConfigFile();
for (var i=0; i < lines.length; i++) {
// if empty line or commented out line
if (lines[i] == '' || lines[i].match(/^;/)) {
continue;
}
isHeaderLine = false;
// If we have a section that does not "extends" from another
matches = lines[i].match(/\[([a-z]{4,})\]/i);
if (matches) {
section = matches[1];
isHeaderLine = true;
}
// If we have a section that extends from another
matches = lines[i].match(/\[([a-z]{4,})\s:\s([a-z]{4,})\]/i);
if (matches) {
section = matches[1];
config.addExtend(section, matches[2]);
isHeaderLine = true;
}
if (!isHeaderLine) {
config.addLineToSection(section, lines[i]);
}
}
configFiles[filename] = config;
return configFiles[filename].getEnv(process.env.ENV_TYPE);
}
function ConfigFile () {
this.sections = {};
this.exts = {};
}
ConfigFile.prototype.addExtend = function (name, extendName) {
if (!this.exts.hasOwnProperty(name)) {
this.exts[name] = '';
}
this.exts[name] = extendName;
}
ConfigFile.prototype.addLineToSection = function (name, line) {
if (!this.sections.hasOwnProperty(name)) {
this.sections[name] = {};
}
var match = line.match(/^([a-z0-9\._]*)\s+\=\s+(.*)$/i)
, currentArray = []
if (!match) {
console.error('#### NoobConfig: Invalid characters for line:', line);
return;
}
var parts = match[1].split(".")
, value = match[2].replace(/^\s+|\s+$/, "").replace(/^"/, "").replace(/"$/, "");
for (var i=0; i < parts.length; i++) {
var index = parts[i].replace(/^\s+|\s+$/, "");
if (i == 0) {
currentArray = this.sections[name];
}
if (i == (parts.length-1)) {
if (value.match(/^\d+$/)) {
value = parseFloat(value);
}
else if (value.match(/^true$/i)) {
value = true;
}
else if (value.match(/^false$/i)) {
value = false;
}
else if (value.match(/^null$/i)) {
value = null;
}
else {
value = value.toString();
}
currentArray[ index ] = value;
}
else if (!currentArray.hasOwnProperty(index)) {
currentArray = currentArray[ index ] = {};
}
else {
currentArray = currentArray[ index ];
}
}
}
ConfigFile.prototype.getEnv = function (env) {
if (!this.sections.hasOwnProperty(env) && !this.sections.hasOwnProperty(this.exts[env]) ) {
console.log('#### No configs for enviroment: ', env);
process.exit(0);
}
var configs = [];
if (this.exts.hasOwnProperty(env)) {
configs = this.sections[this.exts[env]];
}
return MergeRecursive(configs, this.sections[env]);
}
function MergeRecursive(obj1, obj2) {
for (var p in obj2) {
try {
// Property in destination object set; update its value.
if ( obj2[p].constructor==Object ) {
obj1[p] = MergeRecursive(obj1[p], obj2[p]);
}
else {
obj1[p] = obj2[p];
}
}
catch(e) {
// Property in destination object not set; create it and set its value.
obj1[p] = obj2[p];
}
}
return obj1;
}
module.exports = get