1
1
var Log = require ( './logger' ) ,
2
2
logger = new Log ( global . logLevel ) ,
3
- exec = require ( 'child_process' ) . exec ,
3
+ exec = require ( 'child_process' ) . execFile ,
4
4
fs = require ( 'fs' ) ,
5
5
http = require ( 'http' ) ,
6
6
windows = ( ( process . platform . match ( / w i n 3 2 / ) || process . platform . match ( / w i n 6 4 / ) ) !== null ) ,
7
- localBinary = process . cwd ( ) + ( windows ? '/BrowserStackTunnel.jar ' : '/BrowserStackLocal ' ) ,
7
+ localBinary = process . cwd ( ) + '/BrowserStackLocal' + ( windows ? '.exe ' : '' ) ,
8
8
utils = require ( './utils' ) ,
9
9
config = require ( './config' ) ;
10
10
11
11
var Tunnel = function Tunnel ( key , port , uniqueIdentifier , callback ) {
12
12
var that = { } ;
13
13
14
14
function tunnelLauncher ( ) {
15
- var tunnelCommand = ( windows ? 'java -jar ' : '' ) + localBinary + ' ' ;
16
- if ( config . debug ) {
17
- tunnelCommand += ' -v ' ;
18
- }
19
- tunnelCommand += key + ' ' ;
20
- tunnelCommand += 'localhost' + ',' ;
21
- tunnelCommand += port . toString ( ) + ',' ;
22
- tunnelCommand += '0' ;
23
- tunnelCommand += ( typeof uniqueIdentifier === 'undefined' ) ? ' -force -onlyAutomate' : ' -tunnelIdentifier ' + uniqueIdentifier ;
24
- tunnelCommand += checkAndAddProxy ( ) ;
15
+ var tunnelOptions = getTunnelOptions ( key , uniqueIdentifier ) ;
25
16
26
17
if ( typeof callback !== 'function' ) {
27
18
callback = function ( ) { } ;
28
19
}
29
20
30
21
logger . debug ( '[%s] Launching tunnel' , new Date ( ) ) ;
31
- var subProcess = exec ( tunnelCommand , function ( error , stdout , stderr ) {
22
+
23
+ var subProcess = exec ( localBinary , tunnelOptions , function ( error , stdout , stderr ) {
32
24
logger . debug ( stderr ) ;
33
25
logger . debug ( error ) ;
34
26
if ( stdout . indexOf ( 'Error' ) >= 0 || error ) {
35
27
logger . debug ( '[%s] Tunnel launching failed' , new Date ( ) ) ;
36
28
logger . debug ( stdout ) ;
37
- process . kill ( process . pid , 'SIGINT' ) ;
29
+ process . exit ( 'SIGINT' ) ;
38
30
}
39
31
} ) ;
40
32
@@ -67,30 +59,44 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback) {
67
59
that . process = subProcess ;
68
60
}
69
61
70
- function checkAndAddProxy ( ) {
71
- var proxy = config . proxy ;
72
- if ( typeof proxy === 'undefined' ) {
73
- return '' ;
62
+ function getTunnelOptions ( key , uniqueIdentifier ) {
63
+ var options = [ key ] ;
64
+
65
+ if ( config . debug ) {
66
+ options . push ( '-v' ) ;
67
+ }
68
+
69
+ if ( ! uniqueIdentifier ) {
70
+ options . push ( '-force' ) ;
71
+ options . push ( '-onlyAutomate' ) ;
72
+ } else {
73
+ options . push ( '-localIdentifier ' + uniqueIdentifier ) ;
74
74
}
75
- var proxyCommand = '' ;
76
- proxyCommand += ' -proxyHost ' + proxy . host ;
77
- proxyCommand += ' -proxyPort ' + proxy . port ;
78
- if ( typeof proxy . username !== 'undefined' ) {
79
- proxyCommand += ' -proxyUser ' + proxy . username ;
80
- proxyCommand += ' -proxyPass ' + proxy . password ;
75
+
76
+ var proxy = config . proxy ;
77
+
78
+ if ( proxy ) {
79
+ options . push ( '-proxyHost ' + proxy . host ) ;
80
+ options . push ( '-proxyPort ' + proxy . port ) ;
81
+
82
+ if ( proxy . username && proxy . password ) {
83
+ options . push ( '-proxyUser ' + proxy . username ) ;
84
+ options . push ( '-proxyPass ' + proxy . password ) ;
85
+ }
81
86
}
82
- return proxyCommand ;
87
+
88
+ return options ;
83
89
}
84
90
85
91
fs . exists ( localBinary , function ( exists ) {
86
92
if ( exists ) {
87
93
tunnelLauncher ( ) ;
88
94
return ;
89
95
}
90
- logger . debug ( 'Downloading BrowserStack Local to `%s` ' , localBinary ) ;
96
+ logger . debug ( 'Downloading BrowserStack Local to "%s" ' , localBinary ) ;
91
97
92
98
var file = fs . createWriteStream ( localBinary ) ;
93
- http . get ( windows ? 'http://www.browserstack .com/BrowserStackTunnel.jar ' : ( 'http://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-' + process . platform + '-' + process . arch ) ,
99
+ http . get ( ( windows ? 'http://s3.amazonaws .com/browserStack/browserstack-local/BrowserStackLocal.exe ' : ( 'http://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-' + process . platform + '-' + process . arch ) ) ,
94
100
function ( response ) {
95
101
response . pipe ( file ) ;
96
102
@@ -101,7 +107,7 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback) {
101
107
} , 100 ) ;
102
108
} ) . on ( 'error' , function ( e ) {
103
109
logger . info ( 'Got error while downloading binary: ' + e . message ) ;
104
- process . kill ( process . pid , 'SIGINT' ) ;
110
+ process . exit ( 'SIGINT' ) ;
105
111
} ) ;
106
112
} ) ;
107
113
} ) ;
0 commit comments