1
1
var exec = require ( 'child_process' ) . exec ,
2
2
fs = require ( 'fs' ) ,
3
3
http = require ( 'http' ) ,
4
- tunnelJar = __dirname + '/BrowserStackTunnel.jar' ,
4
+ windows = ( process . platform . match ( / w i n / ) != null ) ,
5
+ localBinary = __dirname + ( windows ? '/BrowserStackTunnel.jar' : '/BrowserStackLocal' ) ,
5
6
utils = require ( './utils' ) ,
6
7
config = require ( './config' ) ;
7
8
8
- var Tunnel = function Tunnel ( key , port , tunnelIdentifier , callback , err ) {
9
+ var Tunnel = function Tunnel ( key , port , uniqueIdentifier , callback , err ) {
9
10
var that = { } ;
10
11
11
12
function tunnelLauncher ( ) {
12
- var tunnelCommand = 'java -jar ' + tunnelJar + ' ' ;
13
+ var tunnelCommand = ( windows ? 'java -jar ' : '' ) + localBinary + ' ' ;
13
14
if ( config . debug )
14
15
tunnelCommand += ' -v ' ;
15
16
tunnelCommand += key + ' ' ;
16
17
tunnelCommand += 'localhost' + ',' ;
17
18
tunnelCommand += port . toString ( ) + ',' ;
18
19
tunnelCommand += '0' ;
19
- tunnelCommand += ( typeof tunnelIdentifier === 'undefined' ) ? ' -force -onlyAutomate' : ' -tunnelIdentifier ' + tunnelIdentifier ;
20
+ tunnelCommand += ( typeof uniqueIdentifier === 'undefined' ) ? ' -force -onlyAutomate' : ' -tunnelIdentifier ' + uniqueIdentifier ;
20
21
21
22
if ( typeof callback !== 'function' ) {
22
23
callback = function ( ) { } ;
23
24
}
24
25
25
- console . log ( "Launching tunnel" ) ;
26
+ console . log ( "[%s] Launching tunnel" , new Date ( ) ) ;
26
27
var subProcess = exec ( tunnelCommand , function ( error , stdout , stderr ) {
27
28
console . log ( stderr ) ;
29
+ console . log ( error ) ;
28
30
if ( stdout . indexOf ( 'Error' ) >= 0 ) {
29
- console . log ( "Tunnel launching failed" ) ;
31
+ console . log ( "[%s] Tunnel launching failed" , new Date ( ) ) ;
30
32
console . log ( stdout ) ;
31
33
process . exit ( 1 ) ;
32
34
}
@@ -38,8 +40,7 @@ var Tunnel = function Tunnel (key, port, tunnelIdentifier, callback, err) {
38
40
39
41
setTimeout ( function ( ) {
40
42
if ( ! running ) {
41
- utils . alertBrowserStack ( "Tunnel launch timeout" ,
42
- 'Stdout:\n' + data ) ;
43
+ utils . alertBrowserStack ( "Tunnel launch timeout" , 'Stdout:\n' + data ) ;
43
44
}
44
45
} , 30 * 1000 ) ;
45
46
@@ -52,28 +53,31 @@ var Tunnel = function Tunnel (key, port, tunnelIdentifier, callback, err) {
52
53
53
54
if ( data . indexOf ( runMatcher ) >= 0 ) {
54
55
running = true ;
55
- console . log ( "Tunnel launched" ) ;
56
+ console . log ( "[%s] Tunnel launched" , new Date ( ) ) ;
56
57
callback ( ) ;
57
58
}
58
59
} ) ;
59
60
60
61
that . process = subProcess ;
61
62
}
62
63
63
- fs . exists ( tunnelJar , function ( exists ) {
64
+ fs . exists ( localBinary , function ( exists ) {
64
65
if ( exists ) {
65
- fs . unlinkSync ( tunnelJar ) ;
66
+ fs . unlinkSync ( localBinary ) ;
67
+ // tunnelLauncher();
68
+ // return;
66
69
}
67
- console . log ( 'Downloading tunnel jar to `%s`' , tunnelJar ) ;
70
+ console . log ( 'Downloading BrowserStack Local to `%s`' , localBinary ) ;
68
71
69
- var file = fs . createWriteStream ( tunnelJar ) ;
72
+ var file = fs . createWriteStream ( localBinary ) ;
70
73
var request = http . get (
71
- "http://www.browserstack.com/BrowserStackTunnel.jar" ,
74
+ ( windows ? "http://www.browserstack.com/BrowserStackTunnel.jar" : ( "http://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-" + process . platform + "-" + process . arch ) ) ,
72
75
function ( response ) {
73
76
response . pipe ( file ) ;
74
77
75
78
response . on ( 'end' , function ( ) {
76
- tunnelLauncher ( ) ;
79
+ fs . chmodSync ( localBinary , 0700 ) ;
80
+ setTimeout ( function ( ) { tunnelLauncher ( ) ; } , 100 ) ;
77
81
} ) ;
78
82
}
79
83
) ;
0 commit comments