generate-sitemap.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env bun
  2. import { readdir, writeFile } from "fs/promises"
  3. import { join, dirname } from "path"
  4. import { fileURLToPath } from "url"
  5. import { config } from "../src/config.js"
  6. const __dirname = dirname(fileURLToPath(import.meta.url))
  7. const BASE_URL = config.baseUrl
  8. const PUBLIC_DIR = join(__dirname, "../public")
  9. const ROUTES_DIR = join(__dirname, "../src/routes")
  10. const DOCS_DIR = join(__dirname, "../../../web/src/content/docs")
  11. interface SitemapEntry {
  12. url: string
  13. priority: number
  14. changefreq: string
  15. }
  16. async function getMainRoutes(): Promise<SitemapEntry[]> {
  17. const routes: SitemapEntry[] = []
  18. // Add main static routes
  19. const staticRoutes = [
  20. { path: "/", priority: 1.0, changefreq: "daily" },
  21. { path: "/enterprise", priority: 0.8, changefreq: "weekly" },
  22. { path: "/brand", priority: 0.6, changefreq: "monthly" },
  23. { path: "/zen", priority: 0.8, changefreq: "weekly" },
  24. ]
  25. for (const route of staticRoutes) {
  26. routes.push({
  27. url: `${BASE_URL}${route.path}`,
  28. priority: route.priority,
  29. changefreq: route.changefreq,
  30. })
  31. }
  32. return routes
  33. }
  34. async function getDocsRoutes(): Promise<SitemapEntry[]> {
  35. const routes: SitemapEntry[] = []
  36. try {
  37. const files = await readdir(DOCS_DIR)
  38. for (const file of files) {
  39. if (!file.endsWith(".mdx")) continue
  40. const slug = file.replace(".mdx", "")
  41. const path = slug === "index" ? "/docs/" : `/docs/${slug}`
  42. routes.push({
  43. url: `${BASE_URL}${path}`,
  44. priority: slug === "index" ? 0.9 : 0.7,
  45. changefreq: "weekly",
  46. })
  47. }
  48. } catch (error) {
  49. console.error("Error reading docs directory:", error)
  50. }
  51. return routes
  52. }
  53. function generateSitemapXML(entries: SitemapEntry[]): string {
  54. const urls = entries
  55. .map(
  56. (entry) => ` <url>
  57. <loc>${entry.url}</loc>
  58. <changefreq>${entry.changefreq}</changefreq>
  59. <priority>${entry.priority}</priority>
  60. </url>`,
  61. )
  62. .join("\n")
  63. return `<?xml version="1.0" encoding="UTF-8"?>
  64. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  65. ${urls}
  66. </urlset>`
  67. }
  68. async function main() {
  69. console.log("Generating sitemap...")
  70. const mainRoutes = await getMainRoutes()
  71. const docsRoutes = await getDocsRoutes()
  72. const allRoutes = [...mainRoutes, ...docsRoutes]
  73. console.log(`Found ${mainRoutes.length} main routes`)
  74. console.log(`Found ${docsRoutes.length} docs routes`)
  75. console.log(`Total: ${allRoutes.length} routes`)
  76. const xml = generateSitemapXML(allRoutes)
  77. const outputPath = join(PUBLIC_DIR, "sitemap.xml")
  78. await writeFile(outputPath, xml, "utf-8")
  79. console.log(`✓ Sitemap generated at ${outputPath}`)
  80. }
  81. main()