fix(hopper): launch analysis without stealing focus

This commit is contained in:
morluto committed 2026-07-12 08:19:29 +08:00
1 parent 3d70812c37
commit ff321dc574
4 files changed
+116 -5

No files matched your search

+16 -1
View File
@@ -39,18 +39,21 @@ export class BinarySession implements HopperToolPort {
return ok(parsed.value);
await this.#drainCalls();
const previous = this.#active;
this.#active = undefined;
await previous?.client.close();
const client = this.createClient(parsed.value);
const started = await client.callTool("health", {}, options);
if (!started.ok) {
await client.close();
await this.#restore(previous);
return started;
}
if (isAborted(options.signal)) {
await client.close();
await this.#restore(previous);
return err(new HopperCancelledError());
}
this.#active = { target: parsed.value, client };
await previous?.client.close();
return ok(parsed.value);
});
}
@@ -111,6 +114,18 @@ export class BinarySession implements HopperToolPort {
await Promise.allSettled([...this.#calls]);
}
async #restore(
previous:
| { readonly target: BinaryTarget; readonly client: BinaryClient }
| undefined,
): Promise<void> {
if (previous === undefined) return;
const client = this.createClient(previous.target);
const started = await client.callTool("health", {});
if (started.ok) this.#active = { target: previous.target, client };
else await client.close();
}
async #waitForTransition(
signal?: AbortSignal,
): Promise<Result<undefined, HopperCancelledError>> {
+42
View File
@@ -1,9 +1,15 @@
import { spawn, type ChildProcess } from "node:child_process";
import { execFile } from "node:child_process";
import { chmod, writeFile } from "node:fs/promises";
import { basename, dirname, extname, join } from "node:path";
import { promisify } from "node:util";
import { HopperStartError } from "../domain/errors.js";
import { err, ok, type Result } from "../domain/result.js";
const execFileAsync = promisify(execFile);
const HOPPER_BACKGROUND_STARTUP_MS = 5_000;
/** Coordinates for one private bridge session. */
export interface BridgeSession {
readonly directory: string;
@@ -64,6 +70,7 @@ export class HopperApplicationLauncher implements BridgeLauncher {
this.options.targetPath,
];
try {
await prepareHopperApplication(this.options.launcherPath);
const child = spawn(this.options.launcherPath, args, {
stdio: ["ignore", "ignore", "pipe"],
});
@@ -88,3 +95,38 @@ export class HopperApplicationLauncher implements BridgeLauncher {
}
}
}
const prepareHopperApplication = async (
launcherPath: string,
): Promise<void> => {
const appBundle = hopperApplicationBundle(launcherPath);
if (appBundle === undefined) return;
const executablePath = join(appBundle, "Contents/MacOS/Hopper Disassembler");
if (await processIsRunning(executablePath)) return;
await execFileAsync("/usr/bin/open", [
"--hide",
"--background",
"-a",
appBundle,
]);
await new Promise((resolve) =>
setTimeout(resolve, HOPPER_BACKGROUND_STARTUP_MS),
);
};
const hopperApplicationBundle = (launcherPath: string): string | undefined => {
if (basename(launcherPath) !== "hopper") return undefined;
const candidate = dirname(dirname(dirname(launcherPath)));
return extname(candidate) === ".app" ? candidate : undefined;
};
const processIsRunning = async (executablePath: string): Promise<boolean> => {
try {
const processes = await execFileAsync("/bin/ps", ["-ax", "-o", "command="]);
return processes.stdout
.split("\n")
.some((command) => command.trim() === executablePath);
} catch {
return false;
}
};