-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathreplace.js
More file actions
executable file
·47 lines (39 loc) · 1.82 KB
/
replace.js
File metadata and controls
executable file
·47 lines (39 loc) · 1.82 KB
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
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
// Define filepaths
const leafletOptions = path.join(__dirname, '..', 'src', 'leaflet_options.js')
const debug = path.join(__dirname, '..', 'debug', 'index.html')
// Read & Replace options
for (const filepath of [leafletOptions, debug]) {
let options = fs.readFileSync(filepath, 'utf8')
// Define Environment variables
const ZOOM = process.env.OSRM_ZOOM || 13
const LABEL = process.env.OSRM_LABEL || 'Car (fastest)'
const CENTER = process.env.OSRM_CENTER || '38.8995, -77.0269'
const BACKEND = process.env.OSRM_BACKEND || 'https://router.project-osrm.org'
const LANGUAGE = process.env.OSRM_LANGUAGE || 'en'
const NOMINATIM = process.env.NOMINATIM_URL || 'https://nominatim.openstreetmap.org'
const DEFAULT_LAYER = process.env.OSRM_DEFAULT_LAYER || 'streets'
// Edit Leaflet Options
if (NOMINATIM) options = options.replace(/\/\/nominatim\.openstreetmap\.org/, NOMINATIM)
if (BACKEND) options = options.replace(/http[s]?:\/\/router\.project-osrm\.org/, BACKEND)
if (LABEL) options = options.replace('Car (fastest)', LABEL)
if (ZOOM) options = options.replace('zoom: 13', `zoom: ${ZOOM}`)
if (LANGUAGE) options = options.replace(`language: 'en'`, `language: '${LANGUAGE}'`)
if (DEFAULT_LAYER) options = options.replace('layer: streets', `layer: ${DEFAULT_LAYER}`)
if (CENTER) {
const latLng = CENTER.split(/[, ]+/)
const lat = latLng[0];
const lng = latLng[1];
const lnglat = [lng, lat].join(',')
const latlng = [lat, lng].join(',')
// Mapbox uses LngLat
if (options.match('-122.4536, 37.796')) options = options.replace('-122.4536, 37.796', lnglat)
// Leaflet uses LatLng
else options = options.replace('38.8995,-77.0269', latlng)
}
// Save Leaflet Options
fs.writeFileSync(filepath, options)
}