forked from openstreetmap/iD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
271 lines (225 loc) · 7.7 KB
/
build.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var fs = require('fs'),
path = require('path'),
glob = require('glob'),
YAML = require('js-yaml'),
_ = require('./js/lib/lodash'),
d3 = require('d3'),
jsonschema = require('jsonschema'),
fieldSchema = require('./data/presets/schema/field.json'),
presetSchema = require('./data/presets/schema/preset.json'),
suggestions = require('./data/name-suggestions.json');
function readtxt(f) {
return fs.readFileSync(f, 'utf8');
}
function read(f) {
return JSON.parse(readtxt(f));
}
function r(f) {
return read(__dirname + '/data/' + f);
}
function rp(f) {
return r('presets/' + f);
}
function stringify(o) {
return JSON.stringify(o, null, 4);
}
function validate(file, instance, schema) {
var result = jsonschema.validate(instance, schema);
if (result.length) {
console.error(file + ": ");
result.forEach(function(error) {
if (error.property) {
console.error(error.property + ' ' + error.message);
} else {
console.error(error);
}
});
process.exit(1);
}
}
var translations = {
categories: {},
fields: {},
presets: {}
};
function generateCategories() {
var categories = {};
glob.sync(__dirname + '/data/presets/categories/*.json').forEach(function(file) {
var field = read(file),
id = 'category-' + path.basename(file, '.json');
translations.categories[id] = {name: field.name};
categories[id] = field;
});
return categories;
}
function generateFields() {
var fields = {};
glob.sync(__dirname + '/data/presets/fields/**/*.json').forEach(function(file) {
var field = read(file),
id = file.match(/presets\/fields\/([^.]*)\.json/)[1];
validate(file, field, fieldSchema);
var t = translations.fields[id] = {
label: field.label
};
if (field.placeholder) {
t.placeholder = field.placeholder;
}
if (field.strings) {
for (var i in field.strings) {
t[i] = field.strings[i];
}
}
fields[id] = field;
});
return fields;
}
function suggestionsToPresets(presets) {
var existing = {};
for (var key in suggestions) {
for (var value in suggestions[key]) {
for (var name in suggestions[key][value]) {
var item = key + '/' + value + '/' + name,
tags = {},
count = suggestions[key][value][name].count;
if (existing[name] && count > existing[name].count) {
delete presets[existing[name].category];
delete existing[name];
}
if (!existing[name]) {
tags = _.extend({name: name}, suggestions[key][value][name].tags);
addSuggestion(item, tags, name, count);
}
}
}
}
function addSuggestion(category, tags, name, count) {
var tag = category.split('/'),
parent = presets[tag[0] + '/' + tag[1]];
presets[category] = {
tags: parent.tags ? _.merge(tags, parent.tags) : tags,
name: name,
icon: parent.icon,
geometry: parent.geometry,
fields: parent.fields,
suggestion: true
};
existing[name] = {
category: category,
count: count
};
}
return presets;
}
function generatePresets() {
var presets = {};
glob.sync(__dirname + '/data/presets/presets/**/*.json').forEach(function(file) {
var preset = read(file),
id = file.match(/presets\/presets\/([^.]*)\.json/)[1];
validate(file, preset, presetSchema);
translations.presets[id] = {
name: preset.name,
terms: (preset.terms || []).join(',')
};
presets[id] = preset;
});
presets = _.merge(presets, suggestionsToPresets(presets));
var presetsYaml = _.cloneDeep(translations);
_.forEach(presetsYaml.presets, function(preset) {
preset.terms = "<translate with synonyms or related terms for '" + preset.name + "', separated by commas>"
});
return {
presets: presets,
presetsYaml: presetsYaml
};
}
function validateCategoryPresets(categories, presets) {
_.forEach(categories, function(category) {
if (category.members) {
category.members.forEach(function(preset) {
if (presets[preset] === undefined) {
console.error('Unknown preset: ' + preset + ' in category ' + category.name);
process.exit(1);
}
});
}
});
}
function validatePresetFields(presets, fields) {
var presets = rp('presets.json'),
fields = rp('fields.json');
_.forEach(presets, function(preset) {
if (preset.fields) {
preset.fields.forEach(function(field) {
if (fields[field] === undefined) {
console.error('Unknown preset field: ' + field + ' in preset ' + preset.name);
process.exit(1);
}
});
}
});
}
var categories = generateCategories(),
fields = generateFields(),
presets = generatePresets();
// additional consistency checks
validateCategoryPresets(categories, presets.presets);
validatePresetFields(presets.presets, fields);
// Save individual data files
fs.writeFileSync('data/presets/categories.json', stringify(categories));
fs.writeFileSync('data/presets/fields.json', stringify(fields));
fs.writeFileSync('data/presets/presets.json', stringify(presets.presets));
fs.writeFileSync('data/presets.yaml', YAML.dump({en: {presets: presets.presetsYaml}}));
// Write taginfo data
var taginfo = {
"data_format": 1,
"data_url": "https://raw.githubusercontent.com/openstreetmap/iD/master/data/taginfo.json",
"project": {
"name": "iD Editor",
"description": "Online editor for OSM data.",
"project_url": "https://github.com/openstreetmap/iD",
"doc_url": "https://github.com/openstreetmap/iD/blob/master/data/presets/README.md",
"icon_url": "https://raw.githubusercontent.com/openstreetmap/iD/master/dist/img/logo.png",
"keywords": [
"editor"
]
},
"tags": []
};
_.forEach(presets.presets, function(preset) {
if (preset.suggestion)
return;
var keys = Object.keys(preset.tags),
last = keys[keys.length - 1],
tag = {key: last};
if (!last)
return;
if (preset.tags[last] !== '*') {
tag.value = preset.tags[last];
}
taginfo.tags.push(tag);
});
fs.writeFileSync('data/taginfo.json', stringify(taginfo));
// Push changes from data/core.yaml into en.json
var core = YAML.load(fs.readFileSync('data/core.yaml', 'utf8'));
var presets = {en: {presets: translations}};
var en = _.merge(core, presets);
fs.writeFileSync('dist/locales/en.json', stringify(en.en));
fs.writeFileSync('data/data.js', 'iD.data = ' + stringify({
deprecated: r('deprecated.json'),
discarded: r('discarded.json'),
wikipedia: r('wikipedia.json'),
imperial: r('imperial.json'),
featureIcons: r('feature-icons.json'),
operations: r('operations-sprite.json'),
locales: r('locales.json'),
en: read('dist/locales/en.json'),
suggestions: r('name-suggestions.json'),
addressFormats: r('address-formats.json')
}) + ';');
fs.writeFileSync('dist/presets.js', 'iD.data.presets = ' + stringify({
presets: rp('presets.json'),
defaults: rp('defaults.json'),
categories: rp('categories.json'),
fields: rp('fields.json')
}) + ';');
fs.writeFileSync('dist/imagery.js', 'iD.data.imagery = ' + stringify(r('imagery.json')) + ';');