trilium/scripts/electron-start.mts
Elian Doran ae798be4ea
chore(scripts): typecheck the maintenance scripts
The scripts project is referenced from the root solution, so `pnpm typecheck`
was building it on every run — over an empty program. `include` resolves against
the directory holding the tsconfig, but its globs were written as if they were
relative to the repository root, so `scripts/*.ts` looked for
scripts/scripts/*.ts and matched nothing. Nothing under scripts/ has been
typechecked for as long as the file has looked like this.

Widened to the whole tree, .mts included, which also covers translation/ and
repo-migration/. That found one bug: on anything other than NixOS,
`isNixOS() && getNixLdLibraryPath()` is `false` rather than `undefined`, and
Node stringifies it instead of dropping it — so `pnpm desktop:start` launched
Electron with LD_LIBRARY_PATH=false.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-19 20:53:11 +03:00

39 lines
1.6 KiB
TypeScript

import { execSync } from "child_process";
import { buildSync } from "esbuild";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { getElectronPath, getNixLdLibraryPath, isNixOS } from "./utils.mjs";
// Compile the preload script to CJS so Electron's sandboxed renderer can load it.
// Always build from apps/desktop/ regardless of CWD — window.ts resolves the
// compiled preload there for every Electron-based app (desktop, edit-docs, ...).
const DESKTOP_DIR = resolve(dirname(fileURLToPath(import.meta.url)), "..", "apps", "desktop");
buildSync({
entryPoints: [join(DESKTOP_DIR, "src", "preload.ts")],
outfile: join(DESKTOP_DIR, "src", "preload.compiled.cjs"),
platform: "node",
format: "cjs",
bundle: true,
external: ["electron"]
});
const LD_LIBRARY_PATH = isNixOS() ? getNixLdLibraryPath() : undefined;
const args = process.argv.slice(2);
execSync(`${getElectronPath()} ${args.join(" ")} --no-sandbox`, {
stdio: "inherit",
env: {
...process.env,
NODE_OPTIONS: "--import tsx",
NODE_ENV: "development",
TRILIUM_ENV: "dev",
TRILIUM_RESOURCE_DIR: "../server/src",
// No BETTERSQLITE3_NATIVE_PATH: since better-sqlite3 v13 the addon is
// N-API based and ships prebuilt binaries for every supported
// platform/arch, which load unchanged under Electron. Its own resolver
// picks prebuilds/<platform>-<arch>.node, so there is no Electron-
// specific build in build/Release/ to point at any more.
LD_LIBRARY_PATH
}
});