1
1
var childProcess = require ( 'child_process' ) ,
2
- fs = require ( 'fs' ) ,
3
2
path = require ( 'path' ) ,
4
3
running = require ( 'is-running' ) ,
5
4
LocalBinary = require ( './LocalBinary' ) ,
@@ -10,6 +9,7 @@ function Local(){
10
9
this . pid = undefined ;
11
10
this . key = process . env . BROWSERSTACK_ACCESS_KEY ;
12
11
this . logfile = path . join ( process . cwd ( ) , 'local.log' ) ;
12
+ this . opcode = 'start' ;
13
13
this . exitCallback ;
14
14
this . userArgs = [ ] ;
15
15
@@ -27,54 +27,75 @@ function Local(){
27
27
that . binaryPath = binaryPath ;
28
28
childProcess . exec ( 'echo "" > ' + that . logfile ) ;
29
29
30
- that . tunnel = childProcess . spawn ( binaryPath , that . getBinaryArgs ( ) ) ;
31
- that . tunnel . on ( 'exit' , function ( ) {
32
- that . tunnel = undefined ;
33
- if ( that . exitCallback ) that . exitCallback ( ) ;
34
- } ) ;
35
-
36
- that . stdout = fs . openSync ( that . logfile , 'r' ) ;
37
- var chunkSize = 512 ,
38
- buffer = new Buffer ( 81920 ) ,
39
- bytesRead = 0 ,
40
- error = undefined ;
41
-
42
- while ( true ) {
43
- var bytes = fs . readSync ( that . stdout , buffer , bytesRead , chunkSize , bytesRead ) ;
44
- if ( bytes == 0 ) continue ;
45
-
46
- var buffRead = buffer . slice ( bytesRead , bytesRead + bytes ) ;
47
- bytesRead += bytes ;
30
+ that . opcode = 'start' ;
31
+ that . tunnel = childProcess . execFile ( that . binaryPath , that . getBinaryArgs ( ) , function ( error , stdout , stderr ) {
32
+ if ( error ) callback ( new LocalError ( error . toString ( ) ) ) ;
48
33
49
- var data = buffRead . toString ( ) ;
34
+ var data = { } ;
35
+ if ( stdout )
36
+ data = JSON . parse ( stdout ) ;
37
+ else if ( stderr )
38
+ data = JSON . parse ( stderr ) ;
39
+ else
40
+ callback ( new LocalError ( 'No output received' ) ) ;
50
41
51
- if ( data . match ( that . errorRegex ) ) {
52
- fs . closeSync ( that . stdout ) ;
53
- error = data . match ( that . errorRegex ) [ 0 ] . trim ( ) ;
54
- break ;
55
- }
56
-
57
- if ( data . match ( that . doneRegex ) ) {
58
- fs . closeSync ( that . stdout ) ;
59
- break ;
42
+ if ( data [ 'state' ] != 'connected' ) {
43
+ callback ( new LocalError ( data [ 'message' ] ) ) ;
44
+ } else {
45
+ that . pid = data [ 'pid' ] ;
46
+ callback ( ) ;
60
47
}
61
- }
48
+ } ) ;
62
49
63
- if ( error ) throw new LocalError ( error ) ;
64
- callback ( ) ;
50
+ // that.tunnel = childProcess.spawn(binaryPath, that.getBinaryArgs());
51
+ // that.tunnel.on('exit', function(){
52
+ // that.tunnel = undefined;
53
+ // if(that.exitCallback) that.exitCallback();
54
+ // });
55
+
56
+ // that.stdout = fs.openSync(that.logfile, 'r');
57
+ // var chunkSize = 512,
58
+ // buffer = new Buffer(81920),
59
+ // bytesRead = 0,
60
+ // error = undefined;
61
+
62
+ // while(true){
63
+ // var bytes = fs.readSync(that.stdout, buffer, bytesRead, chunkSize, bytesRead);
64
+ // if(bytes == 0) continue;
65
+
66
+ // var buffRead = buffer.slice(bytesRead, bytesRead+bytes);
67
+ // bytesRead += bytes;
68
+
69
+ // var data = buffRead.toString();
70
+
71
+ // if(data.match(that.errorRegex)){
72
+ // fs.closeSync(that.stdout);
73
+ // error = data.match(that.errorRegex)[0].trim();
74
+ // break;
75
+ // }
76
+
77
+ // if(data.match(that.doneRegex)){
78
+ // fs.closeSync(that.stdout);
79
+ // break;
80
+ // }
81
+ // }
82
+
83
+ // if(error) throw new LocalError(error);
84
+ // callback();
65
85
} ) ;
66
86
} ;
67
87
68
88
this . isRunning = function ( ) {
69
- return this . tunnel && running ( this . tunnel . pid ) ;
89
+ return this . pid && running ( this . pid ) ;
70
90
} ;
71
91
72
92
this . stop = function ( callback ) {
73
- if ( this . tunnel ) {
74
- if ( callback ) this . exitCallback = callback ;
75
- this . tunnel . kill ( ) ;
76
- }
77
- else if ( callback ) callback ( ) ;
93
+ if ( ! this . pid ) return callback ( ) ;
94
+ this . opcode = 'stop' ;
95
+ this . tunnel = childProcess . execFile ( this . binaryPath , this . getBinaryArgs ( ) , function ( error ) {
96
+ if ( error ) callback ( new LocalError ( error . toString ( ) ) ) ;
97
+ callback ( ) ;
98
+ } ) ;
78
99
} ;
79
100
80
101
this . addArgs = function ( options ) {
@@ -185,7 +206,7 @@ function Local(){
185
206
} ;
186
207
187
208
this . getBinaryArgs = function ( ) {
188
- var args = [ '-logFile' , this . logfile ] ;
209
+ var args = [ '-d' , this . opcode , '- logFile', this . logfile ] ;
189
210
if ( this . folderFlag )
190
211
args . push ( this . folderFlag ) ;
191
212
args . push ( this . key ) ;
0 commit comments