Skip to content

Commit a7c7d30

Browse files
committed
Split build and build_dist into 2 steps
This hopefully avoids rebuilding minified and combined files so often
1 parent 214c262 commit a7c7d30

13 files changed

+32757
-32701
lines changed

RELEASE.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ community resources. Whenever the list of resource types changes:
2121
- [ ] git checkout release
2222
- [ ] git reset --hard master
2323
- [ ] npm run build
24+
- [ ] npm run dist
2425
- [ ] git add -f dist/*
2526
- [ ] git commit -m 'Check in build'
2627
- [ ] git tag vA.B.C

build.js

+33-87
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
/* eslint-disable no-console */
2+
const calcArea = require('@mapbox/geojson-area');
23
const colors = require('colors/safe');
34
const fs = require('fs');
45
const glob = require('glob');
6+
const isValidLocation = require('./lib/isValidLocation.js');
57
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');
138
const precision = require('geojson-precision');
9+
const prettyStringify = require('json-stringify-pretty-compact');
1410
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');
1814

1915
const geojsonSchema = require('./schema/geojson.json');
2016
const featureSchema = require('./schema/feature.json');
@@ -27,34 +23,37 @@ buildAll();
2723

2824

2925
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);
3232

3333
// 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+
]);
3539

3640
let tstrings = {}; // translation strings
3741
const features = generateFeatures();
3842
const resources = generateResources(tstrings, features);
39-
const combined = generateCombined(features, resources);
4043

4144
// 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);
5150
}
5251

5352

5453
function generateFeatures() {
5554
let features = {};
5655
let files = {};
57-
process.stdout.write('Features:');
56+
process.stdout.write('📦 Features: ');
5857

5958
glob.sync(__dirname + '/features/**/*.geojson').forEach(file => {
6059
const contents = fs.readFileSync(file, 'utf8');
@@ -110,7 +109,7 @@ function generateFeatures() {
110109
process.stdout.write(colors.green('✓'));
111110
});
112111

113-
process.stdout.write(Object.keys(files).length + '\n');
112+
process.stdout.write(' ' + Object.keys(files).length + '\n');
114113

115114
return features;
116115
}
@@ -119,7 +118,7 @@ function generateFeatures() {
119118
function generateResources(tstrings, features) {
120119
let resources = {};
121120
let files = {};
122-
process.stdout.write('Resources:');
121+
process.stdout.write('📦 Resources: ');
123122

124123
glob.sync(__dirname + '/resources/**/*.json').forEach(file => {
125124
let contents = fs.readFileSync(file, 'utf8');
@@ -218,7 +217,7 @@ function generateResources(tstrings, features) {
218217
process.stdout.write(colors.green('✓'));
219218
});
220219

221-
process.stdout.write(Object.keys(files).length + '\n');
220+
process.stdout.write(' ' + Object.keys(files).length + '\n');
222221

223222
return resources;
224223
}
@@ -260,65 +259,12 @@ function prettifyFile(file, object, contents) {
260259
}
261260

262261

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];
321268
});
322-
323-
return { type: 'FeatureCollection', features: Object.values(keepFeatures) };
269+
return sorted;
324270
}

build_dist.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* eslint-disable no-console */
2+
const colors = require('colors/safe');
3+
const fs = require('fs');
4+
const locationToFeature = require('./lib/locationToFeature.js');
5+
const prettyStringify = require('json-stringify-pretty-compact');
6+
const shell = require('shelljs');
7+
8+
const features = require('./dist/features.json').features;
9+
const resources = require('./dist/resources.json').resources;
10+
11+
12+
buildAll();
13+
14+
15+
function buildAll() {
16+
const START = '🏗 ' + colors.yellow('Building dist...');
17+
const END = '👍 ' + colors.green('dist built');
18+
19+
console.log('');
20+
console.log(START);
21+
console.time(END);
22+
23+
// Start clean
24+
shell.rm('-f', [
25+
'dist/combined.geojson',
26+
'dist/combined.min.geojson',
27+
'dist/features.min.geojson',
28+
'dist/resources.min.geojson'
29+
]);
30+
31+
const combined = generateCombined(features, resources);
32+
33+
// Save individual data files
34+
fs.writeFileSync('dist/combined.geojson', prettyStringify(combined) );
35+
fs.writeFileSync('dist/combined.min.geojson', JSON.stringify(combined) );
36+
fs.writeFileSync('dist/features.min.json', JSON.stringify({ features: features }) );
37+
fs.writeFileSync('dist/resources.min.json', JSON.stringify({ resources: resources }) );
38+
39+
console.timeEnd(END);
40+
}
41+
42+
43+
function deepClone(obj) {
44+
return JSON.parse(JSON.stringify(obj));
45+
}
46+
47+
48+
// Generate a combined GeoJSON FeatureCollection
49+
// containing all the features w/ resources stored in properties
50+
//
51+
// {
52+
// type: 'FeatureCollection',
53+
// features: [
54+
// {
55+
// type: 'Feature',
56+
// id: 'ghana',
57+
// geometry: { ... },
58+
// properties: {
59+
// 'area': 297118.3,
60+
// 'resources': {
61+
// 'osm-gh-facebook': { ... },
62+
// 'osm-gh-twitter': { ... },
63+
// 'talk-gh': { ... }
64+
// }
65+
// }
66+
// }, {
67+
// type: 'Feature',
68+
// id: 'madagascar',
69+
// geometry: { ... },
70+
// properties: {
71+
// 'area': 964945.85,
72+
// 'resources': {
73+
// 'osm-mg-facebook': { ... },
74+
// 'osm-mg-twitter': { ... },
75+
// 'talk-mg': { ... }
76+
// }
77+
// }
78+
// },
79+
// ...
80+
// ]
81+
// }
82+
//
83+
function generateCombined(features, resources) {
84+
let keepFeatures = {};
85+
86+
Object.keys(resources).forEach(resourceId => {
87+
const resource = resources[resourceId];
88+
89+
resource.includeLocations.forEach(location => {
90+
const featureId = location.toString();
91+
let keepFeature = keepFeatures[featureId];
92+
if (!keepFeature) {
93+
const origFeature = locationToFeature(location, features).feature;
94+
keepFeature = deepClone(origFeature);
95+
keepFeature.properties.resources = {};
96+
keepFeatures[featureId] = keepFeature;
97+
}
98+
99+
keepFeature.properties.resources[resourceId] = deepClone(resource);
100+
});
101+
});
102+
103+
return { type: 'FeatureCollection', features: Object.values(keepFeatures) };
104+
}

build_icons.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ function buildAll() {
4141
youtube: faYoutube
4242
};
4343

44-
console.log('building icons');
45-
console.time(colors.green('icons built'));
44+
const START = '🏗 ' + colors.yellow('Building icons...');
45+
const END = '👍 ' + colors.green('icons built');
46+
47+
console.log('');
48+
console.log(START);
49+
console.time(END);
4650

4751
for (let key in faIconMap) {
4852
const val = faIconMap[key];
@@ -51,5 +55,5 @@ function buildAll() {
5155
fs.writeFileSync(file, fontawesome.icon(val).html);
5256
}
5357

54-
console.timeEnd(colors.green('icons built'));
58+
console.timeEnd(END);
5559
}

0 commit comments

Comments
 (0)