forked from wuhan2020/wuhan2020.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap-generator.js
51 lines (38 loc) · 1.11 KB
/
sitemap-generator.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
let fs = require('fs');
let builder = require('xmlbuilder');
let languages = ['zh-cn', 'en-us', 'it-it', 'ja-jp', 'fr-fr', 'pt-br'];
let domain = 'https://community.wuhan2020.org.cn/';
let files = [];
var waitingCount = 0;
languages.forEach(function (lang) {
ls(lang)
});
function ls(path) {
waitingCount ++;
fs.readdir(path, function(err, items) {
items.forEach(function (item) {
if (fs.lstatSync(path + "/" + item).isDirectory()) {
ls(path + "/" + item)
} else {
if (!item.endsWith('json'))
files.push(domain + path + "/" + item);
}
});
waitingCount --;
if (waitingCount === 0) {
files.sort();
xml();
}
});
}
function xml() {
let root = builder.create('urlset', { encoding: 'utf-8' }).att('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
for (var i = 0; i < files.length; i ++) {
root.ele("url").ele("loc", files[i]);
}
let xml = root.end({ pretty: true});
fs.writeFile("sitemap.xml", xml, function(err, result) {
if (err) console.log('error', err);
else console.log("sitemap.xml has been generated!");
});
}