1
1
var https = require ( 'https' ) ,
2
2
unzip = require ( 'unzip' ) ,
3
- fs = require ( 'fs' ) ;
3
+ fs = require ( 'fs' ) ,
4
+ path = require ( 'path' ) ;
4
5
5
6
function ZipBinary ( helper ) {
6
7
var log = helper . log ;
@@ -13,34 +14,44 @@ function ZipBinary(helper) {
13
14
platform = 'win32' ;
14
15
arch = null ;
15
16
}
17
+ const binaryName = 'BrowserStackLocal' + ( platform === 'win32' ? '.exe' : '' ) ;
16
18
17
19
this . args = [ ] ;
18
20
19
- var ensurePath = function ( ) {
21
+ var ensurePath = function ( shouldFail ) {
22
+ var checkPath = '' ;
20
23
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 ) ;
22
26
} catch ( error ) {
23
- log . warn ( 'Cannot read/write to ' + helper . getBasePath ( ) ) ;
27
+ log . warn ( 'Cannot read/write to ' + checkPath ) ;
24
28
helper . fallbackBase ( ) ;
25
29
return ensurePath ( ) ;
26
30
}
27
31
28
32
try {
29
- fs . accessSync ( helper . getBinaryPath ( ) , fs . F_OK ) ;
33
+ checkPath = helper . getBinaryPath ( ) ;
34
+ fs . accessSync ( checkPath , fs . F_OK ) ;
30
35
} 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
+ }
33
44
}
34
45
35
46
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 ) ;
37
48
} catch ( error ) {
38
49
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 ) ;
42
53
} catch ( error ) {
43
- log . warn ( 'Cannot add execute permissions to ' + helper . getBasePath ( ) ) ;
54
+ log . warn ( 'Cannot add execute permissions to ' + checkPath ) ;
44
55
helper . fallbackBase ( ) ;
45
56
return ensurePath ( ) ;
46
57
}
@@ -49,16 +60,23 @@ function ZipBinary(helper) {
49
60
return false ;
50
61
} ;
51
62
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 ) ) {
54
68
var extractStream = unzip . Extract ( {
55
- path : helper . getBasePath ( )
69
+ path : binaryDir
56
70
} ) ;
57
71
https . get ( 'https://www.browserstack.com/browserstack-local/BrowserStackLocal-' + platform + ( arch ? '-' + arch : '' ) + '.zip' , function ( response ) {
58
72
log . info ( 'Downloading binary for ' + platform + ( arch ? '-' + arch : '' ) + ' ...' ) ;
59
73
extractStream . on ( 'close' , function ( ) {
60
74
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
+ } ) ;
62
80
} ) ;
63
81
response . pipe ( extractStream ) ;
64
82
} ) ;
0 commit comments