-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
67 lines (51 loc) · 2.02 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
const fs = require('fs');
const path = require('path');
const shell = require('shelljs');
const program = require('commander');
const turfBBox = require('@turf/bbox');
const turfHelpers = require('@turf/helpers');
const bbox = turfBBox.default; // @see https://github.com/Turfjs/turf/issues/1932
const version = require('./package.json').version;
program.version(version);
program.option('-c, --city <path>').option('-s, --serve').action(bundle);
program
.parseAsync(process.argv)
.catch(error => {
shell.echo(`Error: ${error}`);
shell.exit(1);
});
async function bundle (options) {
const city = options.city;
const serve = options.serve || false;
const directory = `../cities/${city}`;
const outDir = path.join(__dirname, 'dist', city);
if (shell.test('-e', directory) === true) {
shell.rm('-rf', ['assets', 'dist', 'public', 'static', outDir]);
shell.mkdir('assets', 'public', 'static');
shell.mkdir('-p', outDir);
shell.cp(path.join(directory, 'assets', '*'), 'assets');
shell.cp('-r', path.join(directory, 'html', '*'), 'public');
shell.cp(path.join(directory, 'data', '*.geojson'), 'static');
const boundary = JSON.parse(fs.readFileSync(path.resolve(directory, 'data', 'boundary.geojson')));
const bounds = bbox(turfHelpers.feature(boundary));
const metadata = JSON.parse(fs.readFileSync(path.resolve(directory, 'data', 'metadata.json')));
const static = { bounds, statistics: metadata.genders, lastUpdate: metadata.update };
fs.writeFileSync(
path.join('static', 'static.json'),
JSON.stringify(static),
'utf8'
);
const file = path.join(__dirname, 'public', 'index.html');
if (serve === true) {
shell.exec(`parcel serve "${file}" --dist-dir "${outDir}"`, { async: true });
} else {
if (shell.exec(`parcel build "${file}" --dist-dir "${outDir}"`).code !== 0) {
shell.echo('Error: Build failed');
shell.exit(1);
}
}
} else {
shell.echo(`Error: Path ${directory} does not exist.`);
shell.exit(1);
}
}