Skip to content

findSoftware

根据软件名获取软件安装路径,支持多种查找策略。

前置依赖

依赖参数

参数名类型说明
systemInfo.userHomestring用户主目录路径
systemInfo.envRecord<string, string | undefined>环境变量对象
systemInfo.fileExists(path: string) => boolean文件系统检查函数
systemInfo.platformPlatform当前运行平台
systemInfo.joinPath(dir: string, file: string) => string路径拼接函数

函数签名

typescript
function findSoftwareByName(
  softwareName: string,
  systemInfo: SystemInfo
): SoftwareSearchResult

interface SystemInfo {
  userHome: string
  env: Record<string, string | undefined>
  fileExists: (path: string) => boolean
  platform: Platform
  joinPath: (dir: string, file: string) => string
}

interface SoftwareSearchResult {
  found: boolean
  path?: string
}

参数

参数名类型必填说明
softwareNamestring软件名称(支持别名)
systemInfoSystemInfo系统信息对象

返回值

类型说明
SoftwareSearchResult软件查找结果,包含是否找到和路径信息

工作原理

  1. 标准化软件名称:从 SOFTWARE_NAME_MAP 中获取标准化的软件名称(支持别名)
  2. 获取平台配置:从 SOFTWARE_CONFIGS 中获取当前平台的配置信息
  3. 查找策略(按优先级)
    • 专用环境变量:检查软件专用的环境变量(如 ANDROID_HOME
    • PATH环境变量:遍历 PATH 环境变量中的所有目录,查找可执行文件
    • 默认安装路径:检查平台配置中的默认安装路径和用户路径
  4. 验证文件存在:使用 fileExists 验证找到的路径是否真实存在
  5. 返回结果:如果找到,返回 { found: true, path: '...' },否则返回 { found: false }

支持跨平台查找,自动处理路径展开(如 ~ 替换为用户主目录),适用于查找开发工具、SDK 等软件的安装路径。