-
Notifications
You must be signed in to change notification settings - Fork 159
/
build_index.js
392 lines (319 loc) · 13.2 KB
/
build_index.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
// External
import chalk from 'chalk';
import fs from 'node:fs';
import glob from 'glob';
import JSON5 from 'json5';
import jsonschema from 'jsonschema';
import LocationConflation from '@ideditor/location-conflation';
import localeCompare from 'locale-compare';
import path from 'node:path';
import geojsonArea from '@mapbox/geojson-area';
import geojsonBounds from 'geojson-bounds';
import geojsonPrecision from 'geojson-precision';
import geojsonRewind from '@mapbox/geojson-rewind';
import stringify from '@aitodotai/json-stringify-pretty-compact';
import YAML from 'js-yaml';
const withLocale = localeCompare('en-US');
// Internal
import { resolveStrings } from '../lib/resolve_strings.js';
import { sortObject } from '../lib/sort_object.js';
import { simplify } from '../lib/simplify.js';
import { writeFileWithMeta } from '../lib/write_file_with_meta.js';
// JSON
import geojsonSchemaJSON from '../schema/geojson.json' assert {type: 'json'};
import featureSchemaJSON from '../schema/feature.json' assert {type: 'json'};
import resourceSchemaJSON from '../schema/resource.json' assert {type: 'json'};
const Validator = jsonschema.Validator;
let v = new Validator();
v.addSchema(geojsonSchemaJSON, 'http://json.schemastore.org/geojson.json');
let _tstrings = { _defaults: {}, _communities: {} };
let _defaults = {};
let _features = {};
let _resources = {};
buildAll();
function buildAll() {
const START = '🏗 ' + chalk.yellow('Building data...');
const END = '👍 ' + chalk.green('data built');
console.log('');
console.log(START);
console.time(END);
// Defaults
_defaults = collectDefaults();
// Features
_features = collectFeatures();
const featureCollection = { type: 'FeatureCollection', features: _features };
writeFileWithMeta('dist/featureCollection.json', stringify(featureCollection, { maxLength: 9999 }) + '\n');
// Resources
_resources = collectResources(featureCollection);
writeFileWithMeta('dist/resources.json', stringify({ resources: sortObject(_resources) }, { maxLength: 9999 }) + '\n');
// Translations
_tstrings._defaults = sortObject(_tstrings._defaults);
_tstrings._communities = sortObject(_tstrings._communities);
fs.writeFileSync('i18n/en.yaml', YAML.dump({ en: sortObject(_tstrings) }, { lineWidth: -1 }) );
console.timeEnd(END);
}
//
// Gather default strings from `defaults.json`
//
function collectDefaults() {
process.stdout.write('📦 Defaults: ');
let contents = fs.readFileSync('./defaults.json', 'utf8');
let defaults;
try {
defaults = JSON5.parse(contents).defaults;
} catch (jsonParseError) {
console.error(chalk.red(`Error - ${jsonParseError.message} in:`));
console.error(' ' + chalk.yellow('./defaults.json'));
process.exit(1);
}
Object.keys(defaults).forEach(k => _tstrings._defaults[k] = defaults[k]);
process.stdout.write(chalk.green('✓') + ' 1\n');
return defaults;
}
//
// Gather feature files from `/features/**/*.geojson`
//
function collectFeatures() {
let features = [];
let files = {};
process.stdout.write('📦 Features: ');
glob.sync('./features/**/*', { nodir: true }).forEach(file => {
if (!/\.geojson$/.test(file)) {
console.error(chalk.red(`Error - file should have a .geojson extension:`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
const contents = fs.readFileSync(file, 'utf8');
let parsed;
try {
parsed = JSON5.parse(contents);
} catch (jsonParseError) {
console.error(chalk.red(`Error - ${jsonParseError.message} in:`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
let feature = geojsonPrecision(geojsonRewind(parsed, true), 5);
let fc = feature.features;
// A FeatureCollection with a single feature inside (geojson.io likes to make these).
if (feature.type === 'FeatureCollection' && Array.isArray(fc) && fc.length === 1) {
feature = fc[0];
}
// Warn if this feature is so small it would better be represented as a circular area.
let area = geojsonArea.geometry(feature.geometry) / 1e6; // m² to km²
area = Number(area.toFixed(2));
if (area < 2000) {
const extent = geojsonBounds.extent(feature);
const lon = ((extent[0] + extent[2]) / 2).toFixed(4);
const lat = ((extent[1] + extent[3]) / 2).toFixed(4);
console.warn('');
console.warn(chalk.yellow(`Warning for ` + chalk.yellow(file) + `:`));
console.warn(chalk.yellow(`GeoJSON feature for small area (${area} km²). Consider circular include location instead: [${lon}, ${lat}]`));
}
// use the filename as the feature.id
const id = path.basename(file).toLowerCase();
feature.id = id;
// sort properties
let obj = {};
if (feature.type) { obj.type = feature.type; }
if (feature.id) { obj.id = feature.id; }
if (feature.properties) { obj.properties = feature.properties; }
if (feature.geometry) { obj.geometry = feature.geometry; }
feature = obj;
validateFile(file, feature, featureSchemaJSON);
prettifyFile(file, feature, contents);
if (files[id]) {
console.error(chalk.red('Error - Duplicate filenames: ') + chalk.yellow(id));
console.error(' ' + chalk.yellow(files[id]));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
features.push(feature);
files[id] = file;
process.stdout.write(chalk.green('✓'));
});
process.stdout.write(' ' + Object.keys(files).length + '\n');
return features;
}
//
// Gather resource files from `/resources/**/*.json`
//
function collectResources(featureCollection) {
let resources = {};
let files = {};
const loco = new LocationConflation(featureCollection);
process.stdout.write('📦 Resources: ');
glob.sync('./resources/**/*.json', { nodir: true }).forEach(file => {
if (!/\.json$/.test(file)) {
console.error(chalk.red(`Error - file should have a .json extension:`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
let contents = fs.readFileSync(file, 'utf8');
let item;
try {
item = JSON5.parse(contents);
} catch (jsonParseError) {
console.error(chalk.red(`Error - ${jsonParseError.message} in:`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
// Check locationSet
item.locationSet = item.locationSet || {};
try {
const resolved = loco.resolveLocationSet(item.locationSet);
if (!resolved.feature.geometry.coordinates.length || !resolved.feature.properties.area) {
throw new Error(`locationSet ${resolved.id} resolves to an empty feature.`);
}
} catch (err) {
console.error(chalk.red(`Error - ${err.message} in:`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
// Check strings
item.strings = item.strings || {};
convertURLs(item); // first: perform trivial url conversions in-place
let resolvedStrings;
try {
resolvedStrings = resolveStrings(item, _defaults);
if (!resolvedStrings.name) { throw new Error('Cannot resolve a value for name'); }
if (!resolvedStrings.description) { throw new Error('Cannot resolve a value for description'); }
if (!resolvedStrings.url) { throw new Error('Cannot resolve a value for url'); }
} catch (err) {
console.error(chalk.red(`Error - ${err.message} in:`));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
// Clean and sort the properties for consistency, save them that way.
let obj = {};
if (item.id) { obj.id = item.id; }
if (item.type) { obj.type = item.type; }
if (item.account) { obj.account = item.account; }
obj.locationSet = {};
if (item.locationSet.include) { obj.locationSet.include = item.locationSet.include; }
if (item.locationSet.exclude) { obj.locationSet.exclude = item.locationSet.exclude; }
if (item.languageCodes) { obj.languageCodes = item.languageCodes.sort(withLocale); }
if (item.order) { obj.order = item.order; }
obj.strings = {};
if (item.strings.community) { obj.strings.community = item.strings.community; }
if (item.strings.name) { obj.strings.name = item.strings.name; }
if (item.strings.description) { obj.strings.description = item.strings.description; }
if (item.strings.extendedDescription) { obj.strings.extendedDescription = item.strings.extendedDescription; }
if (item.strings.signupUrl) { obj.strings.signupUrl = item.strings.signupUrl; }
if (item.strings.url) { obj.strings.url = item.strings.url; }
// obj.resolved = resolvedStrings;
if (item.contacts) { obj.contacts = item.contacts; }
if (item.events) { obj.events = item.events; }
item = obj;
validateFile(file, item, resourceSchemaJSON);
prettifyFile(file, item, contents);
const itemID = item.id;
if (files[itemID]) {
console.error(chalk.red('Error - Duplicate resource id: ') + chalk.yellow(itemID));
console.error(' ' + chalk.yellow(files[itemID]));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
resources[itemID] = item;
files[itemID] = file;
// Collect translation strings for this resource
let translateStrings = {};
if (item.strings.community) {
const communityID = simplify(item.strings.community);
_tstrings._communities[communityID] = item.strings.community;
}
if (item.strings.name) { translateStrings.name = item.strings.name; }
if (item.strings.description) { translateStrings.description = item.strings.description; }
if (item.strings.extendedDescription) { translateStrings.extendedDescription = item.strings.extendedDescription; }
// Validate event dates and collect translation strings from upcoming events (where `i18n=true`)
if (item.events) {
let estrings = {};
for (let i = 0; i < item.events.length; i++) {
const event = item.events[i];
// check date
const d = new Date(event.when);
if (isNaN(d.getTime())) {
console.error(chalk.red('Error - Bad date: ') + chalk.yellow(event.when));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
if (!event.i18n) continue;
if (estrings[event.id]) {
console.error(chalk.red('Error - Duplicate event id: ') + chalk.yellow(event.id));
console.error(' ' + chalk.yellow(file));
process.exit(1);
}
estrings[event.id] = {
name: event.name,
description: event.description,
where: event.where
};
}
if (Object.keys(estrings).length) {
translateStrings.events = estrings;
}
}
if (Object.keys(translateStrings).length) {
_tstrings[itemID] = translateStrings;
}
process.stdout.write(chalk.green('✓'));
});
process.stdout.write(' ' + Object.keys(files).length + '\n');
return resources;
}
function convertURLs(item) {
const url = item.strings && item.strings.url;
if (!url) return;
let matchUrl;
if (item.type === 'aparat') {
matchUrl = url.match(/aparat.com\/([\-A-Za-z0-9_.]+)\/?$/i);
} else if (item.type === 'discord') {
matchUrl = url.match(/discord.gg\/(\w+)\/?$/i);
} else if (item.type === 'facebook') {
matchUrl = url.match(/facebook.com\/([\-A-Za-z0-9_.]+)\/?$/i);
} else if (item.type === 'forum') {
matchUrl = url.match(/forum.openstreetmap.org\/viewforum.php\?id=(\d+)\/?$/i);
} else if (item.type === 'github') {
matchUrl = url.match(/github.com\/([\-A-Za-z0-9_.]+)\/?$/i);
} else if (item.type === 'gitlab') {
matchUrl = url.match(/gitlab.com\/([\-A-Za-z0-9_.]+)\/?$/i);
} else if (item.type === 'irc') {
matchUrl = url.match(/webchat.oftc.net\/\?channels=([\-A-Za-z0-9_]+)\/?$/i);
} else if (item.type === 'linkedin') {
matchUrl = url.match(/linkedin.com\/company\/([\-A-Za-z0-9_.]+)\/?$/i);
} else if (item.type === 'mailinglist') {
matchUrl = url.match(/lists.openstreetmap.org\/listinfo\/([\-A-Za-z0-9_]+)\/?$/i);
} else if (item.type === 'meetup') {
matchUrl = url.match(/meetup.com\/([\-A-Za-z0-9_]+)\/?$/i);
} else if (item.type === 'telegram') {
matchUrl = url.match(/t.me\/([\-A-Za-z0-9_]+)\/?$/i);
} else if (item.type === 'twitter') {
matchUrl = url.match(/twitter.com\/([\-A-Za-z0-9_.]+)\/?$/i);
} else if (item.type === 'youtube') {
matchUrl = url.match(/youtube.com\/channel\/([\-A-Za-z0-9_.]+)\/?$/i);
}
if (matchUrl) {
item.account = matchUrl[1];
delete item.strings.url;
}
}
function validateFile(file, resource, schema) {
const validationErrors = v.validate(resource, schema).errors;
if (validationErrors.length) {
console.error(chalk.red('Error - Schema validation:'));
console.error(' ' + chalk.yellow(file + ': '));
validationErrors.forEach(error => {
if (error.property) {
console.error(' ' + chalk.yellow(error.property + ' ' + error.message));
} else {
console.error(' ' + chalk.yellow(error));
}
});
process.exit(1);
}
}
function prettifyFile(file, object, contents) {
const pretty = stringify(object, { maxLength: 70 }) + '\n';
if (pretty !== contents) {
fs.writeFileSync(file, pretty);
}
}