mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
fix(desktop): match rune hex installation checks (#64131)
This commit is contained in:
@@ -4,33 +4,49 @@ 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")
|
||||
test("uses Rune's per-platform installation paths", () => {
|
||||
expect(hexPaths("darwin", "/home/test", { PATH: "/bin" })).toEqual({
|
||||
support: "/home/test/Library/Application Support/hex",
|
||||
bundles: ["/Applications/Hex.app", "/home/test/Applications/Hex.app"],
|
||||
})
|
||||
expect(hexPaths("win32", "C:\\Users\\test", { APPDATA: "D:\\roaming", PATH: "C:\\tools" })).toEqual({
|
||||
support: "D:\\roaming\\hex",
|
||||
registry: "C:\\Windows\\System32\\reg.exe",
|
||||
})
|
||||
|
||||
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: "relative",
|
||||
XDG_DATA_DIRS: "relative:/opt/share",
|
||||
PATH: ":/bin:.",
|
||||
})
|
||||
|
||||
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")
|
||||
expect(linux).toEqual({
|
||||
support: "/home/test/.local/share/hex",
|
||||
entries: [
|
||||
"/home/test/.local/share/applications/anomaly.hex.desktop",
|
||||
"/home/test/.local/share/applications/hex.desktop",
|
||||
"/opt/share/applications/anomaly.hex.desktop",
|
||||
"/opt/share/applications/hex.desktop",
|
||||
],
|
||||
binaries: ["/home/test/.local/bin/hex", "/bin/hex"],
|
||||
})
|
||||
})
|
||||
|
||||
test("retained Hex settings alone do not count as an installation", async () => {
|
||||
const root = await mkdtemp(path.join(tmpdir(), "hex-detect-"))
|
||||
test("a stale version marker is ignored; a discovery document or the selected exe counts", async () => {
|
||||
const support = 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)
|
||||
const paths = { support, registry: path.join(support, "missing-reg.exe") }
|
||||
await writeFile(path.join(support, "current-version"), "2.3.0\r\n")
|
||||
expect(await detectHex(paths)).toBe(false)
|
||||
await writeFile(path.join(support, "local-api.json"), "")
|
||||
expect(await detectHex(paths)).toBe(true)
|
||||
await rm(path.join(support, "local-api.json"))
|
||||
const version = path.join(support, "versions", "2.3.0")
|
||||
await mkdir(version, { recursive: true })
|
||||
await writeFile(path.join(version, "hex.exe"), "")
|
||||
expect(await detectHex(paths)).toBe(true)
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true })
|
||||
await rm(support, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,56 +1,79 @@
|
||||
import { stat } from "node:fs/promises"
|
||||
import { execFile } from "node:child_process"
|
||||
import { readFile, stat } from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
import { promisify } from "node:util"
|
||||
|
||||
const uninstallKey = String.raw`HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\{6ED48C92-5177-41F4-B8D6-37B7B02CFDF4}_is1`
|
||||
|
||||
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))
|
||||
if (platform === "win32") {
|
||||
const roaming = env.APPDATA || path.win32.join(home, "AppData/Roaming")
|
||||
|
||||
return new Set([...files, ...dirs.flatMap((dir) => names.map((name) => paths.join(dir, name)))])
|
||||
return {
|
||||
support: path.win32.join(roaming, "hex"),
|
||||
registry: path.win32.join(env.SystemRoot || "C:\\Windows", "System32/reg.exe"),
|
||||
}
|
||||
}
|
||||
|
||||
if (platform === "darwin") {
|
||||
return {
|
||||
support: path.posix.join(home, "Library/Application Support/hex"),
|
||||
bundles: ["/Applications/Hex.app", path.posix.join(home, "Applications/Hex.app")],
|
||||
}
|
||||
}
|
||||
|
||||
const data =
|
||||
env.XDG_DATA_HOME && path.posix.isAbsolute(env.XDG_DATA_HOME) ? env.XDG_DATA_HOME : path.posix.join(home, ".local/share")
|
||||
|
||||
const support = path.posix.join(data, "hex")
|
||||
|
||||
if (platform !== "linux") return { support }
|
||||
|
||||
const absoluteDirs = (list: string) => list.split(":").filter((dir) => path.posix.isAbsolute(dir))
|
||||
const dirs = new Set([data, ...absoluteDirs(env.XDG_DATA_DIRS || "/usr/local/share:/usr/share")])
|
||||
const bins = new Set([path.posix.join(home, ".local/bin"), ...absoluteDirs(env.PATH ?? "")])
|
||||
|
||||
return {
|
||||
support,
|
||||
entries: [...dirs].flatMap((dir) =>
|
||||
["anomaly.hex.desktop", "hex.desktop"].map((name) => path.posix.join(dir, "applications", name)),
|
||||
),
|
||||
binaries: [...bins].map((dir) => path.posix.join(dir, "hex")),
|
||||
}
|
||||
}
|
||||
|
||||
export async function detectHex(files: Iterable<string>) {
|
||||
for (const file of files) {
|
||||
const info = await stat(file).catch(() => undefined)
|
||||
const metadata = (file: string) => stat(file).catch(() => undefined)
|
||||
|
||||
if (!info) continue
|
||||
export async function detectHex(paths: ReturnType<typeof hexPaths>) {
|
||||
if (await metadata(path.join(paths.support, "local-api.json"))) return true
|
||||
|
||||
if (file.endsWith("/service.sock")) {
|
||||
if (info.isSocket()) return true
|
||||
continue
|
||||
}
|
||||
if (paths.registry) {
|
||||
const version = await readFile(path.join(paths.support, "current-version"), "utf8").then(
|
||||
(text) => text.trim(),
|
||||
() => undefined,
|
||||
)
|
||||
|
||||
if (info.isFile() && (path.basename(file) !== "hex" || (info.mode & 0o111) !== 0)) return true
|
||||
if (version && (await metadata(path.join(paths.support, "versions", version, "hex.exe")))?.isFile()) return true
|
||||
|
||||
return promisify(execFile)(paths.registry, ["query", uninstallKey], { windowsHide: true, timeout: 1000 }).then(
|
||||
() => true,
|
||||
() => false,
|
||||
)
|
||||
}
|
||||
|
||||
for (const bundle of paths.bundles ?? []) {
|
||||
if ((await metadata(bundle))?.isDirectory()) return true
|
||||
}
|
||||
|
||||
for (const entry of paths.entries ?? []) {
|
||||
if ((await metadata(entry))?.isFile()) return true
|
||||
}
|
||||
|
||||
for (const binary of paths.binaries ?? []) {
|
||||
const info = await metadata(binary)
|
||||
|
||||
if (info?.isFile() && (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"),
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user