-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-sitemap.mjs
47 lines (38 loc) · 1.2 KB
/
generate-sitemap.mjs
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
import * as fs from "node:fs";
import * as path from "node:path";
let rootDirName = process.argv[2];
if (!rootDirName) {
console.error(`Root path not specified`);
process.exit(1);
}
let rootPath = rootDirName;
if (!path.isAbsolute(rootPath)) {
rootPath = path.resolve(process.cwd(), rootPath);
}
if (!fs.existsSync(rootPath)) {
console.error(`Path does not exist: ` + rootPath);
process.exit(1);
}
let lastMod = new Date();
let sitemap = `<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
`;
function addFilesFromDirectory(dirPath, baseUrlPathname) {
for (const currentFileName of fs.readdirSync(dirPath)) {
const currentPath = path.resolve(dirPath, currentFileName)
if (fs.statSync(currentPath).isDirectory()) {
addFilesFromDirectory(currentPath, baseUrlPathname + "/" + currentFileName);
continue;
}
if (path.extname(currentFileName) === ".html") {
sitemap += ` <url>
<loc>https://api.feathersui.com/${baseUrlPathname}/${currentFileName}</loc>
<lastmod>${lastMod.toISOString()}</lastmod>
</url>
`;
}
}
}
addFilesFromDirectory(rootPath, rootDirName);
sitemap += `</urlset>`;
fs.writeFileSync(`sitemap.xml`, sitemap, {encoding: "utf8"});