config.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. console.warn('Failed to load adb-path-config.js:', error.message);
  34. }
  35. return null;
  36. }
  37. // 查找 ADB 可执行文件路径
  38. function getAdbPath() {
  39. // 首先尝试从配置文件读取
  40. const config = loadConfig();
  41. if (config && config['adb-path']) {
  42. const configAdbPath = path.join(config['adb-path'], 'adb.exe');
  43. if (existsSync(configAdbPath)) {
  44. console.log('Using ADB path from adb-path-config.js:', configAdbPath);
  45. return configAdbPath;
  46. }
  47. // 如果配置的路径不存在,尝试直接使用配置的路径(可能已经是完整路径)
  48. if (existsSync(config['adb-path'])) {
  49. console.log('Using ADB path from adb-path-config.js:', config['adb-path']);
  50. return config['adb-path'];
  51. }
  52. }
  53. // 如果配置文件没有或路径不存在,使用常见 ADB 安装位置(按优先级排序)
  54. const possiblePaths = [
  55. path.join(process.env.LOCALAPPDATA || '', 'Android', 'Sdk', 'platform-tools', 'adb.exe'),
  56. path.join(process.env.USERPROFILE || '', 'AppData', 'Local', 'Android', 'Sdk', 'platform-tools', 'adb.exe'),
  57. path.join(process.env.ProgramFiles || '', 'Android', 'android-sdk', 'platform-tools', 'adb.exe'),
  58. path.join(process.env['ProgramFiles(x86)'] || '', 'Android', 'android-sdk', 'platform-tools', 'adb.exe'),
  59. ];
  60. // 检查常见位置
  61. for (const adbPath of possiblePaths) {
  62. if (existsSync(adbPath)) {
  63. return adbPath;
  64. }
  65. }
  66. // 如果都找不到,尝试使用 PATH 中的 adb
  67. return 'adb';
  68. }
  69. // 缓存 ADB 路径
  70. let adbPathCache = null;
  71. export function getCachedAdbPath() {
  72. if (!adbPathCache) {
  73. adbPathCache = getAdbPath();
  74. }
  75. return adbPathCache;
  76. }