diff --git a/packages/app/src/tips/model.ts b/packages/app/src/tips/model.ts index e13f055b7ca..caf2c6e8c5b 100644 --- a/packages/app/src/tips/model.ts +++ b/packages/app/src/tips/model.ts @@ -33,7 +33,15 @@ export function createTipToasts() { const location = () => server()?.data.location const [awake, setAwake] = createSignal(false) - const startup = setTimeout(() => setAwake(true), STARTUP_DELAY) + const [hexInstalled, setHexInstalled] = createSignal() + + const startup = setTimeout(() => { + setAwake(true) + + if (!platform.checkAppExists) return setHexInstalled(false) + void platform.checkAppExists("hex").then(setHexInstalled, () => setHexInstalled(false)) + }, STARTUP_DELAY) + onCleanup(() => clearTimeout(startup)) createEffect(() => { const ctx = server() @@ -66,6 +74,7 @@ export function createTipToasts() { id: "hex", title: "tips.hex.title", description: "tips.hex.description", + when: () => hexInstalled() === false, action: { label: "tips.hex.install", // anoma.ly/hex 404s with a query string; the trailing slash is required. diff --git a/packages/desktop/src/main/files/apps.ts b/packages/desktop/src/main/files/apps.ts index ac897fb91c7..caf23ae3863 100644 --- a/packages/desktop/src/main/files/apps.ts +++ b/packages/desktop/src/main/files/apps.ts @@ -1,10 +1,18 @@ import { execFile } from "node:child_process" +import { homedir } from "node:os" import util from "node:util" import { Effect, FileSystem, Path } from "effect" +import { detectHex, hexPaths } from "./hex" const execFilePromise = util.promisify(execFile) +let hex: Promise | undefined + export const checkAppExists = Effect.fn("DesktopFiles.checkAppExists")(function* (appName: string) { + if (appName === "hex") { + return yield* Effect.promise(() => (hex ??= detectHex(hexPaths(process.platform, homedir(), process.env)))) + } + if (process.platform === "win32") return true if (process.platform === "linux") return true diff --git a/packages/desktop/src/main/files/hex.test.ts b/packages/desktop/src/main/files/hex.test.ts new file mode 100644 index 00000000000..f6b58842669 --- /dev/null +++ b/packages/desktop/src/main/files/hex.test.ts @@ -0,0 +1,36 @@ +import { expect, test } from "bun:test" +import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import path from "node:path" +import { detectHex, hexPaths } from "./hex" + +test("uses the Hex installers' per-user layouts and desktop PATH", () => { + const mac = hexPaths("darwin", "/Users/test", { PATH: "/bin" }) + expect([...mac]).toContain("/Users/test/Applications/Hex.app/Contents/MacOS/hex") + expect([...mac]).toContain("/bin/hex") + + const windows = hexPaths("win32", "C:\\Users\\test", { APPDATA: "D:\\roaming", PATH: "D:\\bin;C:\\tools" }) + expect([...windows]).toContain("D:\\roaming\\hex\\current-version") + expect([...windows]).toContain("C:\\tools\\hex.com") + + const linux = hexPaths("linux", "/home/test", { XDG_DATA_HOME: "/data" }) + expect([...linux]).toContain("/data/applications/anomaly.hex.desktop") + expect([...linux]).toContain("/data/hex/service.sock") + expect([...linux]).toContain("/usr/bin/hex") +}) + +test("retained Hex settings alone do not count as an installation", async () => { + const root = await mkdtemp(path.join(tmpdir(), "hex-detect-")) + + try { + const support = path.join(root, "hex") + await mkdir(support) + await writeFile(path.join(support, "settings.json"), "{}") + const marker = path.join(support, "current-version") + expect(await detectHex([marker])).toBe(false) + await writeFile(marker, "3.1.0") + expect(await detectHex([marker])).toBe(true) + } finally { + await rm(root, { recursive: true, force: true }) + } +}) diff --git a/packages/desktop/src/main/files/hex.ts b/packages/desktop/src/main/files/hex.ts new file mode 100644 index 00000000000..ccffb04d0e2 --- /dev/null +++ b/packages/desktop/src/main/files/hex.ts @@ -0,0 +1,56 @@ +import { stat } from "node:fs/promises" +import path from "node:path" + +export function hexPaths(platform: NodeJS.Platform, home: string, env: NodeJS.ProcessEnv) { + const paths = platform === "win32" ? path.win32 : path.posix + const files = installedPaths(platform, home, env) + const names = platform === "win32" ? ["hex.com", "hex.exe"] : ["hex"] + const dirs = (env.PATH ?? "").split(paths.delimiter).filter((dir) => paths.isAbsolute(dir)) + + return new Set([...files, ...dirs.flatMap((dir) => names.map((name) => paths.join(dir, name)))]) +} + +export async function detectHex(files: Iterable) { + for (const file of files) { + const info = await stat(file).catch(() => undefined) + + if (!info) continue + + if (file.endsWith("/service.sock")) { + if (info.isSocket()) return true + continue + } + + if (info.isFile() && (path.basename(file) !== "hex" || (info.mode & 0o111) !== 0)) return true + } + + return false +} + +function installedPaths(platform: NodeJS.Platform, home: string, env: NodeJS.ProcessEnv) { + if (platform === "darwin") { + const binary = "Applications/Hex.app/Contents/MacOS/hex" + + return [`/${binary}`, path.posix.join(home, binary)] + } + + if (platform === "win32") { + const roaming = env.APPDATA || path.win32.join(home, "AppData", "Roaming") + + return ["hex", "voice-control"].map((dir) => path.win32.join(roaming, dir, "current-version")) + } + + if (platform !== "linux") return [] + + const data = env.XDG_DATA_HOME || path.posix.join(home, ".local/share") + const support = env.HEX_APPLICATION_SUPPORT_DIR || path.posix.join(data, "hex") + + return [ + "/usr/bin/hex", + "/usr/share/applications/anomaly.hex.desktop", + path.posix.join(data, "applications/anomaly.hex.desktop"), + path.posix.join(env.HEX_INSTALL_DIR || path.posix.join(home, ".local/bin"), "hex"), + path.posix.join(support, "current/hex"), + path.posix.join(support, "service.sock"), + ] +}