Skip to content

Commit

Permalink
Merge pull request #9 from codin-gg/3-sitemap
Browse files Browse the repository at this point in the history
adds www/sitemap.xml build
  • Loading branch information
ivoputzer authored Oct 6, 2023
2 parents 387dae9 + e6db42a commit 0b4876b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
path: data
key: ${{ steps.cache.outputs.cache-primary-key }}
- run: npm run build-coinbase
- run: npm run build-sitemap
- uses: actions/configure-pages@v3
- uses: actions/upload-pages-artifact@v2
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ node_modules
www/api/*/
!www/api/*.*
www/api/.DS_Store
www/sitemap.xml

32 changes: 32 additions & 0 deletions bin/build-sitemap.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from 'fs/promises'
import path from 'path'

async function * getAllFiles (dir, { join } = path, { readdir, stat } = fs) {
const files = await readdir(dir)
for (const file of files) {
const path = join(dir, file)
const stats = await stat(path)
if (stats.isDirectory()) {
yield * getAllFiles(path)
} else if (stats.isFile()) {
yield path
}
}
}

async function generateSitemap (dir, { relative } = path, { stat } = fs) {
let sitemap = '<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
for await (const file of getAllFiles(dir)) {
if (['www/sitemap.xml'].includes(file)) continue
sitemap += `<url><loc>/${relative(dir, file).replace(/\\/g, '/')}</loc><lastmod>${(await stat(file)).mtime.toISOString().split('T')[0]}</lastmod></url>\n`
}
return sitemap + '</urlset>'
}

console.time('[bin/build-sitemap] created: www/sitemap.xml time')
await fs.writeFile('www/sitemap.xml', await generateSitemap('www'))
console.timeEnd('[bin/build-sitemap] created: www/sitemap.xml time')

// For when this file will become too big:
// - https://webmasters.stackexchange.com/a/93807
// - https://chat.openai.com/c/ebb14320-2827-423d-8ddd-1dd016c25497 (first thread)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"scripts": {
"sync-coinbase": "node --experimental-modules bin/sync-coinbase.mjs",
"build-sitemap": "node --experimental-modules bin/build-sitemap.mjs",
"build-coinbase": "node --experimental-modules bin/build-coinbase.mjs",
"test": "NODE_V8_COVERAGE=coverage node --experimental-modules --test --test-reporter spec --experimental-test-coverage ",
"watch": "serve www & node --experimental-modules --test --test-reporter dot --watch & wait"
Expand Down

0 comments on commit 0b4876b

Please sign in to comment.