From f5fbbc3fdf90cc5fd836a538ebd0697b817d8063 Mon Sep 17 00:00:00 2001 From: Namnarak <87424988+Namnarak@users.noreply.github.com> Date: Wed, 7 Oct 2026 20:14:39 +0700 Subject: [PATCH] fix(config): reuse user-local Hopper launcher --- src/config/parseConfig.ts | 31 ++++++++++++++++++--- tests/composition/hopperConfig.test.ts | 38 ++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 tests/composition/hopperConfig.test.ts diff --git a/src/config/parseConfig.ts b/src/config/parseConfig.ts index 61a9152ba..17fb465fb 100644 --- a/src/config/parseConfig.ts +++ b/src/config/parseConfig.ts @@ -1,13 +1,36 @@ +import { accessSync, constants } from "node:fs"; +import { homedir } from "node:os"; +import { join } from "node:path"; + import { ConfigurationError } from "../domain/configurationErrors.js"; import { ok, type Result } from "../domain/result.js"; import { parseEnvironment } from "./environment.js"; import { parseStringArray, parseLoaderArgs } from "./parsers.js"; import type { AppConfig } from "./types.js"; -const defaultHopperLauncherPath = (): string => - process.platform === "linux" - ? "/opt/hopper/bin/Hopper" - : "/Applications/Hopper Disassembler.app/Contents/MacOS/hopper"; +const DEFAULT_HOPPER_LAUNCHER_PATH = + "/Applications/Hopper Disassembler.app/Contents/MacOS/hopper"; +const SYSTEM_LINUX_HOPPER = "/opt/hopper/bin/Hopper"; + +const executableAvailable = (path: string): boolean => { + try { + accessSync(path, constants.X_OK); + return true; + } catch { + return false; + } +}; + +export const defaultHopperLauncherPath = ( + platform: NodeJS.Platform = process.platform, + homeDirectory: string = homedir(), + executable: (path: string) => boolean = executableAvailable, +): string => { + if (platform !== "linux") return DEFAULT_HOPPER_LAUNCHER_PATH; + if (executable(SYSTEM_LINUX_HOPPER)) return SYSTEM_LINUX_HOPPER; + const userLocal = join(homeDirectory, ".local/share/rea/hopper/bin/Hopper"); + return executable(userLocal) ? userLocal : SYSTEM_LINUX_HOPPER; +}; /** Parse provider configuration and runtime prerequisites at the composition root. */ export const parseConfig = ( diff --git a/tests/composition/hopperConfig.test.ts b/tests/composition/hopperConfig.test.ts new file mode 100644 index 000000000..82c21d578 --- /dev/null +++ b/tests/composition/hopperConfig.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from "vitest"; + +import { + defaultHopperLauncherPath, + parseConfig, +} from "../../src/config/parseConfig.js"; + +describe("Hopper launcher defaults", () => { + it("prefers an executable system Hopper on Linux", () => { + const executable = (path: string) => path === "/opt/hopper/bin/Hopper"; + expect(defaultHopperLauncherPath("linux", "/home/test", executable)).toBe( + "/opt/hopper/bin/Hopper", + ); + }); + + it("falls back to the legacy user-local Hopper on Linux", () => { + const executable = (path: string) => + path === "/home/test/.local/share/rea/hopper/bin/Hopper"; + expect(defaultHopperLauncherPath("linux", "/home/test", executable)).toBe( + "/home/test/.local/share/rea/hopper/bin/Hopper", + ); + }); + + it("keeps the system path as the diagnostic fallback when neither exists", () => { + expect(defaultHopperLauncherPath("linux", "/home/test", () => false)).toBe( + "/opt/hopper/bin/Hopper", + ); + }); + + it("keeps explicit HOPPER_LAUNCHER_PATH authoritative", () => { + const parsed = parseConfig({ + HOPPER_LAUNCHER_PATH: "/custom/Hopper", + }); + expect(parsed.ok).toBe(true); + if (parsed.ok) + expect(parsed.value.hopperLauncherPath).toBe("/custom/Hopper"); + }); +});