-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.js
107 lines (91 loc) · 2.36 KB
/
sitemap.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
import axios from 'axios';
import dotenv from 'dotenv';
import fs from 'fs';
dotenv.config();
const {
PUBLIC_BASE_URL_SERVER,
PUBLIC_ENV,
PUBLIC_PORT_NUMBER_SERVER,
} = process.env;
const baseUrlServer = PUBLIC_ENV === 'development' ? `${PUBLIC_BASE_URL_SERVER}:${PUBLIC_PORT_NUMBER_SERVER}` : PUBLIC_BASE_URL_SERVER;
const serviceGroups = {
bus: {
modes: [
'bus',
],
pathService: '/bus/service',
pathServices: '/bus',
},
core: {
modes: [
'tube',
'overground',
'elizabeth-line',
'dlr',
'tram',
],
pathService: '/service',
pathServices: '/',
},
['national-rail']: {
modes: [
'national-rail',
],
pathService: '/national-rail/service',
pathServices: '/national-rail',
},
['river-bus']: {
modes: [
'river-bus',
],
pathService: '/river-bus/service',
pathServices: '/river-bus',
},
};
function formatServiceGroups(services) {
const output = {};
for (const [ group, { modes, pathService, pathServices } ] of Object.entries(serviceGroups)) {
output[group] = {
modes: modes.flatMap((mode) => {
return services[mode];
}),
pathService,
pathServices,
};
}
return output;
}
const { data } = await axios.get(`${baseUrlServer}/services/all`);
const date = new Date().toISOString().slice(0, 10);
const serviceGroupsFormatted = formatServiceGroups(data);
const generateSitemapItem = (path) => {
return (
`
<url>
<loc>https://tflstatus.co.uk${path}</loc>
<lastmod>${date}</lastmod>
<changefreq>always</changefreq>
<priority>0.8</priority>
</url>
`
);
};
const generateSitemap = () => {
let output = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
output += generateSitemapItem('/');
output += generateSitemapItem('/about');
Object.values(serviceGroupsFormatted).forEach((serviceGroup) => {
output += generateSitemapItem(serviceGroup.pathServices);
serviceGroup.modes.forEach((mode) => {
output += generateSitemapItem(`${serviceGroup.pathService}/${mode.id}`);
});
});
output += '</urlset>';
return output;
};
try {
fs.writeFileSync('./client/dist/sitemap.xml', generateSitemap());
console.log('sitemap.xml write success 👍');
} catch (error) {
console.error('sitemap.xml write error', error);
}