UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.09 kB
import{n as pruneMapToMaxSize}from"./regexp-DvzVyjZw.mjs";import{n as canAccessSync}from"./path-existence-4c3xWldM.mjs";import{i as getWindowsProgramFilesRoots,n as getWindowsInstallRoots}from"./windows-install-roots-BXU3EsrD.mjs";import fsSync from"node:fs";import path from"node:path"; //!src/infra/resolve-system-bin.ts const UNIX_BASE_TRUSTED_DIRS=[`/usr/bin`,`/bin`,`/usr/sbin`,`/sbin`];const DARWIN_STANDARD_DIRS=[`/opt/homebrew/bin`,`/usr/local/bin`];const LINUX_STANDARD_DIRS=[`/usr/local/bin`];const WIN_PATHEXT=[`.exe`,`.cmd`,`.bat`,`.com`];const WINDOWS_PROGRAM_FILES_TOOL_DIR_PREFIXES=[`ImageMagick-`,`GraphicsMagick-`];const WINDOWS_PROGRAM_FILES_TOOL_DIRS=[`ImageMagick`,`GraphicsMagick`];const RESOLVED_BIN_CACHE_LIMIT=512;const resolvedCacheStrict=/* @__PURE__ */ new Map;const resolvedCacheStandard=/* @__PURE__ */ new Map;function cacheResolvedSystemBin(e,t,n){e.set(t,n);pruneMapToMaxSize(e,RESOLVED_BIN_CACHE_LIMIT)}function collectWindowsProgramFilesToolDirs(e){const t=WINDOWS_PROGRAM_FILES_TOOL_DIRS.map(t=>path.win32.join(e,t));try{for(const n of fsSync.readdirSync(e,{withFileTypes:true}))if(n.isDirectory()&&WINDOWS_PROGRAM_FILES_TOOL_DIR_PREFIXES.some(e=>n.name.startsWith(e)))t.push(path.win32.join(e,n.name))}catch{}return t}function buildWindowsTrustedDirs(){const e=[];const{systemRoot:t}=getWindowsInstallRoots();e.push(path.win32.join(t,`System32`));e.push(path.win32.join(t,`SysWOW64`));e.push(path.win32.join(t,`System32`,`WindowsPowerShell`,`v1.0`));for(const t of getWindowsProgramFilesRoots()){e.push(path.win32.join(t,`OpenSSL-Win64`,`bin`));e.push(path.win32.join(t,`OpenSSL`,`bin`));e.push(path.win32.join(t,`ffmpeg`,`bin`))}return e}function buildWindowsStandardDirs(){const{systemRoot:e}=getWindowsInstallRoots();const t=path.win32.parse(e).root;const n=[path.win32.join(t,`ProgramData`,`chocolatey`,`bin`)];for(const e of getWindowsProgramFilesRoots())n.push(...collectWindowsProgramFilesToolDirs(e));return n}function buildUnixTrustedDirs(e){const t=[...UNIX_BASE_TRUSTED_DIRS];const n=process.platform;if(n===`linux`){t.push(`/run/current-system/sw/bin`);t.push(`/snap/bin`)}if(e===`standard`){if(n===`darwin`)t.push(...DARWIN_STANDARD_DIRS);else if(n===`linux`)t.push(...LINUX_STANDARD_DIRS)}return t}const trustedDirs={};function getTrustedDirs(e){return trustedDirs[e]??=process.platform!==`win32`?buildUnixTrustedDirs(e):e===`strict`?buildWindowsTrustedDirs():[...getTrustedDirs(`strict`),...buildWindowsStandardDirs()]}function resolveSystemBin(e,t){const n=t?.trust??`strict`;const r=(t?.extraDirs?.length??0)>0;const i=n===`standard`?resolvedCacheStandard:resolvedCacheStrict;if(!r){const t=i.get(e);if(t!==void 0){i.delete(e);i.set(e,t);return t}}const a=[...getTrustedDirs(n),...t?.extraDirs??[]];const o=process.platform===`win32`;const s=o&&path.win32.extname(e).length>0;for(const t of a){const n=o&&!s?WIN_PATHEXT.map(n=>path.win32.join(t,e+n)):[path.join(t,e)];for(const t of n)if(canAccessSync(t,o?fsSync.constants.R_OK:fsSync.constants.X_OK)){if(!r)cacheResolvedSystemBin(i,e,t);return t}}return null}export{resolveSystemBin as t};