forked from kersing/multitech-installer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge.js
84 lines (79 loc) · 2.38 KB
/
merge.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
// Node-JS program to merge TheThingsNetwork configuration with changes required
// for MultiTech LoRaWAN gateway.
//
// Copyright by Jac Kersing <[email protected]>
//
// Use of this source code is governed by the MIT license that can be found in the LICENSE file
// at github.com/kersing/multitech-installer
//
function substitute(path, prop, value) {
var element = 'ttnjson'
for (var i = 0; i < path.length; i++) {
element = element + "." + path[i];
}
element = element+"."+prop;
if (typeof(value) == "string") {
eval(element + "=\"" + value +"\"");
} else {
eval(element + "=" + value);
}
}
function substproperties(node, path) {
if (node instanceof Array) {
for (var i=0; i<node.length; i++) {
if (typeof node[i] == "object" && node[i]) {
path.push(prop);
substproperties(node[prop],path);
path.pop();
} else {
substitute(path, prop, node[prop]);
}
}
} else {
for (var prop in node) {
if (typeof node[prop] == "object" && node[prop]) {
path.push(prop);
substproperties(node[prop],path);
path.pop();
} else {
substitute(path, prop, node[prop]);
}
}
}
}
ttncfgname='/var/config/lora/ttn_global_conf.json';
overridesname='/var/config/lora/multitech_overrides.json';
outputname='/var/config/lora/global_conf.json';
if (process.argv.length > 2) {
if (process.argv[2] == '-h' || process.argv[2] == '--help') {
console.log('Usage: node merge.js [ttn global_conf.json] [multitech_overrides.json] [output global_conf.json]');
process.exit(0);
}
ttncfgname = process.argv[2];
if (process.argv.length > 3) {
overridesname=process.argv[3];
if (process.argv.length > 4) {
outputname=process.argv[4];
}
}
}
var fs = require('fs');
ttnconfig = fs.readFileSync(ttncfgname, 'utf8');
config = ttnconfig;
// skip all comments in the input global_conf.json file
found=config.indexOf("/*");
while (found != -1) {
endpos = config.indexOf("*/");
if (endpos != -1) {
config = config.substring(0,found) + config.substring(endpos+2);
}
found=config.indexOf("/*");
}
ttnjson = JSON.parse(config);
start=overridesname.substring(0,1);
if (start != '.' && start != '/') {
overridesname = './' + overridesname;
}
mtoverrides = require(overridesname);
substproperties(mtoverrides, new Array());
fs.writeFile(outputname, JSON.stringify(ttnjson,null,4));