Skip to content

Commit 502f10d

Browse files
authored
chore: enforce formatting and linting (#38)
1 parent a665a46 commit 502f10d

File tree

11 files changed

+3038
-229
lines changed

11 files changed

+3038
-229
lines changed

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
max_line_length = 120
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true

.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { overrides } = require('@netlify/eslint-config-node')
2+
3+
module.exports = {
4+
extends: '@netlify/eslint-config-node',
5+
overrides: [...overrides],
6+
}

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.github/workflows/test.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@ jobs:
2222
uses: actions/setup-node@v1
2323
with:
2424
node-version: ${{ matrix.node-version }}
25-
- run: npm ci
26-
- run: npm test
25+
- name: Install dependencies
26+
run: npm ci
27+
- name: Linting
28+
run: npm run format:ci
29+
- name: Tests
30+
run: npm test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,5 @@ Icon
109109
Network Trash Folder
110110
Temporary Items
111111
.apdisk
112+
113+
.vscode

.prettierrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"@netlify/eslint-config-node/.prettierrc.json"

index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
/* Generates a sitemap */
2+
const { env } = require('process')
3+
24
const makeSitemap = require('./make_sitemap')
35

6+
const getBuildDir = ({ inputs, constants }) => {
7+
// Backwards compat... Correct opt is buildDir
8+
const buildDir = inputs.dir || inputs.distPath || inputs.buildDir || constants.PUBLISH_DIR
9+
// remove leading / to treat the dir a a relative one
10+
const trimmedBuildDir = buildDir.startsWith('/') ? buildDir.slice(1) : buildDir
11+
return trimmedBuildDir
12+
}
13+
414
module.exports = {
515
onPostBuild: async ({ constants, inputs, utils }) => {
6-
const baseUrl = inputs.baseUrl || process.env.URL
7-
// Backwards compat... Correct opt is buildDir
8-
const buildDir = inputs.dir || inputs.distPath || inputs.buildDir || constants.PUBLISH_DIR
9-
// remove leading / to treat the dir a a relative one
10-
const trimmedBuildDir = buildDir.startsWith('/') ? buildDir.slice(1) : buildDir
16+
const baseUrl = inputs.baseUrl || env.URL
17+
const buildDir = getBuildDir({ inputs, constants })
1118

1219
console.log('Creating sitemap from files...')
1320

1421
const data = await makeSitemap({
1522
homepage: baseUrl,
16-
distPath: trimmedBuildDir,
23+
distPath: buildDir,
1724
exclude: inputs.exclude,
1825
prettyURLs: inputs.prettyURLs,
1926
changeFreq: inputs.changeFreq,

make_sitemap.js

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,57 @@
11
const fs = require('fs')
2-
const util = require('util')
32
const path = require('path')
3+
const util = require('util')
4+
5+
const globby = require('globby')
46
const mkdirp = require('mkdirp')
57
const sm = require('sitemap')
6-
const globby = require('globby')
78

8-
module.exports = async function makeSitemap(opts = {}) {
9-
const { distPath, fileName, homepage, exclude, prettyURLs, trailingSlash, failBuild } = opts
9+
const getPaths = async ({ distPath, exclude }) => {
1010
const htmlFiles = `${distPath}/**/**.html`
11-
const excludeFiles = (exclude || []).map((filePath) => {
12-
return `!${filePath.replace(/^!/, '')}`
13-
})
11+
const excludeFiles = (exclude || []).map((filePath) => `!${filePath.replace(/^!/, '')}`)
1412

15-
const lookup = [ htmlFiles ].concat(excludeFiles)
13+
const lookup = [htmlFiles].concat(excludeFiles)
1614
const paths = await globby(lookup)
17-
const urls = paths.map(file => {
18-
let urlPath = file.startsWith(distPath) ? file.replace(distPath, '') : distPath
19-
if (prettyURLs) {
20-
urlPath = urlPath.replace(/\/index\.html$/, '').replace(/\.html$/, '')
21-
22-
if (trailingSlash) {
23-
urlPath += urlPath.endsWith("/") ? "" : "/";
24-
}
25-
}
15+
return paths
16+
}
17+
18+
const getUrlFromFile = ({ file, distPath, prettyURLs, trailingSlash }) => {
19+
const url = file.startsWith(distPath) ? file.replace(distPath, '') : distPath
2620

21+
if (!prettyURLs) {
22+
return url
23+
}
24+
25+
const prettyUrl = url.replace(/\/index\.html$/, '').replace(/\.html$/, '')
26+
27+
if (!trailingSlash) {
28+
return prettyUrl
29+
}
30+
31+
return prettyUrl.endsWith('/') ? prettyUrl : `${prettyUrl}/`
32+
}
33+
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 })
42+
const urls = paths.map((file) => {
43+
const url = getUrlFromFile({ file, distPath, prettyURLs, trailingSlash })
2744
return {
28-
url: urlPath,
29-
changefreq: opts.changeFreq || 'weekly',
30-
priority: opts.priority || 0.8,
45+
url,
46+
changefreq: opts.changeFreq || DEFAULT_CHANGE_FREQ,
47+
priority: opts.priority || DEFAULT_PRIORITY,
3148
lastmodrealtime: true,
3249
lastmodfile: file,
3350
}
3451
})
3552
const options = {
3653
hostname: `${homepage.replace(/\/$/, '')}/`,
37-
cacheTime: 600000, // 600 sec cache period
54+
cacheTime: DEFAULT_CACHE_TIME,
3855
urls,
3956
}
4057
// Creates a sitemap object given the input configuration with URLs
@@ -57,6 +74,6 @@ module.exports = async function makeSitemap(opts = {}) {
5774
// Return info
5875
return {
5976
sitemapPath: sitemapFileName,
60-
sitemap: sitemap
77+
sitemap,
6178
}
6279
}

0 commit comments

Comments
 (0)