11var Log = require ( './logger' ) ,
22 logger = new Log ( global . logLevel ) ,
3- exec = require ( 'child_process' ) . exec ,
3+ exec = require ( 'child_process' ) . execFile ,
44 fs = require ( 'fs' ) ,
55 http = require ( 'http' ) ,
66 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 ' : '' ) ,
88 utils = require ( './utils' ) ,
99 config = require ( './config' ) ;
1010
1111var Tunnel = function Tunnel ( key , port , uniqueIdentifier , callback ) {
1212 var that = { } ;
1313
1414 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 ) ;
2516
2617 if ( typeof callback !== 'function' ) {
2718 callback = function ( ) { } ;
2819 }
2920
3021 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 ) {
3224 logger . debug ( stderr ) ;
3325 logger . debug ( error ) ;
3426 if ( stdout . indexOf ( 'Error' ) >= 0 || error ) {
3527 logger . debug ( '[%s] Tunnel launching failed' , new Date ( ) ) ;
3628 logger . debug ( stdout ) ;
37- process . kill ( process . pid , 'SIGINT' ) ;
29+ process . exit ( 'SIGINT' ) ;
3830 }
3931 } ) ;
4032
@@ -67,30 +59,44 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback) {
6759 that . process = subProcess ;
6860 }
6961
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 ) ;
7474 }
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+ }
8186 }
82- return proxyCommand ;
87+
88+ return options ;
8389 }
8490
8591 fs . exists ( localBinary , function ( exists ) {
8692 if ( exists ) {
8793 tunnelLauncher ( ) ;
8894 return ;
8995 }
90- logger . debug ( 'Downloading BrowserStack Local to `%s` ' , localBinary ) ;
96+ logger . debug ( 'Downloading BrowserStack Local to "%s" ' , localBinary ) ;
9197
9298 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 ) ) ,
94100 function ( response ) {
95101 response . pipe ( file ) ;
96102
@@ -101,7 +107,7 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback) {
101107 } , 100 ) ;
102108 } ) . on ( 'error' , function ( e ) {
103109 logger . info ( 'Got error while downloading binary: ' + e . message ) ;
104- process . kill ( process . pid , 'SIGINT' ) ;
110+ process . exit ( 'SIGINT' ) ;
105111 } ) ;
106112 } ) ;
107113 } ) ;
0 commit comments