11var childProcess = require ( 'child_process' ) ,
2- fs = require ( 'fs' ) ,
32 path = require ( 'path' ) ,
43 running = require ( 'is-running' ) ,
54 LocalBinary = require ( './LocalBinary' ) ,
@@ -10,6 +9,7 @@ function Local(){
109 this . pid = undefined ;
1110 this . key = process . env . BROWSERSTACK_ACCESS_KEY ;
1211 this . logfile = path . join ( process . cwd ( ) , 'local.log' ) ;
12+ this . opcode = 'start' ;
1313 this . exitCallback ;
1414 this . userArgs = [ ] ;
1515
@@ -27,54 +27,75 @@ function Local(){
2727 that . binaryPath = binaryPath ;
2828 childProcess . exec ( 'echo "" > ' + that . logfile ) ;
2929
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 ( ) ) ) ;
4833
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' ) ) ;
5041
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 ( ) ;
6047 }
61- }
48+ } ) ;
6249
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();
6585 } ) ;
6686 } ;
6787
6888 this . isRunning = function ( ) {
69- return this . tunnel && running ( this . tunnel . pid ) ;
89+ return this . pid && running ( this . pid ) ;
7090 } ;
7191
7292 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+ } ) ;
7899 } ;
79100
80101 this . addArgs = function ( options ) {
@@ -185,7 +206,7 @@ function Local(){
185206 } ;
186207
187208 this . getBinaryArgs = function ( ) {
188- var args = [ '-logFile' , this . logfile ] ;
209+ var args = [ '-d' , this . opcode , '- logFile', this . logfile ] ;
189210 if ( this . folderFlag )
190211 args . push ( this . folderFlag ) ;
191212 args . push ( this . key ) ;
0 commit comments