|
| 1 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 2 | +// scripts/generate-sitemap.js |
| 3 | + |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +const SITEMAP_URL = 'https://middleware.jobstash.xyz/app/sitemap/ev'; |
| 8 | +const PUBLIC_DIR = path.join(process.cwd(), 'public'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Fetches sitemap from middleware API and writes to public/sitemap.xml |
| 12 | + * Creates public directory if it doesn't exist |
| 13 | + * Only override the file if the contents have changed |
| 14 | + */ |
| 15 | +async function generateSitemap() { |
| 16 | + console.log(`Fetching sitemap from ${SITEMAP_URL}...`); |
| 17 | + |
| 18 | + const response = await fetch(SITEMAP_URL); |
| 19 | + if (!response.ok) { |
| 20 | + console.error(`Failed to fetch sitemap: ${response.statusText}`); |
| 21 | + process.exit(1); |
| 22 | + } |
| 23 | + |
| 24 | + const newSitemap = await response.text(); |
| 25 | + const sitemapPath = path.join(PUBLIC_DIR, 'sitemap.xml'); |
| 26 | + |
| 27 | + if (!fs.existsSync(PUBLIC_DIR)) { |
| 28 | + fs.mkdirSync(PUBLIC_DIR, { recursive: true }); |
| 29 | + } |
| 30 | + |
| 31 | + let currentSitemap = null; |
| 32 | + if (fs.existsSync(sitemapPath)) { |
| 33 | + currentSitemap = fs.readFileSync(sitemapPath, 'utf-8'); |
| 34 | + } |
| 35 | + |
| 36 | + if (currentSitemap !== newSitemap) { |
| 37 | + fs.writeFileSync(sitemapPath, newSitemap); |
| 38 | + console.log(`Sitemap updated at ${sitemapPath}`); |
| 39 | + } else { |
| 40 | + console.log('No changes in sitemap. File not updated.'); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +generateSitemap().catch((err) => { |
| 45 | + console.error('Error generating sitemap:', err); |
| 46 | + process.exit(1); |
| 47 | +}); |
0 commit comments