forked from TradeTrust/tradetrust-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.ts
55 lines (49 loc) · 1.71 KB
/
sitemap.ts
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
const path = require("path");
const fs = require("fs");
const { SitemapStream, streamToPromise } = require("sitemap");
const { paths } = require("./src/config/routes-config");
// https://github.com/ekalinin/sitemap.js/blob/master/api.md#sitemap-item-options
interface SitemapRecord {
url: string;
changefreq?: string;
priority?: number;
}
const mainPages: SitemapRecord[] = [];
const pathArray: string[] = Object.values(paths);
Object.values(pathArray).reduce((accumulator, currentPath) => {
if (!currentPath.includes("*") && !currentPath.includes(":slug")) {
accumulator.push({
url: currentPath,
changefreq: "monthly",
priority: 0.6,
});
}
return accumulator;
}, mainPages);
const cmsDetailPages: SitemapRecord[] = [];
const populateCmsPages = (prefix: string, srcDirectory: string) => {
fs.readdirSync(srcDirectory).forEach((file: string) => {
const name = path.parse(file).name;
if (name !== ".DS_Store") {
cmsDetailPages.push({
url: `${prefix}${name}`,
changefreq: "monthly",
priority: 0.5,
});
}
});
};
populateCmsPages("/news/", `${__dirname}/cms/article`);
populateCmsPages("/news/", `${__dirname}/cms/newsletter`);
populateCmsPages("/news/", `${__dirname}/cms/partner-news`);
populateCmsPages("/news/", `${__dirname}/cms/press-release`);
populateCmsPages("/news/", `${__dirname}/cms/speech`);
populateCmsPages("/event/", `${__dirname}/cms/event`);
const allPages = [...mainPages, ...cmsDetailPages];
const sitemap = new SitemapStream({ hostname: "https://www.tradetrust.io" });
const writeStream = fs.createWriteStream(`public/static/sitemap.xml`);
sitemap.pipe(writeStream);
allPages.forEach((page) => {
sitemap.write(page);
});
sitemap.end();