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
|
// Generates the sources that can't exist at runtime on Workers:
//
// src/views.generated.ts - views/*.eta precompiled into plain functions.
// Workers disallow runtime code generation (`new Function`), so Eta cannot
// compile templates there at request time.
// src/commit.generated.ts - the commit this build was made from, for the
// page footer. Workers can't shell out to git at request time either.
import { execFileSync } from "node:child_process";
import { Eta } from "eta";
import fs from "node:fs";
import path from "node:path";
const root = path.join(import.meta.dirname, "..");
const viewsDir = path.join(root, "views");
const eta = new Eta({ views: viewsDir });
const names = fs
.readdirSync(viewsDir)
.filter((name) => name.endsWith(".eta"))
.sort();
let out = `// @ts-nocheck
// Generated by scripts/build.ts -- do not edit.
export const views = {
`;
for (const name of names) {
const source = fs.readFileSync(path.join(viewsDir, name), "utf8");
const body = eta.compileToString(source);
out += ` ${JSON.stringify(name)}: function (it, options) {\n${body}\n },\n`;
}
out += "};\n";
fs.writeFileSync(path.join(root, "src", "views.generated.ts"), out);
console.log(`Compiled ${names.length} templates to src/views.generated.ts`);
// Untagged repositories describe as a bare short hash; --dirty marks a build
// made from an unclean working copy.
let commit = "unknown";
try {
commit = execFileSync("git", ["describe", "--tags", "--always", "--dirty"], {
cwd: root,
encoding: "utf8",
}).trim();
} catch {
// Building from a source copy without git history.
}
fs.writeFileSync(
path.join(root, "src", "commit.generated.ts"),
`// Generated by scripts/build.ts -- do not edit.\nexport const commit = ${JSON.stringify(commit)};\n`,
);
console.log(`Wrote commit ${commit} to src/commit.generated.ts`);
|