openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
90 lines (89 loc) • 2.34 kB
JavaScript
import path from "node:path";
import os from "node:os";
import { promisify } from "node:util";
import { execFile } from "node:child_process";
import { walkDirectory } from "@openclaw/fs-safe/walk";
import pLimit from "p-limit";
//#region src/infra/installed-apps.ts
const execFileAsync = promisify(execFile);
const PLIST_READ_CONCURRENCY = 8;
const PLIST_READ_TIMEOUT_MS = 2e3;
const SYSTEM_APP_NAMES = /* @__PURE__ */ new Set([
"Calendar",
"Contacts",
"FaceTime",
"Home",
"Mail",
"Maps",
"Messages",
"Music",
"Notes",
"Photos",
"Podcasts",
"Reminders",
"Shortcuts"
]);
function isBackupishBundle(e) {
return /(?:^|[\s._-])(?:backup|previous|rollback)(?:[\s._-]|$)/i.test(e) || /(?:^|[\s._-])pre-[\p{L}\p{N}._-]+$/iu.test(e);
}
async function listAppPaths(e, t) {
const { entries: n } = await walkDirectory(e, {
maxDepth: 1,
symlinks: "follow",
include: (e) => {
if (e.kind !== "directory" || !e.name.toLowerCase().endsWith(".app")) return false;
const n = e.name.slice(0, -4);
return !isBackupishBundle(n) && (!t || SYSTEM_APP_NAMES.has(n));
}
});
return n.map((e) => ({
path: e.path,
system: t
}));
}
async function readBundleIdWithPlutil(e) {
try {
const t = path.join(e, "Contents", "Info.plist");
const { stdout: n } = await execFileAsync("/usr/bin/plutil", [
"-extract",
"CFBundleIdentifier",
"raw",
"-expect",
"string",
t
], {
encoding: "utf8",
maxBuffer: 1048576,
timeout: PLIST_READ_TIMEOUT_MS
});
return n.trim() || void 0;
} catch {
return;
}
}
async function scanInstalledApps() {
if (process.platform !== "darwin") return {
status: "unsupported",
apps: []
};
const e = (await Promise.all([
listAppPaths("/Applications", false),
listAppPaths(path.join(os.homedir(), "Applications"), false),
listAppPaths("/System/Applications", true)
])).flat();
const t = pLimit(PLIST_READ_CONCURRENCY);
return {
status: "ok",
apps: (await Promise.all(e.map((e) => t(async () => {
const t = await readBundleIdWithPlutil(e.path);
return {
label: path.basename(e.path, ".app"),
...t ? { bundleId: t } : {},
path: e.path,
system: e.system
};
})))).toSorted((e, t) => e.label.localeCompare(t.label, "en", { sensitivity: "base" }) || e.path.localeCompare(t.path))
};
}
//#endregion
export { scanInstalledApps as t };