Skip to content

Commit 92a4f2f

Browse files
committedNov 12, 2019
fix: fail on invalid options
1 parent 4b23c02 commit 92a4f2f

File tree

6 files changed

+59
-34
lines changed

6 files changed

+59
-34
lines changed
 

‎lib/builder.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const { URL } = require('url')
55
const isHTTPS = require('is-https')
66
const sm = require('sitemap')
77

8+
const logger = require('./logger')
9+
810
/**
911
* Initialize a fresh sitemap instance
1012
*
@@ -73,11 +75,11 @@ function createSitemapIndex(options, base = null, req = null) {
7375
const sitemapIndexConfig = {}
7476

7577
// Set urls
76-
const defaultHostname = getHostname(options, req, base)
78+
const defaultHostname = options.hostname
7779
sitemapIndexConfig.urls = options.sitemaps.map(options => {
7880
// Normalize to absolute path
7981
const path = join('.', options.gzip ? `${options.path}.gz` : options.path)
80-
const hostname = options.hostname ? getHostname(options, req, base) : defaultHostname
82+
const hostname = getHostname(options.hostname ? options : { ...options, hostname: defaultHostname }, req, base)
8183
const url = new URL(path, hostname)
8284
return url.href
8385
})
@@ -104,6 +106,10 @@ function createSitemapIndex(options, base = null, req = null) {
104106
* @returns {string}
105107
*/
106108
function getHostname(options, req, base) {
109+
/* istanbul ignore if */
110+
if (!options.hostname && !req) {
111+
logger.fatal('The `hostname` option is mandatory in your config on `spa` or `generate` build mode', options)
112+
}
107113
return join(
108114
options.hostname || (req && `${isHTTPS(req) ? 'https' : 'http'}://${req.headers.host}`) || `http://${hostname()}`,
109115
base

‎lib/generator.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const path = require('path')
22
const { gzipSync } = require('zlib')
33

4-
const consola = require('consola')
54
const fs = require('fs-extra')
65

76
const { createSitemap, createSitemapIndex } = require('./builder')
87
const { createRoutesCache } = require('./cache')
8+
const logger = require('./logger')
99
const { setDefaultSitemapOptions, setDefaultSitemapIndexOptions } = require('./options')
1010
const { excludeRoutes } = require('./routes')
1111

@@ -34,8 +34,6 @@ async function generateSitemaps(options, globalCache, nuxtInstance) {
3434
* @param {Nuxt} nuxtInstance
3535
*/
3636
async function generateSitemap(options, globalCache, nuxtInstance) {
37-
consola.info('Generating sitemap')
38-
3937
// Init options
4038
options = setDefaultSitemapOptions(options, nuxtInstance)
4139

@@ -50,13 +48,13 @@ async function generateSitemap(options, globalCache, nuxtInstance) {
5048
const sitemap = await createSitemap(options, routes, base)
5149
const xmlFilePath = path.join(nuxtInstance.options.generate.dir, options.path)
5250
fs.outputFileSync(xmlFilePath, sitemap.toXML())
53-
consola.success('Generated', getPathname(nuxtInstance.options.generate.dir, xmlFilePath))
51+
logger.success('Generated', getPathname(nuxtInstance.options.generate.dir, xmlFilePath))
5452

5553
// Generate sitemap.xml.gz
5654
if (options.gzip) {
5755
const gzipFilePath = path.join(nuxtInstance.options.generate.dir, options.pathGzip)
5856
fs.outputFileSync(gzipFilePath, sitemap.toGzip())
59-
consola.success('Generated', getPathname(nuxtInstance.options.generate.dir, gzipFilePath))
57+
logger.success('Generated', getPathname(nuxtInstance.options.generate.dir, gzipFilePath))
6058
}
6159
}
6260

@@ -68,8 +66,6 @@ async function generateSitemap(options, globalCache, nuxtInstance) {
6866
* @param {Nuxt} nuxtInstance
6967
*/
7068
async function generateSitemapIndex(options, globalCache, nuxtInstance) {
71-
consola.info('Generating sitemapindex')
72-
7369
// Init options
7470
options = setDefaultSitemapIndexOptions(options)
7571

@@ -78,14 +74,14 @@ async function generateSitemapIndex(options, globalCache, nuxtInstance) {
7874
const xml = createSitemapIndex(options, base)
7975
const xmlFilePath = path.join(nuxtInstance.options.generate.dir, options.path)
8076
fs.outputFileSync(xmlFilePath, xml)
81-
consola.success('Generated', getPathname(nuxtInstance.options.generate.dir, xmlFilePath))
77+
logger.success('Generated', getPathname(nuxtInstance.options.generate.dir, xmlFilePath))
8278

8379
// Generate sitemapindex.xml.gz
8480
if (options.gzip) {
8581
const gzip = gzipSync(xml)
8682
const gzipFilePath = path.join(nuxtInstance.options.generate.dir, options.pathGzip)
8783
fs.outputFileSync(gzipFilePath, gzip)
88-
consola.success('Generated', getPathname(nuxtInstance.options.generate.dir, gzipFilePath))
84+
logger.success('Generated', getPathname(nuxtInstance.options.generate.dir, gzipFilePath))
8985
}
9086

9187
// Generate linked sitemaps

‎lib/logger.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* istanbul ignore file */
2+
3+
const consola = require('consola')
4+
5+
function warn(message, options = null) {
6+
consola.warn({
7+
message: `[sitemap-module] ${message}`,
8+
additional: JSON.stringify(options, null, 2)
9+
})
10+
}
11+
12+
function fatal(message, options = null) {
13+
consola.fatal({
14+
message: `[sitemap-module] ${message}`,
15+
additional: options ? JSON.stringify(options, null, 2) : null
16+
})
17+
}
18+
19+
module.exports = { ...consola, warn, fatal }

‎lib/middleware.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
const { gzipSync } = require('zlib')
22

3-
const consola = require('consola')
4-
53
const { createSitemap, createSitemapIndex } = require('./builder')
64
const { createRoutesCache } = require('./cache')
5+
const logger = require('./logger')
76
const { setDefaultSitemapOptions, setDefaultSitemapIndexOptions } = require('./options')
87
const { excludeRoutes } = require('./routes')
98

@@ -19,7 +18,7 @@ function registerSitemaps(options, globalCache, nuxtInstance, depth = 0) {
1918
if (depth > 1) {
2019
// see https://webmasters.stackexchange.com/questions/18243/can-a-sitemap-index-contain-other-sitemap-indexes
2120
/* istanbul ignore next */
22-
consola.warn("A sitemap index file can't list other sitemap index files, but only sitemap files")
21+
logger.warn("A sitemap index file can't list other sitemap index files, but only sitemap files")
2322
}
2423

2524
const isSitemapIndex = options && options.sitemaps && Array.isArray(options.sitemaps) && options.sitemaps.length > 0

‎lib/module.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const path = require('path')
22

3-
const consola = require('consola')
43
const fs = require('fs-extra')
54

65
const { generateSitemaps } = require('./generator')
6+
const logger = require('./logger')
77
const { registerSitemaps } = require('./middleware')
88
const { getStaticRoutes } = require('./routes')
99

@@ -36,7 +36,7 @@ module.exports = function module(moduleOptions) {
3636

3737
// On "generate" mode, generate static files for each sitemap or sitemapindex
3838
nuxtInstance.nuxt.hook('generate:done', async () => {
39-
consola.info('Generating sitemaps')
39+
logger.info('Generating sitemaps')
4040
await Promise.all(options.map(options => generateSitemaps(options, globalCache, nuxtInstance)))
4141
})
4242

‎lib/options.js

+23-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const consola = require('consola')
1+
const logger = require('./logger')
22

33
const DEFAULT_NUXT_PUBLIC_PATH = '/_nuxt/'
44

@@ -27,24 +27,24 @@ function setDefaultSitemapOptions(options, nuxtInstance) {
2727
defaults: {}
2828
}
2929

30-
options = {
30+
const sitemapOptions = {
3131
...defaults,
3232
...options
3333
}
3434

3535
/* istanbul ignore if */
36-
if (options.generate) {
37-
consola.warn('The "generate" option isn\'t needed anymore')
36+
if (sitemapOptions.generate) {
37+
logger.warn("The `generate` option isn't needed anymore in your config. Please remove it!")
3838
}
3939

4040
/* istanbul ignore if */
41-
if (!options.path) {
42-
consola.warn('The "path" option is either empty or missing for a sitemap')
41+
if (!sitemapOptions.path) {
42+
logger.fatal('The `path` option is either empty or missing in your config for a sitemap', options)
4343
}
4444

45-
options.pathGzip = options.gzip ? `${options.path}.gz` : options.path
45+
sitemapOptions.pathGzip = sitemapOptions.gzip ? `${sitemapOptions.path}.gz` : sitemapOptions.path
4646

47-
return options
47+
return sitemapOptions
4848
}
4949

5050
/**
@@ -63,29 +63,34 @@ function setDefaultSitemapIndexOptions(options) {
6363
xslUrl: undefined
6464
}
6565

66-
options = {
66+
const sitemapIndexOptions = {
6767
...defaults,
6868
...options
6969
}
7070

7171
/* istanbul ignore if */
72-
if (options.generate) {
73-
consola.warn("The option `generate` isn't needed anymore")
72+
if (sitemapIndexOptions.generate) {
73+
logger.warn("The `generate` option isn't needed anymore in your config. Please remove it!")
7474
}
7575

7676
/* istanbul ignore if */
77-
if (!options.path) {
78-
consola.warn('The "path" option is either empty or missing for a sitemap index')
77+
if (!sitemapIndexOptions.path) {
78+
logger.fatal('The `path` option is either empty or missing in your config for a sitemap index', options)
7979
}
8080

81-
// set cascading hostname
82-
options.sitemaps.forEach(sitemapOptions => {
83-
sitemapOptions.hostname = sitemapOptions.hostname || options.hostname
81+
sitemapIndexOptions.sitemaps.forEach(sitemapOptions => {
82+
/* istanbul ignore if */
83+
if (!sitemapOptions.path) {
84+
logger.fatal('The `path` option is either empty or missing in your config for a sitemap', sitemapOptions)
85+
}
86+
87+
// set cascading hostname
88+
sitemapOptions.hostname = sitemapOptions.hostname || sitemapIndexOptions.hostname
8489
})
8590

86-
options.pathGzip = options.gzip ? `${options.path}.gz` : options.path
91+
sitemapIndexOptions.pathGzip = sitemapIndexOptions.gzip ? `${sitemapIndexOptions.path}.gz` : sitemapIndexOptions.path
8792

88-
return options
93+
return sitemapIndexOptions
8994
}
9095

9196
module.exports = { setDefaultSitemapOptions, setDefaultSitemapIndexOptions }

0 commit comments

Comments
 (0)
Please sign in to comment.