aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorJoe Mou <dev@mou.fo>2026-08-11 08:37:25 -0400
committerJoe Mou <dev@mou.fo>2026-08-11 08:38:30 -0400
commitd76a46b37b12823b5b3976d129c2d4b672a99693 (patch)
tree5640077c30a71ac8eccdc66925a82e112aa45963 /scripts
parentde557ad66466261ae578ea9342f32d78a139fbc9 (diff)
Show the build's commit in the page footer
Workers can't shell out at request time, so the commit has to be baked in before wrangler bundles the sources. compile-views.ts grows a second job and becomes scripts/build.ts: alongside the precompiled views it writes src/commit.generated.ts with `git describe --tags --always --dirty`, which yields a bare short hash until the repo is tagged and marks builds made from an unclean working copy. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'scripts')
-rw-r--r--scripts/build.ts52
-rw-r--r--scripts/compile-views.ts29
2 files changed, 52 insertions, 29 deletions
diff --git a/scripts/build.ts b/scripts/build.ts
new file mode 100644
index 0000000..766e5e8
--- /dev/null
+++ b/scripts/build.ts
@@ -0,0 +1,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`);
diff --git a/scripts/compile-views.ts b/scripts/compile-views.ts
deleted file mode 100644
index 785efc1..0000000
--- a/scripts/compile-views.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-// Precompiles views/*.eta into plain functions for the Cloudflare Workers
-// build (src/views.generated.ts). Workers disallow runtime code generation
-// (`new Function`), so Eta cannot compile templates there at request time.
-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/compile-views.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`);