forked from HSLdevcom/digitransit-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
158 lines (135 loc) · 4.91 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import htmlParser from 'htm-to-json';
import defaultConfig from './configurations/config.default';
import configMerger from './util/configMerger';
import { boundWithMinimumAreaSimple } from './util/geo-utils';
const configs = {}; // cache merged configs for speed
const themeMap = {};
if (defaultConfig.themeMap) {
Object.keys(defaultConfig.themeMap).forEach(theme => {
themeMap[theme] = new RegExp(defaultConfig.themeMap[theme], 'i'); // str to regex
});
}
function addMetaData(config) {
let stats;
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
stats = require(`../_static/assets/iconstats-${config.CONFIG}`);
} catch (error) {
return;
}
const html = stats.html.join(' ');
const APP_PATH =
config.APP_PATH && config.APP_PATH !== '' ? `${config.APP_PATH}'/'` : '/';
const appPathPrefix = config.URL.ASSET_URL || APP_PATH;
htmlParser.convert_html_to_json(html, (err, data) => {
if (!err) {
data.meta.forEach(e => {
// eslint-disable-next-line no-param-reassign
delete e.innerHTML;
if (
e.name === 'msapplication-config' ||
e.name === 'msapplication-TileImage'
) {
// eslint-disable-next-line no-param-reassign
e.content = `${appPathPrefix}${stats.outputFilePrefix}${e.content}`; // fix path bug
} else if (e.name === 'theme-color') {
// eslint-disable-next-line no-param-reassign
e.content = '#fff';
} else if (e.name === 'apple-mobile-web-app-status-bar-style') {
// eslint-disable-next-line no-param-reassign
e.content = 'white';
}
});
data.link.forEach(e => {
// eslint-disable-next-line no-param-reassign
delete e.innerHTML;
if (config.URL.ASSET_URL && e.href.startsWith('/icons')) {
e.href = appPathPrefix + e.href;
}
});
// eslint-disable-next-line no-param-reassign
config.metaData = data;
// eslint-disable-next-line no-param-reassign
config.iconPath = stats.outputFilePrefix;
}
});
}
export function getNamedConfiguration(configName) {
if (!configs[configName]) {
let additionalConfig;
if (configName !== 'default') {
// eslint-disable-next-line global-require, import/no-dynamic-require
additionalConfig = require(`./configurations/config.${configName}`)
.default;
}
// use cached baseConfig that is potentially patched in server start up
// for merge if one is configured
const baseConfig = configs[process.env.BASE_CONFIG];
const config = baseConfig
? configMerger(baseConfig, additionalConfig)
: configMerger(defaultConfig, additionalConfig);
if (config.useSearchPolygon && config.areaPolygon) {
// pass poly as 'lon lat, lon lat, lon lat ...' sequence
const pointsParam = config.areaPolygon
.map(p => `${p[0]} ${p[1]}`)
.join(',');
config.searchParams = config.searchParams || {};
config.searchParams['boundary.polygon'] = pointsParam;
}
Object.keys(config.modePolygons).forEach(mode => {
const boundingBoxes = [];
config.modePolygons[mode].forEach(polygon => {
boundingBoxes.push(boundWithMinimumAreaSimple(polygon));
});
config.modeBoundingBoxes = config.modeBoundingBoxes || {};
config.modeBoundingBoxes[mode] = boundingBoxes;
});
Object.keys(config.realTimePatch).forEach(realTimeKey => {
config.realTime[realTimeKey] = {
...(config.realTime[realTimeKey] || {}),
...config.realTimePatch[realTimeKey],
};
});
addMetaData(config); // add dynamic metadata content
if (!process.env.OIDC_CLIENT_ID) {
// disable user account access if backend is not available
config.showLogin = false;
}
const appPathPrefix = config.URL.ASSET_URL || '';
if (config.geoJson && Array.isArray(config.geoJson.layers)) {
for (let i = 0; i < config.geoJson.layers.length; i++) {
const layer = config.geoJson.layers[i];
layer.url = appPathPrefix + layer.url;
}
}
if (config.geoJson && config.geoJson.zones) {
config.geoJson.zones.url = appPathPrefix + config.geoJson.zones.url;
}
configs[configName] = config;
}
return configs[configName];
}
export function getConfiguration(req) {
let configName = process.env.CONFIG || process.env.BASE_CONFIG || 'default';
let host;
if (req) {
host =
(req.headers['x-forwarded-host'] &&
req.headers['x-forwarded-host'].split(':')[0]) ||
(req.headers.host && req.headers.host.split(':')[0]) ||
'localhost';
}
if (
host &&
process.env.NODE_ENV !== 'development' &&
(process.env.CONFIG === '' || !process.env.CONFIG)
) {
// no forced CONFIG, map dynamically
Object.keys(themeMap).forEach(theme => {
if (themeMap[theme].test(host)) {
configName = theme;
}
});
}
return getNamedConfiguration(configName);
}