Skip to content

Commit 2a22aaa

Browse files
authored
test: improve tests (#39)
1 parent 502f10d commit 2a22aaa

File tree

4 files changed

+357
-142
lines changed

4 files changed

+357
-142
lines changed

make_sitemap.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ const util = require('util')
44

55
const globby = require('globby')
66
const mkdirp = require('mkdirp')
7-
const sm = require('sitemap')
7+
const { createSitemap } = require('sitemap')
88

9-
const getPaths = async ({ distPath, exclude }) => {
9+
const ensureTrailingSlash = (url) => (url.endsWith('/') ? url : `${url}/`)
10+
11+
const getPaths = async ({ distPath, exclude, cwd }) => {
1012
const htmlFiles = `${distPath}/**/**.html`
1113
const excludeFiles = (exclude || []).map((filePath) => `!${filePath.replace(/^!/, '')}`)
1214

1315
const lookup = [htmlFiles].concat(excludeFiles)
14-
const paths = await globby(lookup)
16+
const paths = await globby(lookup, { cwd })
1517
return paths
1618
}
1719

@@ -28,34 +30,47 @@ const getUrlFromFile = ({ file, distPath, prettyURLs, trailingSlash }) => {
2830
return prettyUrl
2931
}
3032

31-
return prettyUrl.endsWith('/') ? prettyUrl : `${prettyUrl}/`
33+
return ensureTrailingSlash(prettyUrl)
3234
}
3335

34-
const DEFAULT_CHANGE_FREQ = 'weekly'
35-
const DEFAULT_PRIORITY = 0.8
36-
// 600 sec cache period
37-
const DEFAULT_CACHE_TIME = 600000
38-
39-
module.exports = async function makeSitemap(opts = {}) {
40-
const { distPath, fileName, homepage, exclude, prettyURLs, trailingSlash, failBuild } = opts
41-
const paths = await getPaths({ distPath, exclude })
36+
const getUrlsFromPaths = ({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd }) => {
4237
const urls = paths.map((file) => {
4338
const url = getUrlFromFile({ file, distPath, prettyURLs, trailingSlash })
4439
return {
4540
url,
46-
changefreq: opts.changeFreq || DEFAULT_CHANGE_FREQ,
47-
priority: opts.priority || DEFAULT_PRIORITY,
41+
changefreq: changeFreq,
42+
priority,
4843
lastmodrealtime: true,
49-
lastmodfile: file,
44+
lastmodfile: cwd === undefined ? file : path.resolve(cwd, file),
5045
}
5146
})
52-
const options = {
53-
hostname: `${homepage.replace(/\/$/, '')}/`,
54-
cacheTime: DEFAULT_CACHE_TIME,
55-
urls,
56-
}
47+
return urls
48+
}
49+
50+
const DEFAULT_CHANGE_FREQ = 'weekly'
51+
const DEFAULT_PRIORITY = 0.8
52+
// 600 sec cache period
53+
const DEFAULT_CACHE_TIME = 600000
54+
55+
module.exports = async function makeSitemap(opts = {}) {
56+
const {
57+
distPath,
58+
fileName,
59+
homepage,
60+
exclude,
61+
prettyURLs,
62+
trailingSlash,
63+
failBuild,
64+
cwd,
65+
changeFreq = DEFAULT_CHANGE_FREQ,
66+
priority = DEFAULT_PRIORITY,
67+
} = opts
68+
69+
const paths = await getPaths({ distPath, exclude, cwd })
70+
const urls = getUrlsFromPaths({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd })
71+
5772
// Creates a sitemap object given the input configuration with URLs
58-
const sitemap = sm.createSitemap(options)
73+
const sitemap = createSitemap({ hostname: ensureTrailingSlash(homepage), cacheTime: DEFAULT_CACHE_TIME, urls })
5974
// Generates XML
6075
try {
6176
await util.promisify(sitemap.toXML.bind(sitemap))()

package-lock.json

Lines changed: 170 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"@netlify/eslint-config-node": "^2.0.0",
6060
"ava": "^2.1.0",
6161
"husky": "^4.3.5",
62-
"rimraf": "^3.0.2",
62+
"tempy": "^1.0.0",
6363
"xml2js": "^0.4.23"
6464
}
6565
}

0 commit comments

Comments
 (0)