-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfiguration.js
89 lines (81 loc) · 2.32 KB
/
configuration.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
//
// Shared configuration entry point, used by server and webpack (client).
//
const path = require("path");
const fs = require("fs");
const YAML = require("yaml");
(function initialize() {
// We use env. prefix as this allow us to pass the argument
// through the webpack.
let configurationPath = readProperty("configFileLocation");
if (configurationPath === undefined) {
configurationPath = path.join(__dirname, "configuration.yaml");
}
const file = fs.readFileSync(configurationPath, "utf8");
const configuration = YAML.parse(file)["dcat-ap-viewer"];
const client = sanitizeConfiguration(configuration["client"]);
// As YAML import arrays as dictionaries we need to sanitize 'profiles'.
client.profiles = Object.values(client.profiles);
module.exports = {
"port": configuration["port"],
"serveStaticContent": configuration["serve-static-content"] || false,
"providers": configuration["providers"] || [],
"helmet": configuration["helmet"],
/**
* As this is provided to the profile, it may have custom properties
* thus we only do some basic sanitization.
*/
"client": client,
};
})();
function readProperty(option) {
const argument = readProgramArgument(option);
if (argument !== undefined) {
return argument;
} else if (process.env[option] !== undefined) {
return process.env[option];
} else {
return undefined;
}
}
function readProgramArgument(name) {
let output = undefined;
process.argv.forEach((value) => {
const line = value.split("=");
if (line.length !== 2) {
return;
}
if (line[0] === name) {
output = line[1];
}
});
return output;
}
function sanitizeConfiguration(configuration) {
const result = {};
for (const [key, value] of Object.entries(configuration)) {
if (typeof value === "object") {
result[sanitizeConfigurationKey(key)] = sanitizeConfiguration(value);
} else {
result[sanitizeConfigurationKey(key)] = value;
}
}
return result;
}
function sanitizeConfigurationKey(key) {
let result = "";
let capitalizeNext = false;
for (const char of key) {
if (char === "-") {
capitalizeNext = true;
continue;
}
if (capitalizeNext) {
result += char.toLocaleUpperCase();
capitalizeNext = false;
continue;
}
result += char;
}
return result;
}