forked from matklad/matklad.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
185 lines (166 loc) · 4.7 KB
/
build.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
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
174
175
176
177
178
179
180
181
182
183
184
185
import * as async from "std/async/mod.ts";
import * as fs from "std/fs/mod.ts";
import * as templates from "./templates.ts";
import * as djot from "./djot.ts";
import { HtmlString } from "./templates.ts";
export async function watch(params: { filter: string }) {
let signal = async.deferred();
(async () => {
let build_id = 0;
while (await signal) {
signal = async.deferred();
console.log(`rebuild #${build_id}`);
build_id += 1;
await build({
update: true,
spell: false,
profile: false,
filter: params.filter,
});
}
})();
signal.resolve(true);
const rebuild_debounced = async.debounce(
() => signal.resolve(true),
16,
);
for await (const event of Deno.watchFs("./src", { recursive: true })) {
if (event.kind == "access") continue;
await rebuild_debounced();
}
signal.resolve(false);
}
class Ctx {
constructor(
public read_ms: number = 0,
public parse_ms: number = 0,
public render_ms: number = 0,
public collect_ms: number = 0,
public total_ms: number = 0,
) {}
}
export async function build(params: {
update: boolean;
spell: boolean;
profile: boolean;
filter: string;
}) {
const t = performance.now();
const ctx = new Ctx();
if (params.update) {
await Deno.mkdir("./out/res", { recursive: true });
} else {
await fs.emptyDir("./out/res");
}
const posts = await collect_posts(ctx, params.filter);
await update_file("out/res/index.html", templates.post_list(posts).value);
await update_file("out/res/feed.xml", templates.feed(posts).value);
for (const post of posts) {
await update_file(
`out/res${post.path}`,
templates.post(post, params.spell).value,
);
}
const pages = ["about", "resume", "links", "style"];
for (const page of pages) {
const text = await Deno.readTextFile(`src/${page}.dj`);
const ast = await djot.parse(text);
const html = djot.render(ast, {});
await update_file(`out/res/${page}.html`, templates.page(page, html).value);
}
const paths = [
"favicon.svg",
"favicon.png",
"resume.pdf",
"css/*",
"assets/*",
"assets/resilient-parsing/*",
];
for (const path of paths) {
await update_path(path);
}
ctx.total_ms = performance.now() - t;
console.log(`${ctx.total_ms}ms`);
if (params.profile) console.log(JSON.stringify(ctx));
}
async function update_file(path: string, content: Uint8Array | string) {
if (!content) return;
await fs.ensureFile(path);
await fs.ensureDir("./out/tmp");
const temp = await Deno.makeTempFile({ dir: "./out/tmp" });
if (content instanceof Uint8Array) {
await Deno.writeFile(temp, content);
} else {
await Deno.writeTextFile(temp, content);
}
await Deno.rename(temp, path);
}
async function update_path(path: string) {
if (path.endsWith("*")) {
const dir = path.replace("*", "");
const futs = [];
for await (const entry of Deno.readDir(`src/${dir}`)) {
if (entry.isFile) {
futs.push(update_path(`${dir}/${entry.name}`));
}
}
await Promise.all(futs);
} else {
await update_file(
`out/res/${path}`,
await Deno.readFile(`src/${path}`),
);
}
}
export type Post = {
year: number;
month: number;
day: number;
slug: string;
date: Date;
title: string;
path: string;
src: string;
content: HtmlString;
summary: string;
};
async function collect_posts(ctx: Ctx, filter: string): Promise<Post[]> {
const start = performance.now();
const posts = [];
for await (const entry of fs.walk("./src/posts", { includeDirs: false })) {
if (!entry.name.endsWith(".dj")) continue;
if (filter !== "") {
if (entry.name.indexOf(filter) === -1) continue;
}
const [, y, m, d, slug] = entry.name.match(
/^(\d\d\d\d)-(\d\d)-(\d\d)-(.*)\.dj$/,
)!;
const [year, month, day] = [y, m, d].map((it) => parseInt(it, 10));
const date = new Date(Date.UTC(year, month - 1, day));
let t = performance.now();
const text = await Deno.readTextFile(entry.path);
ctx.read_ms += performance.now() - t;
t = performance.now();
const ast = djot.parse(text);
ctx.parse_ms += performance.now() - t;
t = performance.now();
const render_ctx = { date, summary: undefined, title: undefined };
const html = djot.render(ast, render_ctx);
ctx.render_ms += performance.now() - t;
posts.push({
year,
month,
day,
slug,
date,
title: render_ctx.title!,
content: html,
summary: render_ctx.summary!,
path: `/${y}/${m}/${d}/${slug}.html`,
src: `/src/posts/${y}-${m}-${d}-${slug}.dj`,
});
}
posts.sort((l, r) => l.path < r.path ? 1 : -1);
ctx.collect_ms = performance.now() - start;
return posts;
}