-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-astro-vercel-sitemap.ts
33 lines (31 loc) · 1.3 KB
/
fix-astro-vercel-sitemap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import type { AstroIntegration } from "astro"
import { readdir, cp } from "node:fs/promises"
import * as path from "node:path"
const integrationName = "fix-astro-vercel-sitemap"
const DIST_DIR = "./dist"
const VERCEL_OUTPUT_DIR = "./.vercel/output/static"
// https://github.com/withastro/adapters/issues/445
export const fixAstroVercelSitemap = (): AstroIntegration => ({
name: integrationName,
hooks: {
"astro:build:done": async ({ logger }) => {
const buildLogger = logger.fork(integrationName)
try {
const files = await readdir(DIST_DIR)
const sitemaps = files.filter(
(file) =>
path.extname(file).toLowerCase() === ".xml" &&
path.basename(file).toLowerCase().startsWith("sitemap"),
)
for (const sitemap of sitemaps) {
const sourcePath = path.join(DIST_DIR, sitemap)
const destPath = path.join(VERCEL_OUTPUT_DIR, sitemap)
await cp(sourcePath, destPath)
}
buildLogger.info(`Copied ${sitemaps.length} sitemaps to ${VERCEL_OUTPUT_DIR}`)
} catch (error) {
buildLogger.error(`Error copying files: ${error}`)
}
},
},
})