Skip to content

Commit d6de083

Browse files
committed
feat(cli): stamp font compiler version in localized HTML
1 parent ec68d40 commit d6de083

3 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎packages/cli/src/fontLocalize.test.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it, vi } from "vitest";
2-
import { runFontLocalize, type FontLocalizeIo } from "./fontLocalize.js";
2+
import { runFontLocalize, stampFontCompilerVersion, type FontLocalizeIo } from "./fontLocalize.js";
33

44
function makeIo(input: string): {
55
io: FontLocalizeIo;
@@ -70,3 +70,17 @@ describe("runFontLocalize", () => {
7070
expect(harness.errors.join(" ")).toContain("empty output");
7171
});
7272
});
73+
74+
describe("stampFontCompilerVersion", () => {
75+
it("records the compiler version inside the document head", () => {
76+
const stamped = stampFontCompilerVersion(
77+
"<!doctype html><html><head><title>x</title></head><body></body></html>",
78+
"0.8.15",
79+
);
80+
81+
expect(stamped).toContain('<meta name="hyperframes-font-compiler-version" content="0.8.15">');
82+
expect(stamped.indexOf("hyperframes-font-compiler-version")).toBeLessThan(
83+
stamped.indexOf("</head>"),
84+
);
85+
});
86+
});

‎packages/cli/src/fontLocalize.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ export interface FontLocalizeIo {
44
writeError(value: string): void;
55
}
66

7+
export function stampFontCompilerVersion(html: string, version: string): string {
8+
const safeVersion = version.replace(/[^A-Za-z0-9.+-]/g, "") || "unknown";
9+
const tag = `<meta name="hyperframes-font-compiler-version" content="${safeVersion}">`;
10+
const headClose = html.search(/<\/head\s*>/i);
11+
if (headClose >= 0) return `${html.slice(0, headClose)}${tag}${html.slice(headClose)}`;
12+
const doctype = /^\s*<!doctype[^>]*>/i.exec(html);
13+
if (!doctype) return `${tag}${html}`;
14+
const insertAt = doctype.index + doctype[0].length;
15+
return `${html.slice(0, insertAt)}${tag}${html.slice(insertAt)}`;
16+
}
17+
718
function safeErrorName(error: unknown): string {
819
const name = error instanceof Error ? error.name : "UnknownError";
920
return /^[A-Za-z][A-Za-z0-9]*$/.test(name) ? name : "Error";

‎packages/cli/src/fontLocalizeCli.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// fallow-ignore-file unused-file
22
import { injectDeterministicFontFaces } from "@hyperframes/producer";
3-
import { runFontLocalize } from "./fontLocalize.js";
3+
import { runFontLocalize, stampFontCompilerVersion } from "./fontLocalize.js";
4+
import { VERSION } from "./version.js";
45

56
async function readStdin(): Promise<string> {
67
const chunks: Buffer[] = [];
@@ -18,10 +19,12 @@ export async function main(): Promise<number> {
1819
writeOutput: (value) => process.stdout.write(value),
1920
writeError: (value) => process.stderr.write(value),
2021
},
21-
(html) =>
22-
injectDeterministicFontFaces(html, {
22+
async (html) => {
23+
const localized = await injectDeterministicFontFaces(html, {
2324
failClosedFontFetch: true,
2425
allowSystemFontCapture: false,
25-
}),
26+
});
27+
return stampFontCompilerVersion(localized, VERSION);
28+
},
2629
);
2730
}

0 commit comments

Comments
 (0)