From 211d80bcfc4533f0fe635f35a89aaaee76f5e9c1 Mon Sep 17 00:00:00 2001 From: Vibhaj Rajan Date: Tue, 10 May 2016 14:48:24 +0530 Subject: [PATCH] added daemon mode, bumped up version --- .gitignore | 1 + lib/Local.js | 99 +++++++++++++++++++++++++++++++-------------------- package.json | 2 +- test/local.js | 12 +++---- 4 files changed, 67 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index 940da60..204325e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ bin/ node_modules/ npm-debug.log local.log +browserstack.err diff --git a/lib/Local.js b/lib/Local.js index 8a286be..ce00a15 100644 --- a/lib/Local.js +++ b/lib/Local.js @@ -1,5 +1,4 @@ var childProcess = require('child_process'), - fs = require('fs'), path = require('path'), running = require('is-running'), LocalBinary = require('./LocalBinary'), @@ -10,6 +9,7 @@ function Local(){ this.pid = undefined; this.key = process.env.BROWSERSTACK_ACCESS_KEY; this.logfile = path.join(process.cwd(), 'local.log'); + this.opcode = 'start'; this.exitCallback; this.userArgs = []; @@ -27,54 +27,75 @@ function Local(){ that.binaryPath = binaryPath; childProcess.exec('echo "" > ' + that.logfile); - that.tunnel = childProcess.spawn(binaryPath, that.getBinaryArgs()); - that.tunnel.on('exit', function(){ - that.tunnel = undefined; - if(that.exitCallback) that.exitCallback(); - }); - - that.stdout = fs.openSync(that.logfile, 'r'); - var chunkSize = 512, - buffer = new Buffer(81920), - bytesRead = 0, - error = undefined; - - while(true){ - var bytes = fs.readSync(that.stdout, buffer, bytesRead, chunkSize, bytesRead); - if(bytes == 0) continue; - - var buffRead = buffer.slice(bytesRead, bytesRead+bytes); - bytesRead += bytes; + that.opcode = 'start'; + that.tunnel = childProcess.execFile(that.binaryPath, that.getBinaryArgs(), function(error, stdout, stderr){ + if(error) callback(new LocalError(error.toString())); - var data = buffRead.toString(); + var data = {}; + if(stdout) + data = JSON.parse(stdout); + else if(stderr) + data = JSON.parse(stderr); + else + callback(new LocalError('No output received')); - if(data.match(that.errorRegex)){ - fs.closeSync(that.stdout); - error = data.match(that.errorRegex)[0].trim(); - break; - } - - if(data.match(that.doneRegex)){ - fs.closeSync(that.stdout); - break; + if(data['state'] != 'connected'){ + callback(new LocalError(data['message'])); + } else { + that.pid = data['pid']; + callback(); } - } + }); - if(error) throw new LocalError(error); - callback(); + // that.tunnel = childProcess.spawn(binaryPath, that.getBinaryArgs()); + // that.tunnel.on('exit', function(){ + // that.tunnel = undefined; + // if(that.exitCallback) that.exitCallback(); + // }); + + // that.stdout = fs.openSync(that.logfile, 'r'); + // var chunkSize = 512, + // buffer = new Buffer(81920), + // bytesRead = 0, + // error = undefined; + + // while(true){ + // var bytes = fs.readSync(that.stdout, buffer, bytesRead, chunkSize, bytesRead); + // if(bytes == 0) continue; + + // var buffRead = buffer.slice(bytesRead, bytesRead+bytes); + // bytesRead += bytes; + + // var data = buffRead.toString(); + + // if(data.match(that.errorRegex)){ + // fs.closeSync(that.stdout); + // error = data.match(that.errorRegex)[0].trim(); + // break; + // } + + // if(data.match(that.doneRegex)){ + // fs.closeSync(that.stdout); + // break; + // } + // } + + // if(error) throw new LocalError(error); + // callback(); }); }; this.isRunning = function(){ - return this.tunnel && running(this.tunnel.pid); + return this.pid && running(this.pid); }; this.stop = function (callback) { - if (this.tunnel) { - if(callback) this.exitCallback = callback; - this.tunnel.kill(); - } - else if(callback) callback(); + if(!this.pid) return callback(); + this.opcode = 'stop'; + this.tunnel = childProcess.execFile(this.binaryPath, this.getBinaryArgs(), function(error){ + if(error) callback(new LocalError(error.toString())); + callback(); + }); }; this.addArgs = function(options){ @@ -185,7 +206,7 @@ function Local(){ }; this.getBinaryArgs = function(){ - var args = ['-logFile', this.logfile]; + var args = ['-d', this.opcode, '-logFile', this.logfile]; if(this.folderFlag) args.push(this.folderFlag); args.push(this.key); diff --git a/package.json b/package.json index a087691..60c3094 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "browserstack-local", - "version": "0.1.0", + "version": "0.2.0", "description": "Nodejs bindings for BrowserStack Local", "engine": "^0.10.44", "main": "index.js", diff --git a/test/local.js b/test/local.js index 4d9ac5f..1413e3f 100644 --- a/test/local.js +++ b/test/local.js @@ -29,17 +29,15 @@ describe('Local', function () { it('should throw error on running multiple binary', function (done) { this.timeout(60000); - bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY }, function(){ + bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY }, function(error){ bsLocal_2 = new browserstack.Local(); var tempLogPath = path.join(process.cwd(), 'log2.log'); - try{ - bsLocal_2.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, 'logfile': tempLogPath }, function(){}); - } - catch(err){ - 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'); + + bsLocal_2.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, 'logfile': tempLogPath }, function(error){ + 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'); fs.unlinkSync(tempLogPath); done(); - } + }); }); });