-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.js
69 lines (57 loc) · 2.03 KB
/
setup.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
/**
* This script reads files in src/assets/data directory and generate
* a file that is used by the app in runtime. The goal is to simplify
* the data and only output minimum required data.
*
* @note That this file may change in future as requirements change.
*/
const fs = require('fs');
const path = require('path');
const countries = path.resolve(__dirname, './src/assets/data/countries');
const flags = path.resolve(__dirname, './public/assets/flags');
const countryList = fs.readdirSync(countries);
const countryData = [];
const countryRoutes = [];
const sitemap = ['https://jabran.me/covidonation'];
countryList.forEach((c) => {
const content = JSON.parse(fs.readFileSync(`${countries}/${c}`));
const hasFlag = fs.existsSync(
`${flags}/${content.iso2Code.toLowerCase()}.svg`
);
countryData.push({
name: content.country,
slug: content.slug,
iso2: content.iso2Code.toLowerCase(),
hasFlag
});
countryRoutes.push(
`<RouteWithSummary exact path="/${content.slug}" slug="${content.slug}" render={(props) => <CountryPage slug="${content.slug}" {...props} />} />`
);
sitemap.push(`https://jabran.me/covidonation/${content.slug}`);
});
// write file with slugs for all countries
fs.writeFileSync(
path.resolve(__dirname, './src/assets/data/index.js'),
`/* Automatically generated file. DO NOT edit manually. See setup.js for details. */
export default ${JSON.stringify(countryData)};`
);
// write file with routes based on slugs for all countries
fs.writeFileSync(
path.resolve(__dirname, './src/routes/country-routes.js'),
`/* Automatically generated file. DO NOT edit manually. See setup.js for details. */
import React from 'react';
import { Switch } from 'react-router-dom';
import CountryPage from '../pages/country';
import RouteWithSummary from './route-with-summary';
const CountryRoutes = () => (
<Switch>
${countryRoutes.join('\n')}
</Switch>
);
export default CountryRoutes;`
);
// generate sitemap
fs.writeFileSync(
path.resolve(__dirname, './public/sitemap.txt'),
sitemap.join('\n')
);