-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
173 lines (150 loc) · 4.37 KB
/
build.ts
File metadata and controls
173 lines (150 loc) · 4.37 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import fs from 'node:fs';
import path from 'node:path';
import { config } from './src/config';
import { getMeta } from './src/lib/getMeta';
import { getPosts } from './src/lib/getPosts';
import { PostRepository } from './src/repositories/PostRepository';
import { createRss } from './src/utils/rss';
import { createSitemap } from './src/utils/sitemap';
const POST_ROOT = path.join(process.cwd(), 'posts');
const repository = new PostRepository(POST_ROOT);
const removeRecursively = (directory: string) => {
const files = fs.readdirSync(directory, { withFileTypes: true });
for (const file of files) {
const filename = `${directory}/${file.name}`;
if (file.isFile()) {
fs.unlinkSync(filename);
} else {
removeRecursively(filename);
}
}
fs.rmdirSync(directory);
};
const capture = async (url: string, filename: string) => {
const dir = path.dirname(filename);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const { render } = await import('./dist/server/entry-server.js');
const { ssr, helmet } = await render(url);
const template = fs.readFileSync('./dist/client/index.html').toString();
const html = template
.replace('<!--app-body-->', ssr)
.replace(
'<!--app-head-->',
`${helmet.title.toString()}${helmet.meta.toString()}`,
);
fs.writeFileSync(filename, html);
console.log(`Capture ${url} -> ${filename} complete`);
};
const copyRecursively = (src: string, dest: string) => {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
const files = fs.readdirSync(src, { withFileTypes: true });
for (const file of files) {
const _src = `${src}/${file.name}`;
const _dest = `${dest}/${file.name}`;
if (file.isFile()) {
fs.copyFile(_src, _dest, () => {
console.log(`Copy ${_src} -> ${_dest} complete`);
});
} else {
copyRecursively(_src, _dest);
}
}
};
const dest = './public';
(async () => {
if (fs.existsSync(dest)) {
removeRecursively(dest);
}
fs.mkdirSync(dest);
console.log('Start build');
const posts = repository.getPosts();
const _posts = getPosts(POST_ROOT);
const postNames = _posts
.filter((post) => !getMeta(post).meta.draft)
.map((post) => post.replace(/.+\/posts\/(.+)\/index.md$/, '$1'));
const tags = Array.from(
new Set(
_posts
.map((post) => getMeta(post).meta.tags)
.reduce((acc, cur) => {
if (!cur) return acc;
return acc.concat(cur);
}, []),
),
);
// static files & assets
copyRecursively('./dist/client', dest);
for (const post of postNames) {
const src = path.join(POST_ROOT, post, 'assets');
if (fs.existsSync(src)) {
copyRecursively(src, `${dest}/post/${post}/assets`);
}
}
// home
await capture('/', `${dest}/index.html`);
// posts
await capture('/archive', `${dest}/archive/index.html`);
for (const post of postNames) {
await capture(`/post/${post}/`, `${dest}/post/${post}/index.html`);
}
for (const tag of tags) {
await capture(`/tag/${tag}`, `${dest}/tag/${tag}/index.html`);
}
// search page
await capture('/search', `${dest}/search/index.html`);
// sitemap, rss
fs.writeFileSync(
`${dest}/sitemap.xml`,
createSitemap([
{
url: config.host,
changeFrequency: 'weekly',
priority: 0.9,
},
...posts.map(
(post) =>
({
url: `${config.host}${post.url}`,
modifiedAt: post.meta.updated,
changeFrequency: 'weekly',
priority: 0.7,
}) as const,
),
...tags.map(
(tag) =>
({
url: `${config.host}/tag/${tag}`,
changeFrequency: 'weekly',
priority: 0.3,
}) as const,
),
{
url: `${config.host}/archive`,
changeFrequency: 'weekly',
priority: 0.1,
},
]),
);
console.log('Sitemap creation complete');
fs.writeFileSync(
`${dest}/rss.xml`,
createRss({
title: config.blogName,
link: config.host,
description: config.description,
items: posts.map((post) => ({
title: post.meta.title,
link: `${config.host}${post.url}`,
description: post.content,
author: config.author,
published: post.meta.updated,
})),
}),
);
console.log('RSS creation complete');
console.log('Build complete');
})();