forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.js
executable file
·75 lines (62 loc) · 2.41 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
#!/usr/bin/env node
const color = require('cli-color');
const Sitemap = require('sitemap');
const program = require('commander');
const fs = require('fs');
const Path = require('path');
const common = require('./common');
const urls = require('./config/sitemap_urls');
program
.version('0.2.0')
.description('Generate sitemap.xml')
.parse(process.argv);
const config = [
{
url_prefix : 'https://www.binary.com/',
filename : 'sitemap.xml',
lang_filter: '^(?!id$)',
section : 'app',
},
{
url_prefix : 'https://www.binary.me/',
filename : 'sitemap.id.xml',
lang_filter: '^id$',
section : 'app',
},
];
let excluded;
const getApplicableLanguages = (lang_filter) => common.languages.filter(lang => new RegExp(lang_filter, 'i').test(lang));
const urlFor = (section, lang, path) => `${lang}/${path}.html`;
const createSitemap = (conf) => {
excluded = 0;
const sitemap = Sitemap.createSitemap({
hostname : conf.url_prefix,
cacheTime: 600000,
});
getApplicableLanguages(conf.lang_filter)
.map(lang => lang.toLowerCase())
.forEach((lang) => {
urls[conf.section].forEach((entry) => {
if (!common.isExcluded(entry[3], lang)) {
sitemap.add({
url : `${conf.url_prefix}${urlFor(conf.section, lang, entry[0])}`,
changefreq: entry[1],
priority : entry[2],
});
} else {
excluded++;
}
});
});
fs.writeFileSync(Path.join(common.root_path, 'src/root_files', conf.section, conf.filename), sitemap.toString());
};
config.forEach((conf) => {
const start = Date.now();
process.stdout.write(common.messageStart(`Generating ${conf.section}/${conf.filename}`, true));
createSitemap(conf);
process.stdout.write(common.messageEnd(Date.now() - start));
// Report details
const langs_count = getApplicableLanguages(conf.lang_filter).length;
const total_count = langs_count * urls[conf.section].length - excluded;
console.log(` ${color.green(total_count)} URL nodes total (${color.cyan(langs_count)} Languages ${color.yellowBright('*')} ${color.cyan(urls.length)} URLs ${color.yellowBright('-')} ${color.cyan(excluded)} Excluded)\n`); // eslint-disable-line no-console
});