11var https = require ( 'https' ) ,
22 unzip = require ( 'unzip' ) ,
3- fs = require ( 'fs' ) ;
3+ fs = require ( 'fs' ) ,
4+ path = require ( 'path' ) ;
45
56function ZipBinary ( helper ) {
67 var log = helper . log ;
@@ -13,34 +14,44 @@ function ZipBinary(helper) {
1314 platform = 'win32' ;
1415 arch = null ;
1516 }
17+ const binaryName = 'BrowserStackLocal' + ( platform === 'win32' ? '.exe' : '' ) ;
1618
1719 this . args = [ ] ;
1820
19- var ensurePath = function ( ) {
21+ var ensurePath = function ( shouldFail ) {
22+ var checkPath = '' ;
2023 try {
21- fs . accessSync ( helper . getBasePath ( ) , fs . R_OK | fs . W_OK ) ;
24+ checkPath = path . join ( helper . getBinaryPath ( ) , '..' ) ;
25+ fs . accessSync ( checkPath , fs . R_OK | fs . W_OK ) ;
2226 } catch ( error ) {
23- log . warn ( 'Cannot read/write to ' + helper . getBasePath ( ) ) ;
27+ log . warn ( 'Cannot read/write to ' + checkPath ) ;
2428 helper . fallbackBase ( ) ;
2529 return ensurePath ( ) ;
2630 }
2731
2832 try {
29- fs . accessSync ( helper . getBinaryPath ( ) , fs . F_OK ) ;
33+ checkPath = helper . getBinaryPath ( ) ;
34+ fs . accessSync ( checkPath , fs . F_OK ) ;
3035 } catch ( error ) {
31- log . warn ( 'Binary file is not present at ' + helper . getBinaryPath ( ) ) ;
32- return true ;
36+ if ( shouldFail ) {
37+ log . warn ( 'Download failed to ' + checkPath ) ;
38+ helper . fallbackBase ( ) ;
39+ return ensurePath ( ) ;
40+ } else {
41+ log . warn ( 'Binary file is not present at ' + checkPath ) ;
42+ return true ;
43+ }
3344 }
3445
3546 try {
36- fs . accessSync ( helper . getBinaryPath ( ) , fs . R_OK | fs . W_OK | fs . X_OK ) ;
47+ fs . accessSync ( checkPath , fs . R_OK | fs . W_OK | fs . X_OK ) ;
3748 } catch ( error ) {
3849 try {
39- log . warn ( 'Adding execute permissions to ' + helper . getBinaryPath ( ) ) ;
40- fs . chmodSync ( helper . getBinaryPath ( ) , '0755' ) ;
41- fs . accessSync ( helper . getBinaryPath ( ) , fs . X_OK ) ;
50+ log . warn ( 'Adding execute permissions to ' + checkPath ) ;
51+ fs . chmodSync ( checkPath , '0755' ) ;
52+ fs . accessSync ( checkPath , fs . X_OK ) ;
4253 } catch ( error ) {
43- log . warn ( 'Cannot add execute permissions to ' + helper . getBasePath ( ) ) ;
54+ log . warn ( 'Cannot add execute permissions to ' + checkPath ) ;
4455 helper . fallbackBase ( ) ;
4556 return ensurePath ( ) ;
4657 }
@@ -49,16 +60,23 @@ function ZipBinary(helper) {
4960 return false ;
5061 } ;
5162
52- this . update = function ( callback ) {
53- if ( ensurePath ( ) ) {
63+ this . update = function ( callback , shouldFail ) {
64+ var self = this ;
65+ var binaryPath = helper . getBinaryPath ( ) ;
66+ var binaryDir = path . join ( binaryPath , '..' ) ;
67+ if ( ensurePath ( shouldFail ) ) {
5468 var extractStream = unzip . Extract ( {
55- path : helper . getBasePath ( )
69+ path : binaryDir
5670 } ) ;
5771 https . get ( 'https://www.browserstack.com/browserstack-local/BrowserStackLocal-' + platform + ( arch ? '-' + arch : '' ) + '.zip' , function ( response ) {
5872 log . info ( 'Downloading binary for ' + platform + ( arch ? '-' + arch : '' ) + ' ...' ) ;
5973 extractStream . on ( 'close' , function ( ) {
6074 log . info ( 'Download complete' ) ;
61- fs . chmod ( helper . getBinaryPath ( ) , '0755' , callback ) ;
75+ fs . rename ( path . join ( binaryDir , binaryName ) , binaryPath , function ( ) {
76+ fs . chmod ( binaryPath , '0755' , function ( ) {
77+ self . update . apply ( self , [ callback , true ] ) ;
78+ } ) ;
79+ } ) ;
6280 } ) ;
6381 response . pipe ( extractStream ) ;
6482 } ) ;
0 commit comments