-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
134 lines (111 loc) · 3.29 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
const fs = require('fs');
const path = require('path');
const shell = require('shelljs');
const program = require('commander');
const turf = require('@turf/turf');
const version = require('./package.json').version;
program.version(version);
program.option('-s, --serve').action(bundle);
program.parseAsync(process.argv).catch((error) => {
shell.echo(`Error: ${error}`);
shell.exit(1);
});
async function bundle (options) {
const serve = options.serve || false;
try {
const polygons = { type: 'FeatureCollection', features: [] };
const points = { type: 'FeatureCollection', features: [] };
const citiesDetails = JSON.parse(
fs.readFileSync(path.resolve('./cities.json'))
);
const directory = path.resolve('../cities/');
const countries = fs
.readdirSync(directory, { withFileTypes: true })
.filter((directory) => directory.isDirectory());
countries.forEach((country) => {
const cities = fs
.readdirSync(path.join(directory, country.name), {
withFileTypes: true
})
.filter((directory) => directory.isDirectory());
cities.forEach((city) => {
if (
typeof citiesDetails[country.name] === 'undefined' ||
typeof citiesDetails[country.name][city.name] === 'undefined'
) {
throw new Error(
`Details are missing for "${country.name}/${city.name}".`
);
}
const feature = {
type: 'Feature',
id: `${country.name}:${city.name}`,
properties: citiesDetails[country.name][city.name],
geometry: null
};
const metadata = JSON.parse(
fs.readFileSync(
path.join(
directory,
country.name,
city.name,
'data',
'metadata.json'
),
'utf8'
)
);
feature.properties.statistics = metadata.genders;
feature.properties.lastUpdate = metadata.update;
const boundary = fs.readFileSync(
path.join(
directory,
country.name,
city.name,
'data',
'boundary.geojson'
),
'utf8'
);
feature.geometry = JSON.parse(boundary);
polygons.features.push(feature);
const centroid = turf.centerOfMass(feature.geometry);
const featurePoint = { ...feature };
featurePoint.geometry = centroid.geometry;
points.features.push(featurePoint);
shell.echo('✔', feature.properties.name);
});
});
shell.rm('-rf', 'dist/');
shell.mkdir('dist/');
fs.writeFileSync(
path.resolve('./dist/cities.json'),
JSON.stringify(polygons),
'utf8'
);
fs.writeFileSync(
path.resolve('./dist/cities-point.json'),
JSON.stringify(points),
'utf8'
);
shell.echo(
'Total:',
polygons.features.length,
'polygons',
'&',
points.features.length,
'points'
);
if (serve === true) {
shell.exec('npm run parcel:serve', { async: true });
} else {
if (shell.exec('npm run parcel:build').code !== 0) {
shell.echo('Error: Build failed');
shell.exit(1);
}
}
} catch (error) {
shell.echo(error.message);
shell.exit(1);
}
}