forked from osmlab/osm-community-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
157 lines (129 loc) · 5.24 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
/* 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);
// 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 = {};
glob.sync(__dirname + '/features/**/*.geojson').forEach(function(file) {
var contents = fs.readFileSync(file, 'utf8');
var feature = precision(rewind(JSON.parse(contents), true), 5);
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;
});
return features;
}
function generateResources(tstrings) {
var resources = {};
var files = {};
glob.sync(__dirname + '/resources/**/*.json').forEach(function(file) {
var contents = fs.readFileSync(file, 'utf8');
var resource = JSON.parse(contents);
validateFile(file, resource, resourceSchema);
prettifyFile(file, resource, contents);
var id = resource.id;
if (files[id]) {
console.error(colors.red('Error - Duplicate resource id: ') + colors.yellow(id));
console.error(' ' + colors.yellow(files[id]));
console.error(' ' + colors.yellow(file));
process.exit(1);
}
resources[id] = resource;
files[id] = file;
// collect translation strings for this resource
tstrings[id] = {
name: resource.name,
description: resource.description
};
if (resource.extendedDescription) {
tstrings[id].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[id].events = estrings;
}
}
});
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);
}
}