Skip to content

Build your personal knowledge base with Trilium Notes

chore(desktop): test the locale trim against Electron's real files

Elian Doran5cd034676ed0
diff --git a/apps/desktop/electron-forge/electron-locales.json b/apps/desktop/electron-forge/electron-locales.json
new file mode 100644
index 0000000000..8c020f9c4d
--- /dev/null
+++ b/apps/desktop/electron-forge/electron-locales.json
@@ -0,0 +1,398 @@
+{
+ "electronVersion": "45.0.0",
+ "linux": [
+ "af.pak",
+ "am.pak",
+ "ar.pak",
+ "bg.pak",
+ "bn.pak",
+ "ca.pak",
+ "cs.pak",
+ "da.pak",
+ "de.pak",
+ "el.pak",
+ "en-GB.pak",
+ "en-US.pak",
+ "es-419.pak",
+ "es.pak",
+ "et.pak",
+ "fa.pak",
+ "fi.pak",
+ "fil.pak",
+ "fr.pak",
+ "gu.pak",
+ "he.pak",
+ "hi.pak",
+ "hr.pak",
+ "hu.pak",
+ "id.pak",
+ "it.pak",
+ "ja.pak",
+ "kn.pak",
+ "ko.pak",
+ "lt.pak",
+ "lv.pak",
+ "ml.pak",
+ "mr.pak",
+ "ms.pak",
+ "nb.pak",
+ "nl.pak",
+ "pl.pak",
+ "pt-BR.pak",
+ "pt-PT.pak",
+ "ro.pak",
+ "ru.pak",
+ "sk.pak",
+ "sl.pak",
+ "sr.pak",
+ "sv.pak",
+ "sw.pak",
+ "ta.pak",
+ "te.pak",
+ "th.pak",
+ "tr.pak",
+ "uk.pak",
+ "ur.pak",
+ "vi.pak",
+ "zh-CN.pak",
+ "zh-TW.pak"
+ ],
+ "win32": [
+ "af.pak",
+ "am.pak",
+ "ar.pak",
+ "bg.pak",
+ "bn.pak",
+ "ca.pak",
+ "cs.pak",
+ "da.pak",
+ "de.pak",
+ "el.pak",
+ "en-GB.pak",
+ "en-US.pak",
+ "es-419.pak",
+ "es.pak",
+ "et.pak",
+ "fa.pak",
+ "fi.pak",
+ "fil.pak",
+ "fr.pak",
+ "gu.pak",
+ "he.pak",
+ "hi.pak",
+ "hr.pak",
+ "hu.pak",
+ "id.pak",
+ "it.pak",
+ "ja.pak",
+ "kn.pak",
+ "ko.pak",
+ "lt.pak",
+ "lv.pak",
+ "ml.pak",
+ "mr.pak",
+ "ms.pak",
+ "nb.pak",
+ "nl.pak",
+ "pl.pak",
+ "pt-BR.pak",
+ "pt-PT.pak",
+ "ro.pak",
+ "ru.pak",
+ "sk.pak",
+ "sl.pak",
+ "sr.pak",
+ "sv.pak",
+ "sw.pak",
+ "ta.pak",
+ "te.pak",
+ "th.pak",
+ "tr.pak",
+ "uk.pak",
+ "ur.pak",
+ "vi.pak",
+ "zh-CN.pak",
+ "zh-TW.pak"
+ ],
+ "darwin": {
+ "resources": [
+ "af.lproj",
+ "am.lproj",
+ "ar.lproj",
+ "bg.lproj",
+ "bn.lproj",
+ "ca.lproj",
+ "cs.lproj",
+ "da.lproj",
+ "de.lproj",
+ "el.lproj",
+ "en.lproj",
+ "en_GB.lproj",
+ "es.lproj",
+ "es_419.lproj",
+ "et.lproj",
+ "fa.lproj",
+ "fi.lproj",
+ "fil.lproj",
+ "fr.lproj",
+ "gu.lproj",
+ "he.lproj",
+ "hi.lproj",
+ "hr.lproj",
+ "hu.lproj",
+ "id.lproj",
+ "it.lproj",
+ "ja.lproj",
+ "kn.lproj",
+ "ko.lproj",
+ "lt.lproj",
+ "lv.lproj",
+ "ml.lproj",
+ "mr.lproj",
+ "ms.lproj",
+ "nb.lproj",
+ "nl.lproj",
+ "pl.lproj",
+ "pt_BR.lproj",
+ "pt_PT.lproj",
+ "ro.lproj",
+ "ru.lproj",
+ "sk.lproj",
+ "sl.lproj",
+ "sr.lproj",
+ "sv.lproj",
+ "sw.lproj",
+ "ta.lproj",
+ "te.lproj",
+ "th.lproj",
+ "tr.lproj",
+ "uk.lproj",
+ "ur.lproj",
+ "vi.lproj",
+ "zh_CN.lproj",
+ "zh_TW.lproj"
+ ],
+ "frameworkResources": [
+ "af.lproj",
+ "af_FEMININE.lproj",
+ "af_MASCULINE.lproj",
+ "af_NEUTER.lproj",
+ "am.lproj",
+ "am_FEMININE.lproj",
+ "am_MASCULINE.lproj",
+ "am_NEUTER.lproj",
+ "ar.lproj",
+ "ar_FEMININE.lproj",
+ "ar_MASCULINE.lproj",
+ "ar_NEUTER.lproj",
+ "bg.lproj",
+ "bg_FEMININE.lproj",
+ "bg_MASCULINE.lproj",
+ "bg_NEUTER.lproj",
+ "bn.lproj",
+ "bn_FEMININE.lproj",
+ "bn_MASCULINE.lproj",
+ "bn_NEUTER.lproj",
+ "ca.lproj",
+ "ca_FEMININE.lproj",
+ "ca_MASCULINE.lproj",
+ "ca_NEUTER.lproj",
+ "cs.lproj",
+ "cs_FEMININE.lproj",
+ "cs_MASCULINE.lproj",
+ "cs_NEUTER.lproj",
+ "da.lproj",
+ "da_FEMININE.lproj",
+ "da_MASCULINE.lproj",
+ "da_NEUTER.lproj",
+ "de.lproj",
+ "de_FEMININE.lproj",
+ "de_MASCULINE.lproj",
+ "de_NEUTER.lproj",
+ "el.lproj",
+ "el_FEMININE.lproj",
+ "el_MASCULINE.lproj",
+ "el_NEUTER.lproj",
+ "en.lproj",
+ "en_FEMININE.lproj",
+ "en_GB.lproj",
+ "en_GB_FEMININE.lproj",
+ "en_GB_MASCULINE.lproj",
+ "en_GB_NEUTER.lproj",
+ "en_MASCULINE.lproj",
+ "en_NEUTER.lproj",
+ "es.lproj",
+ "es_419.lproj",
+ "es_419_FEMININE.lproj",
+ "es_419_MASCULINE.lproj",
+ "es_419_NEUTER.lproj",
+ "es_FEMININE.lproj",
+ "es_MASCULINE.lproj",
+ "es_NEUTER.lproj",
+ "et.lproj",
+ "et_FEMININE.lproj",
+ "et_MASCULINE.lproj",
+ "et_NEUTER.lproj",
+ "fa.lproj",
+ "fa_FEMININE.lproj",
+ "fa_MASCULINE.lproj",
+ "fa_NEUTER.lproj",
+ "fi.lproj",
+ "fi_FEMININE.lproj",
+ "fi_MASCULINE.lproj",
+ "fi_NEUTER.lproj",
+ "fil.lproj",
+ "fil_FEMININE.lproj",
+ "fil_MASCULINE.lproj",
+ "fil_NEUTER.lproj",
+ "fr.lproj",
+ "fr_FEMININE.lproj",
+ "fr_MASCULINE.lproj",
+ "fr_NEUTER.lproj",
+ "gu.lproj",
+ "gu_FEMININE.lproj",
+ "gu_MASCULINE.lproj",
+ "gu_NEUTER.lproj",
+ "he.lproj",
+ "he_FEMININE.lproj",
+ "he_MASCULINE.lproj",
+ "he_NEUTER.lproj",
+ "hi.lproj",
+ "hi_FEMININE.lproj",
+ "hi_MASCULINE.lproj",
+ "hi_NEUTER.lproj",
+ "hr.lproj",
+ "hr_FEMININE.lproj",
+ "hr_MASCULINE.lproj",
+ "hr_NEUTER.lproj",
+ "hu.lproj",
+ "hu_FEMININE.lproj",
+ "hu_MASCULINE.lproj",
+ "hu_NEUTER.lproj",
+ "id.lproj",
+ "id_FEMININE.lproj",
+ "id_MASCULINE.lproj",
+ "id_NEUTER.lproj",
+ "it.lproj",
+ "it_FEMININE.lproj",
+ "it_MASCULINE.lproj",
+ "it_NEUTER.lproj",
+ "ja.lproj",
+ "ja_FEMININE.lproj",
+ "ja_MASCULINE.lproj",
+ "ja_NEUTER.lproj",
+ "kn.lproj",
+ "kn_FEMININE.lproj",
+ "kn_MASCULINE.lproj",
+ "kn_NEUTER.lproj",
+ "ko.lproj",
+ "ko_FEMININE.lproj",
+ "ko_MASCULINE.lproj",
+ "ko_NEUTER.lproj",
+ "lt.lproj",
+ "lt_FEMININE.lproj",
+ "lt_MASCULINE.lproj",
+ "lt_NEUTER.lproj",
+ "lv.lproj",
+ "lv_FEMININE.lproj",
+ "lv_MASCULINE.lproj",
+ "lv_NEUTER.lproj",
+ "ml.lproj",
+ "ml_FEMININE.lproj",
+ "ml_MASCULINE.lproj",
+ "ml_NEUTER.lproj",
+ "mr.lproj",
+ "mr_FEMININE.lproj",
+ "mr_MASCULINE.lproj",
+ "mr_NEUTER.lproj",
+ "ms.lproj",
+ "ms_FEMININE.lproj",
+ "ms_MASCULINE.lproj",
+ "ms_NEUTER.lproj",
+ "nb.lproj",
+ "nb_FEMININE.lproj",
+ "nb_MASCULINE.lproj",
+ "nb_NEUTER.lproj",
+ "nl.lproj",
+ "nl_FEMININE.lproj",
+ "nl_MASCULINE.lproj",
+ "nl_NEUTER.lproj",
+ "pl.lproj",
+ "pl_FEMININE.lproj",
+ "pl_MASCULINE.lproj",
+ "pl_NEUTER.lproj",
+ "pt_BR.lproj",
+ "pt_BR_FEMININE.lproj",
+ "pt_BR_MASCULINE.lproj",
+ "pt_BR_NEUTER.lproj",
+ "pt_PT.lproj",
+ "pt_PT_FEMININE.lproj",
+ "pt_PT_MASCULINE.lproj",
+ "pt_PT_NEUTER.lproj",
+ "ro.lproj",
+ "ro_FEMININE.lproj",
+ "ro_MASCULINE.lproj",
+ "ro_NEUTER.lproj",
+ "ru.lproj",
+ "ru_FEMININE.lproj",
+ "ru_MASCULINE.lproj",
+ "ru_NEUTER.lproj",
+ "sk.lproj",
+ "sk_FEMININE.lproj",
+ "sk_MASCULINE.lproj",
+ "sk_NEUTER.lproj",
+ "sl.lproj",
+ "sl_FEMININE.lproj",
+ "sl_MASCULINE.lproj",
+ "sl_NEUTER.lproj",
+ "sr.lproj",
+ "sr_FEMININE.lproj",
+ "sr_MASCULINE.lproj",
+ "sr_NEUTER.lproj",
+ "sv.lproj",
+ "sv_FEMININE.lproj",
+ "sv_MASCULINE.lproj",
+ "sv_NEUTER.lproj",
+ "sw.lproj",
+ "sw_FEMININE.lproj",
+ "sw_MASCULINE.lproj",
+ "sw_NEUTER.lproj",
+ "ta.lproj",
+ "ta_FEMININE.lproj",
+ "ta_MASCULINE.lproj",
+ "ta_NEUTER.lproj",
+ "te.lproj",
+ "te_FEMININE.lproj",
+ "te_MASCULINE.lproj",
+ "te_NEUTER.lproj",
+ "th.lproj",
+ "th_FEMININE.lproj",
+ "th_MASCULINE.lproj",
+ "th_NEUTER.lproj",
+ "tr.lproj",
+ "tr_FEMININE.lproj",
+ "tr_MASCULINE.lproj",
+ "tr_NEUTER.lproj",
+ "uk.lproj",
+ "uk_FEMININE.lproj",
+ "uk_MASCULINE.lproj",
+ "uk_NEUTER.lproj",
+ "ur.lproj",
+ "ur_FEMININE.lproj",
+ "ur_MASCULINE.lproj",
+ "ur_NEUTER.lproj",
+ "vi.lproj",
+ "vi_FEMININE.lproj",
+ "vi_MASCULINE.lproj",
+ "vi_NEUTER.lproj",
+ "zh_CN.lproj",
+ "zh_CN_FEMININE.lproj",
+ "zh_CN_MASCULINE.lproj",
+ "zh_CN_NEUTER.lproj",
+ "zh_TW.lproj",
+ "zh_TW_FEMININE.lproj",
+ "zh_TW_MASCULINE.lproj",
+ "zh_TW_NEUTER.lproj"
+ ]
+ }
+}
diff --git a/apps/desktop/electron-forge/trim-locales.spec.ts b/apps/desktop/electron-forge/trim-locales.spec.ts
index f15397f9c8..cefd4b0fed 100644
--- a/apps/desktop/electron-forge/trim-locales.spec.ts
+++ b/apps/desktop/electron-forge/trim-locales.spec.ts
@@ -4,17 +4,10 @@ import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
+import packageJson from "../package.json" with { type: "json" };
+import electronLocales from "./electron-locales.json" with { type: "json" };
import { trimElectronLocales } from "./trim-locales.js";
-// The locale files Electron 44 ships on Linux and Windows.
-const ELECTRON_PAKS = [
- "af", "am", "ar", "bg", "bn", "ca", "cs", "da", "de", "el", "en-GB", "en-US",
- "es", "es-419", "et", "fa", "fi", "fil", "fr", "gu", "he", "hi", "hr", "hu",
- "id", "it", "ja", "kn", "ko", "lt", "lv", "ml", "mr", "ms", "nb", "nl", "pl",
- "pt-BR", "pt-PT", "ro", "ru", "sk", "sl", "sr", "sv", "sw", "ta", "te", "th",
- "tr", "uk", "ur", "vi", "zh-CN", "zh-TW"
-];
-
const tempDirs: string[] = [];
function makeLocaleDir(entries: string[], asDirectories = false): string {
@@ -38,13 +31,22 @@ afterEach(() => {
});
describe("trimElectronLocales", () => {
- it("keeps exactly the exposed locales, resolving en-US.pak to the \"en\" entry", () => {
- const dir = makeLocaleDir(ELECTRON_PAKS.map((l) => `${l}.pak`));
+ it("runs against the locale files of the pinned Electron version", () => {
+ expect(
+ electronLocales.electronVersion,
+ "electron-locales.json is out of date; run `pnpm chore:update-electron-locales`"
+ ).toBe(packageJson.devDependencies.electron);
+ });
+
+ it.each([
+ [ "Linux", electronLocales.linux ],
+ [ "Windows", electronLocales.win32 ]
+ ])("keeps the exposed locales of the %s package, resolving en-US.pak to \"en\"", (_, paks) => {
+ const dir = makeLocaleDir(paks);
const { kept, removed } = trimElectronLocales([ dir ], false);
- expect(kept.size).toBe(24);
- expect(removed).toHaveLength(31);
+ expect(kept.size + removed.length).toBe(paks.length);
expect(existsSync(join(dir, "en-US.pak"))).toBe(true);
expect(existsSync(join(dir, "zh-CN.pak"))).toBe(true);
// Hebrew is content-only, so its Chromium locale goes.
@@ -52,21 +54,17 @@ describe("trimElectronLocales", () => {
});
it("removes mac .lproj directories, keeping underscore names and ignoring other entries", () => {
- const resources = makeLocaleDir([ "zh_CN.lproj", "he.lproj" ], true);
+ const resources = makeLocaleDir(electronLocales.darwin.resources, true);
writeFileSync(join(resources, "icon.icns"), "");
// Mac spreads locales over two directories; the keep check spans both.
const frameworkResources = makeLocaleDir(
- [ "en.lproj", "de.lproj", "es.lproj", "fr.lproj", "zh_TW.lproj", "ro.lproj", "cs.lproj",
- "da.lproj", "en_GB.lproj", "id.lproj", "it.lproj", "hi.lproj", "ja.lproj",
- "ko.lproj", "pt_BR.lproj", "pt_PT.lproj", "pl.lproj", "ru.lproj", "tr.lproj",
- "uk.lproj", "vi.lproj", "ar.lproj", "fa.lproj" ],
- true
+ electronLocales.darwin.frameworkResources, true
);
- const { kept, removed } = trimElectronLocales([ resources, frameworkResources ], true);
+ const { kept } = trimElectronLocales([ resources, frameworkResources ], true);
expect(kept).toContain("zh_CN");
- expect(removed).toEqual([ "he.lproj" ]);
+ expect(existsSync(join(resources, "zh_CN.lproj"))).toBe(true);
expect(existsSync(join(resources, "he.lproj"))).toBe(false);
expect(existsSync(join(resources, "icon.icns"))).toBe(true);
});
diff --git a/apps/desktop/tsconfig.forge.json b/apps/desktop/tsconfig.forge.json
index e4dafd7cd0..a626810fae 100644
--- a/apps/desktop/tsconfig.forge.json
+++ b/apps/desktop/tsconfig.forge.json
@@ -15,7 +15,8 @@
"**/*.ts",
"electron-forge/**/*.mts",
"../server/src/*.d.ts",
- "package.json"
+ "package.json",
+ "electron-forge/electron-locales.json"
],
"exclude": [
"eslint.config.js",
diff --git a/package.json b/package.json
index 052b311895..a64a5de83c 100644
--- a/package.json
+++ b/package.json
@@ -37,6 +37,7 @@
"chore:ci-update-nightly-version": "tsx ./scripts/update-nightly-version.ts",
"chore:release-notes-body": "node --experimental-strip-types ./scripts/release-notes-body.mts",
"chore:update-build-info": "tsx ./scripts/update-build-info.mts",
+ "chore:update-electron-locales": "tsx ./scripts/update-electron-locales.ts",
"chore:update-flake-electron": "tsx ./scripts/update-flake-electron.ts",
"chore:update-version": "tsx ./scripts/update-version.ts",
"chore:wipe-node-modules": "node --experimental-strip-types ./scripts/wipe-node-modules.mts",
diff --git a/scripts/update-electron-locales.spec.ts b/scripts/update-electron-locales.spec.ts
new file mode 100644
index 0000000000..89d0e6877b
--- /dev/null
+++ b/scripts/update-electron-locales.spec.ts
@@ -0,0 +1,112 @@
+import { describe, expect, it } from "vitest";
+
+import {
+ collectElectronLocales,
+ readCentralDirectoryNames,
+ readEndOfCentralDirectory
+} from "./update-electron-locales.js";
+
+describe("readCentralDirectoryNames", () => {
+ it("lists the names of every record, skipping extra fields and comments", () => {
+ const names = [ "locales/da.pak", "LICENSE" ];
+ const directory = centralDirectory(names, { extra: 4, comment: 3 });
+
+ expect(readCentralDirectoryNames(directory)).toEqual(names);
+ });
+
+ it("rejects a directory that ends in a truncated record", () => {
+ const directory = centralDirectory([ "locales/da.pak" ]);
+
+ expect(() => readCentralDirectoryNames(directory.subarray(0, directory.length - 2)))
+ .toThrow(/unreadable record/);
+ });
+});
+
+describe("readEndOfCentralDirectory", () => {
+ it("finds the record behind a zip comment", () => {
+ const tail = concat(
+ new Uint8Array(10), endOfCentralDirectory(1234, 56, 5), new Uint8Array(5)
+ );
+
+ expect(readEndOfCentralDirectory(tail)).toEqual({ offset: 1234, size: 56 });
+ });
+
+ it("refuses ZIP64 archives and data without a record", () => {
+ expect(() => readEndOfCentralDirectory(endOfCentralDirectory(0xffffffff, 56)))
+ .toThrow(/ZIP64/);
+ expect(() => readEndOfCentralDirectory(new Uint8Array(64))).toThrow(/not a zip/);
+ });
+});
+
+describe("collectElectronLocales", () => {
+ const framework
+ = "Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Resources";
+
+ it("picks the locale files out of each platform's entries", () => {
+ const locales = collectElectronLocales("45.0.0", {
+ linux: [ "locales/", "locales/en-US.pak", "locales/da.pak", "resources.pak" ],
+ win32: [ "locales/da.pak", "locales/sub/x.pak" ],
+ darwin: [
+ "Electron.app/Contents/Resources/da.lproj/",
+ "Electron.app/Contents/Resources/da.lproj/locale.pak",
+ "Electron.app/Contents/Resources/electron.icns",
+ `${framework}/da_FEMININE.lproj/locale.pak`,
+ `${framework}/da.lproj/locale.pak`
+ ]
+ });
+
+ expect(locales).toEqual({
+ electronVersion: "45.0.0",
+ linux: [ "da.pak", "en-US.pak" ],
+ win32: [ "da.pak" ],
+ darwin: {
+ resources: [ "da.lproj" ],
+ frameworkResources: [ "da.lproj", "da_FEMININE.lproj" ]
+ }
+ });
+ });
+
+ it("names every platform that yields no locale files", () => {
+ expect(() => collectElectronLocales("45.0.0", {
+ linux: [ "locales/da.pak" ],
+ win32: [],
+ darwin: [ "Electron.app/Contents/Resources/da.lproj/" ]
+ })).toThrow(/Electron 45\.0\.0 zips for: win32, darwin framework resources$/);
+ });
+});
+
+/** Central directory records for `names`, with zeroed fields apart from the lengths. */
+function centralDirectory(names: string[], { extra = 0, comment = 0 } = {}): Uint8Array {
+ const encoder = new TextEncoder();
+ return concat(...names.map((name) => {
+ const encodedName = encoder.encode(name);
+ const record = new Uint8Array(46 + encodedName.length + extra + comment);
+ const view = new DataView(record.buffer);
+ view.setUint32(0, 0x02014b50, true);
+ view.setUint16(28, encodedName.length, true);
+ view.setUint16(30, extra, true);
+ view.setUint16(32, comment, true);
+ record.set(encodedName, 46);
+ return record;
+ }));
+}
+
+function endOfCentralDirectory(offset: number, size: number, commentLength = 0): Uint8Array {
+ const record = new Uint8Array(22);
+ const view = new DataView(record.buffer);
+ view.setUint32(0, 0x06054b50, true);
+ view.setUint32(12, size, true);
+ view.setUint32(16, offset, true);
+ view.setUint16(20, commentLength, true);
+ return record;
+}
+
+function concat(...parts: Uint8Array[]): Uint8Array {
+ const result = new Uint8Array(parts.reduce((total, part) => total + part.length, 0));
+ let position = 0;
+ for (const part of parts) {
+ result.set(part, position);
+ position += part.length;
+ }
+ return result;
+}
diff --git a/scripts/update-electron-locales.ts b/scripts/update-electron-locales.ts
new file mode 100644
index 0000000000..11c4c5113c
--- /dev/null
+++ b/scripts/update-electron-locales.ts
@@ -0,0 +1,214 @@
+/**
+ * Records the Chromium locale files the pinned Electron release ships, for the
+ * `trim-locales.spec.ts` fixture in `apps/desktop/electron-forge/`.
+ *
+ * `trimElectronLocales` keeps the locale files whose names match an
+ * `electronLocale` in `LOCALES` and fails the packaging when one is missing, so
+ * its spec runs against the file names a real Electron build carries rather than
+ * a hand-kept list. The desktop unit tests never download Electron, so this
+ * script reads the names once per Electron version and commits them as
+ * `electron-locales.json`; the spec fails when that file names a version other
+ * than the one `apps/desktop/package.json` pins.
+ *
+ * Only the central directory at the end of each release zip is fetched, through
+ * HTTP range requests, so a run transfers a few hundred kilobytes instead of
+ * three ~130 MB archives. It is a no-op when the recorded version already
+ * matches. Usage:
+ *
+ * pnpm chore:update-electron-locales
+ */
+
+import { readFileSync, writeFileSync } from "node:fs";
+import { dirname, join, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
+
+import { readWantedElectronVersion } from "./update-flake-electron.js";
+
+const SCRIPT_PATH = fileURLToPath(import.meta.url);
+const ROOT = resolve(dirname(SCRIPT_PATH), "..");
+const DESKTOP_PACKAGE_JSON_PATH = join(ROOT, "apps", "desktop", "package.json");
+const FIXTURE_PATH = join(ROOT, "apps", "desktop", "electron-forge", "electron-locales.json");
+
+/**
+ * The zip comment is at most 65,535 bytes, so the end-of-central-directory
+ * record lies within this many bytes of the end.
+ */
+const EOCD_SEARCH_LENGTH = 22 + 0xffff;
+const EOCD_SIGNATURE = 0x06054b50;
+const CENTRAL_DIRECTORY_SIGNATURE = 0x02014b50;
+
+const PAK_PATTERN = /^locales\/([^/]+\.pak)$/;
+const MAC_RESOURCES_PATTERN = /^Electron\.app\/Contents\/Resources\/([^/]+\.lproj)\//;
+// `Electron Framework.framework/Resources` is a symlink into the current version.
+const MAC_FRAMEWORK_RESOURCES_PATTERN = new RegExp(
+ "^Electron\\.app/Contents/Frameworks/Electron Framework\\.framework/Versions/A/Resources/"
+ + "([^/]+\\.lproj)/"
+);
+
+export interface ElectronLocales {
+ electronVersion: string;
+ /** The `.pak` files under `locales/` in the Linux zip. */
+ linux: string[];
+ /** The `.pak` files under `locales/` in the Windows zip. */
+ win32: string[];
+ darwin: {
+ /** The `.lproj` directories under `Electron.app/Contents/Resources`. */
+ resources: string[];
+ /** The `.lproj` directories under `Electron Framework.framework/Resources`. */
+ frameworkResources: string[];
+ };
+}
+
+export async function main() {
+ const wanted = readWantedElectronVersion(readFileSync(DESKTOP_PACKAGE_JSON_PATH, "utf-8"));
+ const recorded = readRecordedVersion();
+ if (wanted === recorded) {
+ console.log(`electron-locales.json already lists Electron ${recorded}; nothing to do.`);
+ return;
+ }
+
+ console.log(`Electron ${recorded ?? "(none)"} -> ${wanted}; reading the release zips.`);
+ const [ linux, win32, darwin ] = await Promise.all([
+ listZipEntries(releaseZipUrl(wanted, "linux-x64")),
+ listZipEntries(releaseZipUrl(wanted, "win32-x64")),
+ listZipEntries(releaseZipUrl(wanted, "darwin-arm64"))
+ ]);
+
+ const locales = collectElectronLocales(wanted, { linux, win32, darwin });
+ writeFileSync(FIXTURE_PATH, `${JSON.stringify(locales, null, 4)}\n`);
+ console.log(`Wrote ${FIXTURE_PATH}`);
+}
+
+/**
+ * Pick the locale files out of each platform's zip entry names. Throw when a
+ * platform yields none: an empty list means Electron moved its locales, and a
+ * fixture without them would let the spec pass against nothing.
+ */
+export function collectElectronLocales(
+ electronVersion: string,
+ entries: { linux: string[]; win32: string[]; darwin: string[] }
+): ElectronLocales {
+ const locales: ElectronLocales = {
+ electronVersion,
+ linux: matchEntries(entries.linux, PAK_PATTERN),
+ win32: matchEntries(entries.win32, PAK_PATTERN),
+ darwin: {
+ resources: matchEntries(entries.darwin, MAC_RESOURCES_PATTERN),
+ frameworkResources: matchEntries(entries.darwin, MAC_FRAMEWORK_RESOURCES_PATTERN)
+ }
+ };
+
+ const empty = [
+ [ "linux", locales.linux ],
+ [ "win32", locales.win32 ],
+ [ "darwin resources", locales.darwin.resources ],
+ [ "darwin framework resources", locales.darwin.frameworkResources ]
+ ].filter(([ , names ]) => names.length === 0).map(([ platform ]) => platform);
+ if (empty.length > 0) {
+ throw new Error(
+ `Found no locale files in the Electron ${electronVersion} zips for: ${empty.join(", ")}`
+ );
+ }
+ return locales;
+}
+
+/** The distinct first capture group of every matching entry, sorted. */
+function matchEntries(entries: string[], pattern: RegExp): string[] {
+ const names = new Set<string>();
+ for (const entry of entries) {
+ const match = entry.match(pattern);
+ if (match) {
+ names.add(match[1]);
+ }
+ }
+ return [ ...names ].sort();
+}
+
+/**
+ * Find the end-of-central-directory record in the tail of a zip and return the
+ * central directory's offset and size. ZIP64 archives store those in another
+ * record; Electron's zips are far below its limits, so they are refused rather
+ * than parsed.
+ */
+export function readEndOfCentralDirectory(tail: Uint8Array): { offset: number; size: number; } {
+ const view = new DataView(tail.buffer, tail.byteOffset, tail.byteLength);
+ for (let position = tail.byteLength - 22; position >= 0; position--) {
+ if (view.getUint32(position, true) !== EOCD_SIGNATURE) {
+ continue;
+ }
+ const size = view.getUint32(position + 12, true);
+ const offset = view.getUint32(position + 16, true);
+ if (offset === 0xffffffff || size === 0xffffffff) {
+ throw new Error("The zip is a ZIP64 archive, which is not supported.");
+ }
+ return { offset, size };
+ }
+ throw new Error("No end-of-central-directory record found; the file is not a zip.");
+}
+
+/** The file names listed in a zip's central directory, in archive order. */
+export function readCentralDirectoryNames(centralDirectory: Uint8Array): string[] {
+ const { buffer, byteOffset, byteLength } = centralDirectory;
+ const view = new DataView(buffer, byteOffset, byteLength);
+ const decoder = new TextDecoder();
+ const names: string[] = [];
+ let position = 0;
+ while (position + 46 <= byteLength
+ && view.getUint32(position, true) === CENTRAL_DIRECTORY_SIGNATURE) {
+ const nameStart = position + 46;
+ const nameLength = view.getUint16(position + 28, true);
+ const extraLength = view.getUint16(position + 30, true);
+ const commentLength = view.getUint16(position + 32, true);
+ names.push(decoder.decode(centralDirectory.subarray(nameStart, nameStart + nameLength)));
+ position = nameStart + nameLength + extraLength + commentLength;
+ }
+ if (position !== byteLength) {
+ throw new Error(`The central directory ends in an unreadable record at byte ${position}.`);
+ }
+ return names;
+}
+
+function releaseZipUrl(version: string, platformTag: string): string {
+ const release = `https://github.com/electron/electron/releases/download/v${version}`;
+ return `${release}/electron-v${version}-${platformTag}.zip`;
+}
+
+async function listZipEntries(url: string): Promise<string[]> {
+ const head = await fetch(url, { method: "HEAD" });
+ if (!head.ok) {
+ throw new Error(
+ `Failed to fetch ${url}: HTTP ${head.status}. Is that Electron version released?`
+ );
+ }
+ const length = Number(head.headers.get("content-length"));
+ // The release host answers a suffix range (`bytes=-N`) with 501, so ask for explicit bytes.
+ const tail = await fetchRange(url, Math.max(0, length - EOCD_SEARCH_LENGTH), length - 1);
+ const { offset, size } = readEndOfCentralDirectory(tail);
+ return readCentralDirectoryNames(await fetchRange(url, offset, offset + size - 1));
+}
+
+async function fetchRange(url: string, start: number, end: number): Promise<Uint8Array> {
+ const response = await fetch(url, { headers: { Range: `bytes=${start}-${end}` } });
+ if (response.status !== 206) {
+ throw new Error(
+ `Expected bytes ${start}-${end} of ${url}, got HTTP ${response.status}.`
+ );
+ }
+ return new Uint8Array(await response.arrayBuffer());
+}
+
+function readRecordedVersion(): string | undefined {
+ try {
+ return (JSON.parse(readFileSync(FIXTURE_PATH, "utf-8")) as ElectronLocales).electronVersion;
+ } catch {
+ return undefined;
+ }
+}
+
+// Only when run as a script — the pure helpers above are imported by the spec.
+if (process.argv[1] === SCRIPT_PATH) {
+ main().catch((err) => {
+ console.error(err);
+ process.exit(1);
+ });
+}