1
1
/* eslint-disable no-console */
2
+ const calcArea = require ( '@mapbox/geojson-area' ) ;
2
3
const colors = require ( 'colors/safe' ) ;
3
4
const fs = require ( 'fs' ) ;
4
5
const glob = require ( 'glob' ) ;
6
+ const isValidLocation = require ( './lib/isValidLocation.js' ) ;
5
7
const path = require ( 'path' ) ;
6
-
7
- const Validator = require ( 'jsonschema' ) . Validator ;
8
- const shell = require ( 'shelljs' ) ;
9
- const prettyStringify = require ( 'json-stringify-pretty-compact' ) ;
10
- const YAML = require ( 'js-yaml' ) ;
11
-
12
- const calcArea = require ( '@mapbox/geojson-area' ) ;
13
8
const precision = require ( 'geojson-precision' ) ;
9
+ const prettyStringify = require ( 'json-stringify-pretty-compact' ) ;
14
10
const rewind = require ( 'geojson-rewind' ) ;
15
-
16
- const isValidLocation = require ( './lib/isValidLocation.js' ) ;
17
- const locationToFeature = require ( './lib/locationToFeature.js ' ) ;
11
+ const shell = require ( 'shelljs' ) ;
12
+ const Validator = require ( 'jsonschema' ) . Validator ;
13
+ const YAML = require ( 'js-yaml ' ) ;
18
14
19
15
const geojsonSchema = require ( './schema/geojson.json' ) ;
20
16
const featureSchema = require ( './schema/feature.json' ) ;
@@ -27,34 +23,37 @@ buildAll();
27
23
28
24
29
25
function buildAll ( ) {
30
- console . log ( 'building data' ) ;
31
- console . time ( colors . green ( 'data built' ) ) ;
26
+ const START = '🏗 ' + colors . yellow ( 'Building data...' ) ;
27
+ const END = '👍 ' + colors . green ( 'data built' ) ;
28
+
29
+ console . log ( '' ) ;
30
+ console . log ( START ) ;
31
+ console . time ( END ) ;
32
32
33
33
// Start clean
34
- shell . rm ( '-f' , [ 'dist/*.json' , 'dist/*.js' , 'i18n/en.yaml' ] ) ;
34
+ shell . rm ( '-f' , [
35
+ 'dist/features.json' ,
36
+ 'dist/resources.json' ,
37
+ 'i18n/en.yaml'
38
+ ] ) ;
35
39
36
40
let tstrings = { } ; // translation strings
37
41
const features = generateFeatures ( ) ;
38
42
const resources = generateResources ( tstrings , features ) ;
39
- const combined = generateCombined ( features , resources ) ;
40
43
41
44
// Save individual data files
42
- fs . writeFileSync ( 'dist/combined.geojson' , prettyStringify ( combined ) ) ;
43
- fs . writeFileSync ( 'dist/combined.min.geojson' , JSON . stringify ( combined ) ) ;
44
- fs . writeFileSync ( 'dist/features.json' , prettyStringify ( { features : features } , { maxLength : 9999 } ) ) ;
45
- fs . writeFileSync ( 'dist/features.min.json' , JSON . stringify ( { features : features } ) ) ;
46
- fs . writeFileSync ( 'dist/resources.json' , prettyStringify ( { resources : resources } , { maxLength : 9999 } ) ) ;
47
- fs . writeFileSync ( 'dist/resources.min.json' , JSON . stringify ( { resources : resources } ) ) ;
48
- fs . writeFileSync ( 'i18n/en.yaml' , YAML . safeDump ( { en : tstrings } , { lineWidth : - 1 } ) ) ;
49
-
50
- console . timeEnd ( colors . green ( 'data built' ) ) ;
45
+ fs . writeFileSync ( 'dist/features.json' , prettyStringify ( { features : sort ( features ) } , { maxLength : 9999 } ) ) ;
46
+ fs . writeFileSync ( 'dist/resources.json' , prettyStringify ( { resources : sort ( resources ) } , { maxLength : 9999 } ) ) ;
47
+ fs . writeFileSync ( 'i18n/en.yaml' , YAML . safeDump ( { en : sort ( tstrings ) } , { lineWidth : - 1 } ) ) ;
48
+
49
+ console . timeEnd ( END ) ;
51
50
}
52
51
53
52
54
53
function generateFeatures ( ) {
55
54
let features = { } ;
56
55
let files = { } ;
57
- process . stdout . write ( 'Features:' ) ;
56
+ process . stdout . write ( '📦 Features: ' ) ;
58
57
59
58
glob . sync ( __dirname + '/features/**/*.geojson' ) . forEach ( file => {
60
59
const contents = fs . readFileSync ( file , 'utf8' ) ;
@@ -110,7 +109,7 @@ function generateFeatures() {
110
109
process . stdout . write ( colors . green ( '✓' ) ) ;
111
110
} ) ;
112
111
113
- process . stdout . write ( Object . keys ( files ) . length + '\n' ) ;
112
+ process . stdout . write ( ' ' + Object . keys ( files ) . length + '\n' ) ;
114
113
115
114
return features ;
116
115
}
@@ -119,7 +118,7 @@ function generateFeatures() {
119
118
function generateResources ( tstrings , features ) {
120
119
let resources = { } ;
121
120
let files = { } ;
122
- process . stdout . write ( 'Resources:' ) ;
121
+ process . stdout . write ( '📦 Resources: ' ) ;
123
122
124
123
glob . sync ( __dirname + '/resources/**/*.json' ) . forEach ( file => {
125
124
let contents = fs . readFileSync ( file , 'utf8' ) ;
@@ -218,7 +217,7 @@ function generateResources(tstrings, features) {
218
217
process . stdout . write ( colors . green ( '✓' ) ) ;
219
218
} ) ;
220
219
221
- process . stdout . write ( Object . keys ( files ) . length + '\n' ) ;
220
+ process . stdout . write ( ' ' + Object . keys ( files ) . length + '\n' ) ;
222
221
223
222
return resources ;
224
223
}
@@ -260,65 +259,12 @@ function prettifyFile(file, object, contents) {
260
259
}
261
260
262
261
263
- function deepClone ( obj ) {
264
- return JSON . parse ( JSON . stringify ( obj ) ) ;
265
- }
266
-
267
-
268
- // Generate a combined GeoJSON FeatureCollection
269
- // containing all the features w/ resources stored in properties
270
- //
271
- // {
272
- // type: 'FeatureCollection',
273
- // features: [
274
- // {
275
- // type: 'Feature',
276
- // id: 'ghana',
277
- // geometry: { ... },
278
- // properties: {
279
- // 'area': 297118.3,
280
- // 'resources': {
281
- // 'osm-gh-facebook': { ... },
282
- // 'osm-gh-twitter': { ... },
283
- // 'talk-gh': { ... }
284
- // }
285
- // }
286
- // }, {
287
- // type: 'Feature',
288
- // id: 'madagascar',
289
- // geometry: { ... },
290
- // properties: {
291
- // 'area': 964945.85,
292
- // 'resources': {
293
- // 'osm-mg-facebook': { ... },
294
- // 'osm-mg-twitter': { ... },
295
- // 'talk-mg': { ... }
296
- // }
297
- // }
298
- // },
299
- // ...
300
- // ]
301
- // }
302
- //
303
- function generateCombined ( features , resources ) {
304
- let keepFeatures = { } ;
305
-
306
- Object . keys ( resources ) . forEach ( resourceId => {
307
- const resource = resources [ resourceId ] ;
308
-
309
- resource . includeLocations . forEach ( location => {
310
- const featureId = location . toString ( ) ;
311
- let keepFeature = keepFeatures [ featureId ] ;
312
- if ( ! keepFeature ) {
313
- const origFeature = locationToFeature ( location , features ) . feature ;
314
- keepFeature = deepClone ( origFeature ) ;
315
- keepFeature . properties . resources = { } ;
316
- keepFeatures [ featureId ] = keepFeature ;
317
- }
318
-
319
- keepFeature . properties . resources [ resourceId ] = deepClone ( resource ) ;
320
- } ) ;
262
+ // Returns an object with sorted keys and sorted values.
263
+ // (This is useful for file diffing)
264
+ function sort ( obj ) {
265
+ let sorted = { } ;
266
+ Object . keys ( obj ) . sort ( ) . forEach ( k => {
267
+ sorted [ k ] = Array . isArray ( obj [ k ] ) ? obj [ k ] . sort ( ) : obj [ k ] ;
321
268
} ) ;
322
-
323
- return { type : 'FeatureCollection' , features : Object . values ( keepFeatures ) } ;
269
+ return sorted ;
324
270
}
0 commit comments