-
Notifications
You must be signed in to change notification settings - Fork 879
/
file_tree.js
424 lines (350 loc) · 13.5 KB
/
file_tree.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// External
import chalk from 'chalk';
import fs from 'fs-extra';
import { globSync } from 'glob';
import JSON5 from 'json5';
import localeCompare from 'locale-compare';
import stringify from '@aitodotai/json-stringify-pretty-compact';
const withLocale = localeCompare('en-US');
// Internal
import { idgen } from './idgen.js';
import { sortObject } from './sort_object.js';
import { validate } from './validate.js';
// JSON
const treesJSON = JSON5.parse(fs.readFileSync('config/trees.json', 'utf8'));
const trees = treesJSON.trees;
const categoriesSchemaJSON = JSON5.parse(fs.readFileSync('schema/categories.json', 'utf8'));
// The code in here
// - validates data on read, generating any missing data
// - cleans data on write, sorting and lowercasing all the keys and arrays
// cache: {
// 'id': { // `cache.id` is a Map of item id -> items
// 'firstbank-978cca': {…},
// …
// },
// 'path': { // `cache.path` is an Object of t/k/v paths -> category data
// 'brands/amenity/bank': {
// 'properties': {…},
// 'items': […],
// 'templates': […]
// }
// },
export let fileTree = {
read: (cache, loco) => {
cache = cache || {};
cache.id = cache.id || new Map();
cache.path = cache.path || {};
Object.keys(trees).forEach(t => {
const tree = trees[t];
let itemCount = 0;
let fileCount = 0;
globSync(`./data/${t}/**/*`, { nodir: true }).forEach(file => {
if (/\.md$/i.test(file)) return; // ignore markdown/readme files - #7292
if (!/\.json$/.test(file)) {
console.error(chalk.red(`Error - file should have a .json extension:`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
fileCount++;
const contents = fs.readFileSync(file, 'utf8');
let input;
try {
input = JSON5.parse(contents);
} catch (jsonParseError) {
console.error(chalk.red(`Error - ${jsonParseError.message} reading:`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
// check JSON schema
validate(file, input, categoriesSchemaJSON);
let seenkv = {};
const properties = input.properties || {};
const tkv = properties.path;
const parts = tkv.split('/', 3); // tkv = "tree/key/value"
const k = parts[1];
const v = parts[2];
const kv = `${k}/${v}`;
// make sure t/k/v is unique
if (cache.path[tkv]) {
console.error(chalk.red(`Error - '${tkv}' found in multiple files.`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
} else {
cache.path[tkv] = { properties: properties, items: [], templates: [] };
}
// make sure each k/v pair appears in only one tree
const other = seenkv[kv];
if (other && other !== t) {
console.error(chalk.red(`Error - '${kv}' found in multiple trees: ${other} and ${t}.`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
} else {
seenkv[kv] = t;
}
// check and merge each item
let seenName = {};
let items = input.items || [];
items.forEach(item => {
itemCount++;
if (item.templateSource) { // It's a template item
cache.path[tkv].templates.push(item);
return;
}
// check displayName for uniqueness within this category
if (seenName[item.displayName]) {
console.error(chalk.red(`Error - duplicate displayName '${item.displayName}' in:`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
} else {
seenName[item.displayName] = true;
}
// check locationSet
let locationID;
try {
const resolved = loco.resolveLocationSet(item.locationSet);
locationID = resolved.id;
if (!resolved.feature.geometry.coordinates.length || !resolved.feature.properties.area) {
throw new Error(`locationSet ${locationID} resolves to an empty feature.`);
}
} catch (err) {
console.error(chalk.red(`Error - ${err.message} in:`));
console.error(' ' + chalk.yellow(item.displayName));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
// check tags
item.tags[k] = v; // sanity check: `k=v` must exist as a tag.
// generate id
item.id = idgen(item, tkv, locationID);
if (!item.id) {
console.error(chalk.red(`Error - Couldn't generate an id for:`));
console.error(' ' + chalk.yellow(item.displayName));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
// merge into caches
if (cache.id.has(item.id)) {
console.error(chalk.red(`Error - Duplicate id '${item.id}' in:`));
console.error(' ' + chalk.yellow(item.displayName));
console.error(' ' + chalk.yellow(file));
process.exit(1);
} else {
cache.path[tkv].items.push(item);
cache.id.set(item.id, item);
}
});
});
console.log(`${tree.emoji} ${t}:\tLoaded ${itemCount} items in ${fileCount} files`);
});
return cache;
},
write: (cache) => {
cache = cache || {};
cache.path = cache.path || {};
Object.keys(trees).forEach(t => {
const tree = trees[t];
let itemCount = 0;
let fileCount = 0;
Object.keys(cache.path).forEach(tkv => {
if (tkv.split('/')[0] !== t) return;
const category = cache.path[tkv];
const parts = tkv.split('/', 3); // tkv = "tree/key/value"
const v = parts[2];
const file = `./data/${tkv}.json`;
fileCount++;
let templateItems = category.templates || [];
let normalItems = category.items || [];
if (!templateItems.length && !normalItems.length) return; // nothing to do
templateItems = templateItems
.sort((a, b) => withLocale(a.templateSource, b.templateSource)) // sort templateItems by templateSource
.map(item => {
itemCount++;
// clean templateInclude/templateExclude
if (item.templateInclude) {
item.templateInclude = item.templateInclude.map(s => s.toLowerCase()).sort(withLocale);
}
if (item.templateExclude) {
item.templateExclude = item.templateExclude.map(s => s.toLowerCase()).sort(withLocale);
}
// clean templateSource
item.templateSource = _clean(item.templateSource);
// clean templateTags
let cleaned = {};
Object.keys(item.templateTags).forEach(k => {
const osmkey = _clean(k);
const osmval = _clean(item.templateTags[k]);
cleaned[osmkey] = osmval;
});
item.templateTags = sortObject(cleaned);
return sortObject(item);
});
normalItems = normalItems
.filter(item => !item.fromTemplate)
.sort((a, b) => withLocale(a.displayName, b.displayName)) // sort normalItems by displayName
.map(item => {
itemCount++;
// clean displayName
item.displayName = _clean(item.displayName);
// clean locationSet
let cleaned = {};
if (Array.isArray(item.locationSet.include)) {
cleaned.include = item.locationSet.include.map(_cleanLower).sort(withLocale);
} else {
cleaned.include = ['001']; // default to world
}
if (Array.isArray(item.locationSet.exclude)) {
cleaned.exclude = item.locationSet.exclude.map(_cleanLower).sort(withLocale);
}
item.locationSet = cleaned;
// clean matchNames/matchTags
['matchNames', 'matchTags'].forEach(prop => {
if (item[prop]) {
item[prop] = item[prop].map(_cleanLower).sort(withLocale);
}
});
// clean OSM tags
cleaned = {};
Object.keys(item.tags).forEach(k => {
const osmkey = _clean(k);
const osmval = _clean(item.tags[k]);
cleaned[osmkey] = osmval;
});
item.tags = sortObject(cleaned);
return sortObject(item);
});
// clean category properties
let properties = category.properties || {};
properties.exclude = properties.exclude || {};
let cleanedProps = {};
cleanedProps.path = tkv;
if (properties.skipCollection) {
cleanedProps.skipCollection = properties.skipCollection;
}
if (Array.isArray(properties.preserveTags)) {
cleanedProps.preserveTags = properties.preserveTags.map(_cleanLower).sort(withLocale);
}
cleanedProps.exclude = {};
if (Array.isArray(properties.exclude.generic)) {
cleanedProps.exclude.generic = properties.exclude.generic.map(_cleanLower).sort(withLocale);
} else {
const v2 = v.replace(/_/g, ' '); // add the value as a generic name exclude (e.g. 'restaurant')
cleanedProps.exclude.generic = [`^${v2}$`];
}
if (Array.isArray(properties.exclude.named)) {
cleanedProps.exclude.named = properties.exclude.named.map(_cleanLower).sort(withLocale);
}
// generate file
const output = {
properties: cleanedProps,
items: templateItems.concat(normalItems)
};
try {
fs.ensureFileSync(file);
fs.writeFileSync(file, stringify(output, { maxLength: 50 }) + '\n');
} catch (err) {
console.error(chalk.red(`Error - ${err.message} writing:`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
});
console.log(`${tree.emoji} ${t}:\tWrote ${itemCount} items in ${fileCount} files`);
});
function _clean(s) {
if (typeof s !== 'string') return s;
return s.trim();
}
function _cleanLower(s) {
if (typeof s !== 'string') return s;
if (/İ/.test(s)) { // Avoid toLowerCasing this one, it changes - #8261
return s.trim();
} else {
return s.trim().toLowerCase();
}
}
},
expandTemplates: (cache, loco) => {
cache = cache || {};
cache.id = cache.id || new Map();
cache.path = cache.path || {};
Object.keys(cache.path).forEach(tkv => {
const file = `./data/${tkv}.json`;
const templateItems = cache.path[tkv].templates || [];
// expand each template item into real items..
templateItems.forEach(templateItem => {
const includePatterns = (templateItem.templateInclude || []).map(s => new RegExp(s, 'i'));
const excludePatterns = (templateItem.templateExclude || []).map(s => new RegExp(s, 'i'));
const templateSource = templateItem.templateSource;
const templateTags = templateItem.templateTags;
const sourceItems = cache.path[templateSource].items;
if (!Array.isArray(sourceItems)) {
console.error(chalk.red(`Error - template item references invalid source path '${templateSource}' in:`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
sourceItems.forEach(sourceItem => {
if (includePatterns.length) {
if (!includePatterns.some(pattern => pattern.test(sourceItem.id))) return;
}
if (excludePatterns.length) {
if (excludePatterns.some(pattern => pattern.test(sourceItem.id))) return;
}
let item = JSON.parse(JSON.stringify(sourceItem)); // deep clone
delete item.matchTags; // don't copy matchTags (but do copy matchNames)
item.fromTemplate = true;
// replace tags
let tags = item.tags;
Object.keys(templateTags).forEach(osmkey => {
let tagValue = templateTags[osmkey];
if (tagValue) {
tagValue = tagValue.replace(/{(\S+)}/g, (match, token) => {
// token should contain something like 'source.tags.brand'
let replacement = '';
let props = token.split('.');
props.shift(); // Ignore first 'source'. It's just for show.
let source = sourceItem;
while (props.length) {
let prop = props.shift();
let found = source[prop];
if (typeof found === 'object' && found !== null) {
source = found;
} else {
replacement = found;
}
}
return replacement;
});
if (tagValue === 'undefined' || tagValue === 'null') {
tagValue = ''; // wipe out bogus string replacements
}
}
if (tagValue) {
tags[osmkey] = tagValue;
} else {
delete tags[osmkey];
}
});
// generate id
let locationID = loco.validateLocationSet(item.locationSet).id;
item.id = idgen(item, tkv, locationID);
if (!item.id) {
console.error(chalk.red(`Error - Couldn't generate an id for:`));
console.error(' ' + chalk.yellow(item.displayName));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
// merge into caches
if (cache.id.has(item.id)) {
// Note - in case of duplicates, it's ok to fail silently.
// It's allowed to copy multiple source categories into a single
// destination category, and there may be duplicates when we do this.
// For example `route/railway` and `route/tracks` for #8124
} else {
cache.path[tkv].items.push(item);
cache.id.set(item.id, item);
}
});
});
});
return cache;
}
};