Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(flatpak): target the packaging repo from the sources generator
Elian Doran31fd76dd0708
diff --git a/scripts/generate-flatpak-sources.mts b/scripts/generate-flatpak-sources.mtsindex 3dc51e2d3e..78427d8b69 100644--- a/scripts/generate-flatpak-sources.mts+++ b/scripts/generate-flatpak-sources.mts@@ -9,24 +9,28 @@** Usage:*- * node --experimental-strip-types ./scripts/generate-flatpak-sources.mts+ * pnpm exec tsx ./scripts/generate-flatpak-sources.mts [output]+ *+ * `output` is the file to write, or a directory to write `generated-sources.json`+ * into — pass the packaging repo checkout to update its tracked copy in place.+ * Defaults to `upload/generated-sources.json`.** Requires `flatpak-node-generator` on PATH, from the `node` subdirectory of* https://github.com/flatpak/flatpak-builder-tools.*/import { execFileSync } from "node:child_process";-import { mkdirSync, readFileSync, writeFileSync } from "node:fs";-import { dirname, join, relative } from "node:path";+import { mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";+import { dirname, join, relative, resolve } from "node:path";const ROOT = join(import.meta.dirname, "..");const PACKAGE_JSON_PATH = join(ROOT, "package.json");const LOCKFILE_PATH = join(ROOT, "pnpm-lock.yaml");-const OUTPUT_PATH = join(ROOT, "upload", "generated-sources.json");-const PNPM_MAJOR = 11;-// pnpm 11 reads its store from a v11 layout (a SQLite index.db); flatpak-node-generator-// defaults to v10 (per-package JSON index files), which pnpm 11 would not find.+const PNPM_MAJOR = 12;+// pnpm 12 still reads its store from the v11 layout (a SQLite index.db);+// flatpak-node-generator defaults to v10 (per-package JSON index files),+// which pnpm would not find.const STORE_VERSION = "v11";interface Source {@@ -35,17 +39,29 @@ interface Source {[key: string]: unknown;}-export function main() {+export function main(argv: string[]) {+ const outputPath = resolveOutputPath(argv[0]);checkPnpm(readFileSync(PACKAGE_JSON_PATH, "utf-8"));- generateSources();+ generateSources(outputPath);- const sources: Source[] = JSON.parse(readFileSync(OUTPUT_PATH, "utf-8"));+ const sources: Source[] = JSON.parse(readFileSync(outputPath, "utf-8"));const kept = filterSources(sources);- writeFileSync(OUTPUT_PATH, `${JSON.stringify(kept, null, 4)}\n`);- console.log(`Wrote ${relative(ROOT, OUTPUT_PATH)}: `+ writeFileSync(outputPath, `${JSON.stringify(kept, null, 4)}\n`);+ console.log(`Wrote ${relative(process.cwd(), outputPath)}: `+ `${kept.length} sources, ${sources.length - kept.length} dropped.`);}+/** The file to write: the argument, `generated-sources.json` under an argument naming a directory, or the default. */+export function resolveOutputPath(arg: string | undefined): string {+ if (!arg) {+ return join(ROOT, "upload", "generated-sources.json");+ }+ const path = resolve(arg);+ return statSync(path, { throwIfNoEntry: false })?.isDirectory()+ ? join(path, "generated-sources.json")+ : path;+}+export function checkPnpm(packageJson: string) {const { packageManager } = JSON.parse(packageJson);const major = /^pnpm@(\d+)\./.exec(packageManager ?? "")?.[1];@@ -80,13 +96,13 @@ export function filterSources(sources: Source[]): Source[] {return kept;}-function generateSources() {- mkdirSync(dirname(OUTPUT_PATH), { recursive: true });+function generateSources(outputPath: string) {+ mkdirSync(dirname(outputPath), { recursive: true });try {execFileSync("flatpak-node-generator", ["pnpm", LOCKFILE_PATH,"--pnpm-store-version", STORE_VERSION,- "-o", OUTPUT_PATH+ "-o", outputPath], { stdio: "inherit" });} catch (err) {if ((err as NodeJS.ErrnoException).code === "ENOENT") {@@ -103,7 +119,7 @@ function generateSources() {// Only when run as a script — the pure helpers above are imported by the spec.if (process.argv[1] === import.meta.filename) {try {- main();+ main(process.argv.slice(2));} catch (err) {console.error(err instanceof Error ? err.message : err);process.exit(1);diff --git a/scripts/generate-flatpak-sources.spec.ts b/scripts/generate-flatpak-sources.spec.tsindex fab605d6fb..5e7653deba 100644--- a/scripts/generate-flatpak-sources.spec.ts+++ b/scripts/generate-flatpak-sources.spec.ts@@ -1,20 +1,38 @@+import { mkdtempSync, rmSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { describe, expect, it } from "vitest";-import { checkPnpm, filterSources } from "./generate-flatpak-sources.mjs";+import { checkPnpm, filterSources, resolveOutputPath } from "./generate-flatpak-sources.mjs";describe("checkPnpm", () => {it("accepts the pinned pnpm major, with or without a corepack checksum", () => {- expect(() => checkPnpm(`{ "packageManager": "[email protected]" }`)).not.toThrow();- expect(() => checkPnpm(`{ "packageManager": "[email protected]+sha512.abc" }`)).not.toThrow();+ expect(() => checkPnpm(`{ "packageManager": "[email protected]" }`)).not.toThrow();+ expect(() => checkPnpm(`{ "packageManager": "[email protected]+sha512.abc" }`)).not.toThrow();});it("rejects another pnpm major, another package manager, and a missing pin", () => {- expect(() => checkPnpm(`{ "packageManager": "[email protected]" }`)).toThrow(/pnpm 11/);+ expect(() => checkPnpm(`{ "packageManager": "[email protected]" }`)).toThrow(/pnpm 12/);expect(() => checkPnpm(`{ "packageManager": "[email protected]" }`)).toThrow(/[email protected]/);expect(() => checkPnpm(`{}`)).toThrow(/undefined/);});});+describe("resolveOutputPath", () => {+ it("defaults to upload/, appends the filename to a directory, and takes a file path as-is", () => {+ expect(resolveOutputPath(undefined).endsWith(join("upload", "generated-sources.json"))).toBe(true);++ const dir = mkdtempSync(join(tmpdir(), "flatpak-sources-"));+ try {+ expect(resolveOutputPath(dir)).toBe(join(dir, "generated-sources.json"));+ expect(resolveOutputPath(join(dir, "custom.json"))).toBe(join(dir, "custom.json"));+ } finally {+ rmSync(dir, { recursive: true });+ }+ });+});+describe("filterSources", () => {const tarballs = Array.from({ length: 2500 }, (_, i) => ({type: "file",