@@ -11,22 +11,55 @@ const PLATFORM_TOOLS_URLS = {
1111 linux : 'https://dl.google.com/android/repository/platform-tools-latest-linux.zip'
1212} ;
1313
14- // Get platform tools directory
15- function getPlatformToolsDir ( ) {
14+ function getAdbExecutableName ( ) {
15+ return process . platform === 'win32' ? 'adb.exe' : 'adb' ;
16+ }
17+
18+ function getDownloadedPlatformToolsDir ( ) {
1619 return path . join ( os . homedir ( ) , '.adb-proxy-browser' , 'platform-tools' ) ;
1720}
1821
22+ function getDevBundledPlatformToolsDir ( ) {
23+ return path . resolve ( __dirname , '../../../bundled-tools/platform-tools' ) ;
24+ }
25+
26+ function getPackagedPlatformToolsDir ( ) {
27+ if ( ! process . resourcesPath ) {
28+ return null ;
29+ }
30+ return path . join ( process . resourcesPath , 'platform-tools' ) ;
31+ }
32+
33+ function getPlatformToolsDirCandidates ( ) {
34+ const candidates = [ ] ;
35+ const packagedDir = getPackagedPlatformToolsDir ( ) ;
36+ const devBundledDir = getDevBundledPlatformToolsDir ( ) ;
37+ const downloadedDir = getDownloadedPlatformToolsDir ( ) ;
38+
39+ if ( packagedDir ) candidates . push ( packagedDir ) ;
40+ candidates . push ( devBundledDir ) ;
41+ candidates . push ( downloadedDir ) ;
42+
43+ return candidates ;
44+ }
45+
46+ function getExistingPlatformToolsDir ( ) {
47+ return getPlatformToolsDirCandidates ( ) . find ( ( dir ) => fs . existsSync ( path . join ( dir , getAdbExecutableName ( ) ) ) ) || null ;
48+ }
49+
50+ // Backward-compatible name used by the rest of the app.
51+ function getPlatformToolsDir ( ) {
52+ return getExistingPlatformToolsDir ( ) || getDownloadedPlatformToolsDir ( ) ;
53+ }
54+
1955// Get adb path
2056function getBundledAdbPath ( ) {
21- const dir = getPlatformToolsDir ( ) ;
22- const adbName = process . platform === 'win32' ? 'adb.exe' : 'adb' ;
23- return path . join ( dir , adbName ) ;
57+ return path . join ( getPlatformToolsDir ( ) , getAdbExecutableName ( ) ) ;
2458}
2559
2660// Check if bundled adb exists
2761function hasBundledAdb ( ) {
28- const adbPath = getBundledAdbPath ( ) ;
29- return fs . existsSync ( adbPath ) ;
62+ return ! ! getExistingPlatformToolsDir ( ) ;
3063}
3164
3265// Download file
@@ -35,10 +68,9 @@ function downloadFile(url, dest) {
3568 console . log ( `[ADB] Downloading from ${ url } ` ) ;
3669 const file = fs . createWriteStream ( dest ) ;
3770
38- const request = ( url ) => {
39- https . get ( url , ( response ) => {
71+ const request = ( nextUrl ) => {
72+ https . get ( nextUrl , ( response ) => {
4073 if ( response . statusCode === 302 || response . statusCode === 301 ) {
41- // Follow redirect
4274 request ( response . headers . location ) ;
4375 return ;
4476 }
@@ -53,7 +85,7 @@ function downloadFile(url, dest) {
5385
5486 response . on ( 'data' , ( chunk ) => {
5587 downloaded += chunk . length ;
56- const percent = Math . round ( ( downloaded / totalSize ) * 100 ) ;
88+ const percent = totalSize ? Math . round ( ( downloaded / totalSize ) * 100 ) : 0 ;
5789 process . stdout . write ( `\r[ADB] Downloading: ${ percent } %` ) ;
5890 } ) ;
5991
@@ -79,20 +111,18 @@ async function extractZip(zipPath, destDir) {
79111 return new Promise ( ( resolve , reject ) => {
80112 console . log ( `[ADB] Extracting ${ zipPath } to ${ destDir } ` ) ;
81113
82- // Create dest directory
83114 if ( ! fs . existsSync ( destDir ) ) {
84115 fs . mkdirSync ( destDir , { recursive : true } ) ;
85116 }
86117
87118 let cmd ;
88119 if ( process . platform === 'win32' ) {
89- // Use PowerShell to extract
90120 cmd = `powershell -Command "Expand-Archive -Path '${ zipPath } ' -DestinationPath '${ path . dirname ( destDir ) } ' -Force"` ;
91121 } else {
92122 cmd = `unzip -o "${ zipPath } " -d "${ path . dirname ( destDir ) } "` ;
93123 }
94124
95- exec ( cmd , ( error , stdout , stderr ) => {
125+ exec ( cmd , ( error ) => {
96126 if ( error ) {
97127 console . error ( `[ADB] Extract error: ${ error . message } ` ) ;
98128 reject ( error ) ;
@@ -104,39 +134,50 @@ async function extractZip(zipPath, destDir) {
104134 } ) ;
105135}
106136
137+ function ensureAdbExecutable ( targetDir ) {
138+ if ( process . platform === 'win32' ) {
139+ return ;
140+ }
141+
142+ const adbPath = path . join ( targetDir , getAdbExecutableName ( ) ) ;
143+ if ( fs . existsSync ( adbPath ) ) {
144+ fs . chmodSync ( adbPath , '755' ) ;
145+ }
146+ }
147+
148+ function ensureAdbExists ( targetDir ) {
149+ const adbPath = path . join ( targetDir , getAdbExecutableName ( ) ) ;
150+ if ( ! fs . existsSync ( adbPath ) ) {
151+ throw new Error ( `ADB binary not found after extraction: ${ adbPath } ` ) ;
152+ }
153+ return adbPath ;
154+ }
155+
107156// Download and setup platform tools
108- async function downloadPlatformTools ( onProgress ) {
157+ async function downloadPlatformTools ( onProgress , options = { } ) {
109158 const url = PLATFORM_TOOLS_URLS [ process . platform ] ;
110159 if ( ! url ) {
111160 throw new Error ( `Unsupported platform: ${ process . platform } ` ) ;
112161 }
113162
114- const platformToolsDir = getPlatformToolsDir ( ) ;
115- const zipPath = path . join ( os . tmpdir ( ) , ' platform-tools. zip' ) ;
163+ const platformToolsDir = options . targetDir || getDownloadedPlatformToolsDir ( ) ;
164+ const zipPath = options . zipPath || path . join ( os . tmpdir ( ) , ` platform-tools- ${ process . platform } . zip` ) ;
116165
117166 try {
118167 if ( onProgress ) onProgress ( 'downloading' , 0 ) ;
119168
120- // Download
121169 await downloadFile ( url , zipPath ) ;
122170 if ( onProgress ) onProgress ( 'extracting' , 50 ) ;
123171
124- // Extract
125172 await extractZip ( zipPath , platformToolsDir ) ;
173+ ensureAdbExecutable ( platformToolsDir ) ;
174+ const adbPath = ensureAdbExists ( platformToolsDir ) ;
126175 if ( onProgress ) onProgress ( 'complete' , 100 ) ;
127176
128- // Make executable on Unix
129- if ( process . platform !== 'win32' ) {
130- const adbPath = getBundledAdbPath ( ) ;
131- fs . chmodSync ( adbPath , '755' ) ;
132- }
133-
134- // Cleanup
135177 fs . unlinkSync ( zipPath ) ;
136178
137- return getBundledAdbPath ( ) ;
179+ return adbPath ;
138180 } catch ( err ) {
139- // Cleanup on error
140181 try {
141182 if ( fs . existsSync ( zipPath ) ) fs . unlinkSync ( zipPath ) ;
142183 } catch ( e ) { }
@@ -146,6 +187,9 @@ async function downloadPlatformTools(onProgress) {
146187
147188module . exports = {
148189 getPlatformToolsDir,
190+ getDownloadedPlatformToolsDir,
191+ getDevBundledPlatformToolsDir,
192+ getPackagedPlatformToolsDir,
149193 getBundledAdbPath,
150194 hasBundledAdb,
151195 downloadPlatformTools
0 commit comments