// Generates dist/sitemap.xml from the public, indexable routes in src/seo/routes-meta.ts.
// Run after `vite build` (see the "build" script in package.json): npx tsx scripts/generate-sitemap.mts
// Paths are resolved from import.meta.url so the script works regardless of cwd.
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { SITEMAP_ROUTES, SITE_URL } from '../src/seo/routes-meta';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const distDir = path.resolve(__dirname, '../dist');
const outFile = path.join(distDir, 'sitemap.xml');
const lastmod = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
const urlEntries = SITEMAP_ROUTES.map((route) => {
// Root must be the origin with a trailing slash: https://elegalsoftware.com/
const loc = route.path === '/' ? `${SITE_URL}/` : `${SITE_URL}${route.path}`;
return [
' ',
` ${loc}`,
` ${lastmod}`,
' ',
].join('\n');
});
const xml = [
'',
'',
...urlEntries,
'',
'',
].join('\n');
fs.mkdirSync(distDir, { recursive: true });
fs.writeFileSync(outFile, xml, 'utf8');
console.log(`sitemap: wrote ${SITEMAP_ROUTES.length} URLs to ${outFile}`);