Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var BrowserStack = require('browserstack'),
utils = require('../lib/utils');
Server = require('../lib/server').Server;
config = require('../lib/config');
Tunnel = require('../lib/tunnel').Tunnel;
Tunnel = require('../lib/local').Tunnel;

var serverPort = 8888;
var tunnel;
Expand Down
34 changes: 19 additions & 15 deletions lib/tunnel.js → lib/local.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
var exec = require('child_process').exec,
fs = require('fs'),
http = require('http'),
tunnelJar = __dirname + '/BrowserStackTunnel.jar',
windows = (process.platform.match(/win/) != null),
localBinary = __dirname + (windows ? '/BrowserStackTunnel.jar' : '/BrowserStackLocal'),
utils = require('./utils'),
config = require('./config');

var Tunnel = function Tunnel (key, port, tunnelIdentifier, callback, err) {
var Tunnel = function Tunnel (key, port, uniqueIdentifier, callback, err) {
var that = {};

function tunnelLauncher () {
var tunnelCommand = 'java -jar ' + tunnelJar + ' ';
var tunnelCommand = (windows ? 'java -jar ' : '') + localBinary + ' ';
if (config.debug)
tunnelCommand += ' -v ';
tunnelCommand += key + ' ';
tunnelCommand += 'localhost' + ',';
tunnelCommand += port.toString() + ',';
tunnelCommand += '0';
tunnelCommand += (typeof tunnelIdentifier === 'undefined')? ' -force -onlyAutomate' : ' -tunnelIdentifier ' + tunnelIdentifier;
tunnelCommand += (typeof uniqueIdentifier === 'undefined')? ' -force -onlyAutomate' : ' -tunnelIdentifier ' + uniqueIdentifier;

if (typeof callback !== 'function') {
callback = function () {};
}

console.log("Launching tunnel");
console.log("[%s] Launching tunnel", new Date());
var subProcess = exec(tunnelCommand, function (error, stdout, stderr) {
console.log(stderr);
console.log(error);
if (stdout.indexOf('Error') >= 0) {
console.log("Tunnel launching failed");
console.log("[%s] Tunnel launching failed", new Date());
console.log(stdout);
process.exit(1);
}
Expand All @@ -38,8 +40,7 @@ var Tunnel = function Tunnel (key, port, tunnelIdentifier, callback, err) {

setTimeout(function () {
if (!running) {
utils.alertBrowserStack("Tunnel launch timeout",
'Stdout:\n' + data);
utils.alertBrowserStack("Tunnel launch timeout", 'Stdout:\n' + data);
}
}, 30 * 1000);

Expand All @@ -52,28 +53,31 @@ var Tunnel = function Tunnel (key, port, tunnelIdentifier, callback, err) {

if (data.indexOf(runMatcher) >= 0) {
running = true;
console.log("Tunnel launched");
console.log("[%s] Tunnel launched", new Date());
callback();
}
});

that.process = subProcess;
}

fs.exists(tunnelJar, function (exists) {
fs.exists(localBinary, function (exists) {
if (exists) {
fs.unlinkSync(tunnelJar);
fs.unlinkSync(localBinary);
// tunnelLauncher();
// return;
}
console.log('Downloading tunnel jar to `%s`', tunnelJar);
console.log('Downloading BrowserStack Local to `%s`', localBinary);

var file = fs.createWriteStream(tunnelJar);
var file = fs.createWriteStream(localBinary);
var request = http.get(
"http://www.browserstack.com/BrowserStackTunnel.jar",
(windows ? "http://www.browserstack.com/BrowserStackTunnel.jar" : ("http://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-" + process.platform + "-" + process.arch)),
function(response) {
response.pipe(file);

response.on('end', function () {
tunnelLauncher();
fs.chmodSync(localBinary, 0700);
setTimeout(function() {tunnelLauncher();}, 100);
});
}
);
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var alertBrowserStack = function alertBrowserStack (subject, content, params, fn
var urlObject = url.parse(endpoint);

var context = config.alert_context || "Runner alert";
console.log("[%s] %s", context, subject);
console.log("[%s] [%s] %s", new Date(), context, subject);

if (typeof fn !== 'function') {
if (typeof params === 'function') {
Expand Down