forked from danrasmuson/site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.js
65 lines (63 loc) · 1.56 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
/**
* Generates sitemap
*/
const fs = require('fs-extra')
const dir = require('node-dir')
const sm = require('sitemap')
const path = require('path')
const packageInfo = require('../package.json')
const distPath = path.join(__dirname, '..', 'dist')
const sitemapPath = path.join(__dirname, '..', 'dist', 'sitemap.xml')
function getAllUrls(filePath, callBack) {
dir.readFiles(filePath, {
match: /.html$/,
//exclude: /^\./
}, (err, content, filename, next) => {
if (err) throw err
next()
},
(err, files) => {
if (err) {
callBack(err)
}
const urls = files.map((file, i) => {
return {
url: path.dirname(file.split('dist')[1]),
changefreq: 'weekly',
priority: 0.8,
lastmodrealtime: true,
lastmodfile: file
}
})
callBack && callBack(null, urls)
}
)
}
getAllUrls(distPath, (err, urls) => {
if (err) {
console.log(err)
return false
}
const options = {
hostname: `${packageInfo.homepage}/`,
cacheTime: 600000, // 600 sec cache period
urls
}
// Creates a sitemap object given the input configuration with URLs
const sitemap = sm.createSitemap(options)
// Generates XML with a callback function
sitemap.toXML((err, xml) => {
if (!err) {
// console.log(xml)
}
})
// Gives you a string containing the XML data
const xml = sitemap.toString()
// write sitemap to file
fs.writeFileSync(sitemapPath, xml, 'utf-8', (err) => {
if (err) {
return console.log(err)
}
console.log('Sitemap Built!')
})
})