fix(ios): improve bridge discovery and pairing UX
This commit is contained in:
43
src/infra/machine-name.ts
Normal file
43
src/infra/machine-name.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import os from "node:os";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
let cachedPromise: Promise<string> | null = null;
|
||||
|
||||
async function tryScutil(key: "ComputerName" | "LocalHostName") {
|
||||
try {
|
||||
const { stdout } = await execFileAsync("/usr/sbin/scutil", ["--get", key], {
|
||||
timeout: 1000,
|
||||
windowsHide: true,
|
||||
});
|
||||
const value = String(stdout ?? "").trim();
|
||||
return value.length > 0 ? value : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function fallbackHostName() {
|
||||
return (
|
||||
os
|
||||
.hostname()
|
||||
.replace(/\.local$/i, "")
|
||||
.trim() || "clawdis"
|
||||
);
|
||||
}
|
||||
|
||||
export async function getMachineDisplayName(): Promise<string> {
|
||||
if (cachedPromise) return cachedPromise;
|
||||
cachedPromise = (async () => {
|
||||
if (process.platform === "darwin") {
|
||||
const computerName = await tryScutil("ComputerName");
|
||||
if (computerName) return computerName;
|
||||
const localHostName = await tryScutil("LocalHostName");
|
||||
if (localHostName) return localHostName;
|
||||
}
|
||||
return fallbackHostName();
|
||||
})();
|
||||
return cachedPromise;
|
||||
}
|
||||
Reference in New Issue
Block a user