-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.ts
69 lines (59 loc) · 1.65 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import * as fs from 'fs'
import prisma from './lib/prisma'
import { dateToUrl } from './lib/date'
import type { Post } from '@prisma/client'
import * as path from 'path'
const host = 'https://techcrunchjapan.com'
;(async () => {
let posts: {
[key: string]: { slug: string; createdAt: Date; updatedAt: Date }[]
} = {}
// for (
// let index = 0;
// index < Math.ceil((await prisma.post.count()) / 10000);
// index++
// ) {
const years = new Array(2022 - 2006 + 1)
.fill('')
.map((_, index) => 2006 + index)
for (const year of years) {
const _posts = await prisma.post.findMany({
select: {
slug: true,
createdAt: true,
updatedAt: true,
},
where: {
createdAt: {
gte: `${year}-01-01T00:00:00+09:00`,
lt: `${year + 1}-01-01T00:00:00+09:00`,
},
},
orderBy: {
createdAt: 'desc',
},
take: 10000,
})
posts = { ...posts, [year]: _posts }
}
// }
for (const [year, _posts] of Object.entries(posts)) {
const postsXml = _posts.map(
(post) =>
`<url><loc>${host}${dateToUrl(post.createdAt)}/${post.slug
.replaceAll('&', '&')
.replaceAll(`'`, ''')
.replaceAll(`"`, '"')
.replaceAll('>', '>')
.replaceAll('<', '<')}/</loc></url>`
)
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${postsXml.join('\n')}
</urlset>
`
const _path = `public/${year}.xml`
await fs.promises.mkdir(path.dirname(_path), { recursive: true })
await fs.promises.writeFile(_path, xml)
}
})()