Skip to content

Commit 211d80b

Browse files
committed
added daemon mode, bumped up version
1 parent 1108f74 commit 211d80b

File tree

4 files changed

+67
-47
lines changed

4 files changed

+67
-47
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ bin/
22
node_modules/
33
npm-debug.log
44
local.log
5+
browserstack.err

lib/Local.js

+60-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var 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);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "browserstack-local",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Nodejs bindings for BrowserStack Local",
55
"engine": "^0.10.44",
66
"main": "index.js",

test/local.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@ describe('Local', function () {
2929

3030
it('should throw error on running multiple binary', function (done) {
3131
this.timeout(60000);
32-
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY }, function(){
32+
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY }, function(error){
3333
bsLocal_2 = new browserstack.Local();
3434
var tempLogPath = path.join(process.cwd(), 'log2.log');
35-
try{
36-
bsLocal_2.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, 'logfile': tempLogPath }, function(){});
37-
}
38-
catch(err){
39-
expect(err.toString().trim()).to.equal('LocalError: *** Error: Either another browserstack local client is running on your machine or some server is listening on port 45691');
35+
36+
bsLocal_2.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, 'logfile': tempLogPath }, function(error){
37+
expect(error.toString().trim()).to.equal('LocalError: Either another browserstack local client is running on your machine or some server is listening on port 45691');
4038
fs.unlinkSync(tempLogPath);
4139
done();
42-
}
40+
});
4341
});
4442
});
4543

0 commit comments

Comments
 (0)