config.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { existsSync, readFileSync } from 'fs';
  2. import path from 'path';
  3. import { fileURLToPath } from 'url';
  4. const __filename = fileURLToPath(import.meta.url);
  5. const __dirname = path.dirname(__filename);
  6. // 读取配置文件
  7. export function loadConfig() {
  8. try {
  9. const configPath = path.join(__dirname, '..', 'adb-path-config.js');
  10. if (existsSync(configPath)) {
  11. const configContent = readFileSync(configPath, 'utf-8');
  12. // 解析 CommonJS 格式的配置文件
  13. // 移除 module.exports = 和分号,转换为纯 JSON
  14. let jsonContent = configContent
  15. .replace(/module\.exports\s*=\s*/g, '') // 移除 module.exports =
  16. .replace(/;[\s]*$/, '') // 移除末尾分号
  17. .replace(/\/\/.*$/gm, '') // 移除单行注释
  18. .replace(/,(\s*[}\]])/g, '$1') // 移除尾随逗号
  19. .trim();
  20. // 将单引号字符串转换为双引号字符串(JSON 要求)
  21. // 匹配单引号字符串,包括转义的单引号
  22. jsonContent = jsonContent.replace(/'((?:[^'\\]|\\.)*)'/g, (match, content) => {
  23. // 转义双引号和反斜杠,然后用双引号包裹
  24. // 注意:反斜杠需要先转义,因为后续的替换可能会影响它们
  25. const escaped = content
  26. .replace(/\\/g, '\\\\') // 转义反斜杠(先处理)
  27. .replace(/"/g, '\\"'); // 转义双引号
  28. return `"${escaped}"`;
  29. });
  30. return JSON.parse(jsonContent);
  31. }
  32. } catch (error) {
  33. // 加载配置文件失败,忽略错误
  34. }
  35. return null;
  36. }
  37. // 查找 ADB 可执行文件路径
  38. function getAdbPath() {
  39. // 首先尝试使用项目根目录下的 adb-tools/adb.exe(最高优先级)
  40. const projectRoot = path.join(__dirname, '..');
  41. const localAdbPath = path.join(projectRoot, 'adb-tools', 'adb.exe');
  42. if (existsSync(localAdbPath)) {
  43. return localAdbPath;
  44. }
  45. // 如果项目目录下没有,尝试从配置文件读取(向后兼容)
  46. const config = loadConfig();
  47. if (config && config['adb-path']) {
  48. const configAdbPath = path.join(config['adb-path'], 'adb.exe');
  49. if (existsSync(configAdbPath)) {
  50. return configAdbPath;
  51. }
  52. // 如果配置的路径不存在,尝试直接使用配置的路径(可能已经是完整路径)
  53. if (existsSync(config['adb-path'])) {
  54. return config['adb-path'];
  55. }
  56. }
  57. // 如果配置文件没有或路径不存在,使用常见 ADB 安装位置(按优先级排序)
  58. const possiblePaths = [
  59. path.join(process.env.LOCALAPPDATA || '', 'Android', 'Sdk', 'platform-tools', 'adb.exe'),
  60. path.join(process.env.USERPROFILE || '', 'AppData', 'Local', 'Android', 'Sdk', 'platform-tools', 'adb.exe'),
  61. path.join(process.env.ProgramFiles || '', 'Android', 'android-sdk', 'platform-tools', 'adb.exe'),
  62. path.join(process.env['ProgramFiles(x86)'] || '', 'Android', 'android-sdk', 'platform-tools', 'adb.exe'),
  63. ];
  64. // 检查常见位置
  65. for (const adbPath of possiblePaths) {
  66. if (existsSync(adbPath)) {
  67. return adbPath;
  68. }
  69. }
  70. // 如果都找不到,尝试使用 PATH 中的 adb
  71. return 'adb';
  72. }
  73. // 缓存 ADB 路径
  74. let adbPathCache = null;
  75. export function getCachedAdbPath() {
  76. if (!adbPathCache) {
  77. adbPathCache = getAdbPath();
  78. }
  79. return adbPathCache;
  80. }