forked from osmlab/osm-community-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
210 lines (170 loc) · 7.04 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
/* eslint-disable no-console */
const colors = require('colors/safe');
const fs = require('fs');
const glob = require('glob');
const precision = require('geojson-precision');
const rewind = require('geojson-rewind');
const Validator = require('jsonschema').Validator;
const shell = require('shelljs');
const prettyStringify = require('json-stringify-pretty-compact');
const YAML = require('js-yaml');
const geojsonSchema = require('./schema/geojson.json');
const featureSchema = require('./schema/feature.json');
const resourceSchema = require('./schema/resource.json');
var v = new Validator();
v.addSchema(geojsonSchema, 'http://json.schemastore.org/geojson.json');
buildAll();
function buildAll() {
console.log('building data');
console.time(colors.green('data built'));
// Start clean
shell.rm('-f', ['dist/*.json', 'dist/*.js', 'i18n/en.yaml']);
var tstrings = {}; // translation strings
var features = generateFeatures();
var resources = generateResources(tstrings, features);
// Save individual data files
fs.writeFileSync('dist/features.json', prettyStringify({ features: features }) );
fs.writeFileSync('dist/features.min.json', JSON.stringify({ features: features }) );
fs.writeFileSync('dist/resources.json', prettyStringify({ resources: resources }) );
fs.writeFileSync('dist/resources.min.json', JSON.stringify({ resources: resources }) );
fs.writeFileSync('i18n/en.yaml', YAML.safeDump({ en: tstrings }, { lineWidth: -1 }) );
console.timeEnd(colors.green('data built'));
}
function generateFeatures() {
var features = {};
var files = {};
process.stdout.write('Features:');
glob.sync(__dirname + '/features/**/*.geojson').forEach(function(file) {
var contents = fs.readFileSync(file, 'utf8');
var parsed;
try {
parsed = JSON.parse(contents);
} catch (jsonParseError)
{
console.error(colors.red('Error - ' + jsonParseError.message + ' in:'));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
var feature = precision(rewind(parsed, true), 5);
var 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) {
if (feature.id && !fc[0].id) {
fc[0].id = feature.id;
}
feature = fc[0];
}
validateFile(file, feature, featureSchema);
prettifyFile(file, feature, contents);
var id = feature.id;
if (files[id]) {
console.error(colors.red('Error - Duplicate feature id: ') + colors.yellow(id));
console.error(' ' + colors.yellow(files[id]));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
features[id] = feature;
files[id] = file;
process.stdout.write(colors.green('✓'));
});
process.stdout.write(Object.keys(files).length + '\n');
return features;
}
function generateResources(tstrings, features) {
var resources = {};
var files = {};
process.stdout.write('Resources:');
glob.sync(__dirname + '/resources/**/*.json').forEach(function(file) {
var contents = fs.readFileSync(file, 'utf8');
var resource;
try {
resource = JSON.parse(contents);
} catch (jsonParseError) {
console.error(colors.red('Error - ' + jsonParseError.message + ' in:'));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
validateFile(file, resource, resourceSchema);
prettifyFile(file, resource, contents);
var resourceId = resource.id;
if (files[resourceId]) {
console.error(colors.red('Error - Duplicate resource id: ') + colors.yellow(resourceId));
console.error(' ' + colors.yellow(files[resourceId]));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
var featureId = resource.featureId;
if (featureId && !features[featureId]) {
console.error(colors.red('Error - Unknown feature id: ') + colors.yellow(featureId));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
if (!featureId && !/\/resources\/world/.test(file)) {
console.error(colors.red('Error - feature id is required for non-worldwide resource:'));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
resources[resourceId] = resource;
files[resourceId] = file;
// collect translation strings for this resource
tstrings[resourceId] = {
name: resource.name,
description: resource.description
};
if (resource.extendedDescription) {
tstrings[resourceId].extendedDescription = resource.extendedDescription;
}
// Validate event dates and collect strings from upcoming events (where `i18n=true`)
if (resource.events) {
var estrings = {};
for (var i = 0; i < resource.events.length; i++) {
var event = resource.events[i];
// check date
var d = new Date(event.when);
if (isNaN(d.getTime())) {
console.error(colors.red('Error - Bad date: ') + colors.yellow(event.when));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
if (!event.i18n) continue;
if (estrings[event.id]) {
console.error(colors.red('Error - Duplicate event id: ') + colors.yellow(event.id));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
estrings[event.id] = {
name: event.name,
description: event.description,
where: event.where
};
}
if (Object.keys(estrings).length) {
tstrings[resourceId].events = estrings;
}
}
process.stdout.write(colors.green('✓'));
});
process.stdout.write(Object.keys(files).length + '\n');
return resources;
}
function validateFile(file, resource, schema) {
var validationErrors = v.validate(resource, schema).errors;
if (validationErrors.length) {
console.error(colors.red('Error - Schema validation:'));
console.error(' ' + colors.yellow(file + ': '));
validationErrors.forEach(function(error) {
if (error.property) {
console.error(' ' + colors.yellow(error.property + ' ' + error.message));
} else {
console.error(' ' + colors.yellow(error));
}
});
process.exit(1);
}
}
function prettifyFile(file, object, contents) {
var pretty = prettyStringify(object);
if (pretty !== contents) {
fs.writeFileSync(file, pretty);
}
}