-
Notifications
You must be signed in to change notification settings - Fork 5
/
field-options.js
51 lines (45 loc) · 1.47 KB
/
field-options.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
/**
* Script to add value options to all presets without default values. This is limited to combo fields at the moment
* This script is part of `yarn build-presets`
*/
const request = require('request')
const fs = require('fs')
const prettyStringify = require('json-stringify-pretty-compact')
const fields = JSON.parse(fs.readFileSync('app/presets/fields.json', { encoding: 'utf-8' }))
const tasks = []
Object.keys(fields.fields).forEach(key => {
const field = fields.fields[key]
if (field.type === 'combo' && (!field.options && !field.strings)) {
console.error('Preparing field options for key:', field.key)
tasks.push(getValuesFromTaginfo(field.key)
.then(response => {
const options = []
response.data.forEach((d) => {
options.push(d.value)
})
field.options = options
})
.catch(err => {
console.error(err)
}))
}
})
Promise.all(tasks)
.then(() => {
fs.writeFileSync('app/presets/fields.json', prettyStringify(fields, { maxLength: 9999 }), { encoding: 'utf8', flag: 'w' })
})
.catch(err => {
console.error(err)
})
function getValuesFromTaginfo (key) {
const url = 'https://taginfo.openstreetmap.org/api/4/key/values?lang=en&page=1&query=&rp=25&sortname=count_ways&sortorder=desc&key=' + key
return new Promise((resolve, reject) => {
request.get(url, (err, response, body) => {
if (err) {
reject(err)
} else {
resolve(JSON.parse(body))
}
})
})
}