Skip to content

Commit def45db

Browse files
committed
Log to a file
Tests pending
1 parent 36d0dcf commit def45db

File tree

5 files changed

+79
-29
lines changed

5 files changed

+79
-29
lines changed

lib/ZipBinary.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
var path = require('path'),
2-
https = require('https'),
1+
var https = require('https'),
32
unzip = require('unzip'),
43
fs = require('fs'),
5-
log = require('./helper').log;
4+
helper = require('./helper'),
5+
log = helper.log;
66

7-
function ZipBinary(platform, arch, bin, ext) {
8-
var self = this;
9-
self.bin = (typeof(bin) == 'undefined' || bin == null || !bin.trim()) ? path.resolve(path.join(__dirname, '..', 'bin', arch ? path.join(platform, arch) : platform)) : bin;
10-
self.path = path.resolve(path.join(self.bin, 'BrowserStackLocal' + (ext ? '.' + ext : '')));
11-
self.command = self.path;
12-
self.args = [];
7+
function ZipBinary(platform, arch) {
8+
this.command = helper.getBinaryPath();
9+
this.args = [];
1310

14-
self.update = function (callback) {
11+
this.update = function (callback) {
1512
var extractStream = unzip.Extract({
16-
path: self.bin
13+
path: helper.getBasePath()
1714
});
1815
https.get('https://www.browserstack.com/browserstack-local/BrowserStackLocal-' + platform + (arch ? '-' + arch : '') + '.zip', function (response) {
1916
log.info('Downloading binary for ' + platform + (arch ? '-' + arch : '') + ' ...');
2017
extractStream.on('close', function () {
2118
log.info('Download complete');
22-
fs.chmod(self.path, '0755', callback);
19+
fs.chmod(helper.getBinaryPath(), '0755', callback);
2320
});
2421
response.pipe(extractStream);
2522
});

lib/browserStackTunnel.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
var fs = require('fs'),
22
childProcess = require('child_process'),
3-
os = require('os'),
43
ZipBinary = require('./ZipBinary'),
5-
log = require('./helper').log;
4+
helper = require('./helper'),
5+
log = helper.log;
66

77
function BrowserStackTunnel(options) {
8+
if (options.path) {
9+
helper.setBasePath(options.path);
10+
}
11+
812
var params = [],
913
startCallback = null,
1014
stopCallback = null,
@@ -20,22 +24,22 @@ function BrowserStackTunnel(options) {
2024
};
2125

2226
var binary;
23-
switch (os.platform()) {
27+
switch (helper.getPlatform()) {
2428
case 'linux':
25-
switch (os.arch()) {
29+
switch (helper.getArch()) {
2630
case 'x64':
27-
binary = new ZipBinary('linux', 'x64', options.path);
31+
binary = new ZipBinary('linux', 'x64');
2832
break;
2933
case 'ia32':
30-
binary = new ZipBinary('linux', 'ia32', options.path);
34+
binary = new ZipBinary('linux', 'ia32');
3135
break;
3236
}
3337
break;
3438
case 'darwin':
35-
binary = new ZipBinary('darwin', 'x64', options.path);
39+
binary = new ZipBinary('darwin', 'x64');
3640
break;
3741
default:
38-
binary = new ZipBinary('win32', null, options.path, 'exe');
42+
binary = new ZipBinary('win32', null);
3943
break;
4044
}
4145

@@ -51,7 +55,7 @@ function BrowserStackTunnel(options) {
5155
}
5256

5357
if (options.verbose) {
54-
params.push('-v');
58+
params.push('-vvv');
5559
}
5660

5761
if (options.force) {
@@ -96,6 +100,7 @@ function BrowserStackTunnel(options) {
96100
};
97101

98102
this.updateState = function (data) {
103+
helper.logBinaryOutput(data.toString());
99104
var state;
100105
this.stdoutData += data.toString();
101106
for (state in this.stateMatchers) {
@@ -163,7 +168,7 @@ function BrowserStackTunnel(options) {
163168

164169
this.startTunnel = function () {
165170
var self = this;
166-
if (!fs.existsSync(binary.path)) {
171+
if (!fs.existsSync(helper.getBinaryPath())) {
167172
log.warn('Binary not present');
168173
binary.update(function () {
169174
self._startTunnel();

lib/helper.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,52 @@
1-
var log = require('npmlog');
1+
var fs = require('fs'),
2+
path = require('path'),
3+
log = require('npmlog'),
4+
os = require('os');
5+
6+
const osHomedir = require('os-homedir');
7+
var basePath = osHomedir(),
8+
appendLogs = true;
9+
10+
exports.getPlatform = function() {
11+
return os.platform();
12+
};
13+
exports.gerArch = function() {
14+
return os.arch();
15+
};
16+
17+
const localBinaryName = 'BrowserStackLocal' + ( exports.getPlatform() === 'win32' ? '.exe' : '' );
18+
const zipName = 'BrowserStackLocal.zip';
19+
const logFileName = 'local.log';
20+
21+
exports.getBinaryPath = function() {
22+
return path.resolve(path.join(basePath, localBinaryName));
23+
};
24+
exports.getZipPath = function() {
25+
return path.resolve(path.join(basePath, zipName));
26+
};
27+
exports.setBasePath = function(path) {
28+
basePath = path;
29+
};
30+
exports.getBasePath = function() {
31+
return basePath;
32+
};
33+
234
if(process.env.NODE_ENV === 'testing') {
335
log.level = 'silent';
36+
appendLogs = false;
437
}
5-
638
exports.log = log;
39+
40+
var getLogFilePath = function() {
41+
return path.resolve(path.join(basePath, logFileName));
42+
};
43+
exports.logBinaryOutput = function(log) {
44+
if(appendLogs) {
45+
fs.appendFile(getLogFilePath(), log, function (err) {
46+
if(err) {
47+
exports.log.warn('Error Ssaving log file to ' + getLogFilePath());
48+
appendLogs = false;
49+
}
50+
});
51+
}
52+
};

node-example.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var options = {
1919
// port: 8080,
2020
// sslFlag: 0
2121
//}],
22-
//path: 'bin',
22+
path: 'bin',
2323
localIdentifier: identifier,
2424
verbose: true,
2525
//proxyUser: '',
@@ -34,11 +34,12 @@ var options = {
3434
local.start(options, function(error) {
3535
console.log('Is Running ' + local.isRunning());
3636
console.log('Started');
37-
console.log('Is Running ' + local.isRunning());
37+
3838
capabilities['browserstack.user'] = process.env.BROWSERSTACK_USERNAME;
39-
console.log('Is Running ' + local.isRunning());
40-
capabilities['browserstack.key'] = process.env.BROWSERSTACK_ACCESS_KEY
41-
console.log('Is Running ' + local.isRunning());
39+
capabilities['browserstack.key'] = process.env.BROWSERSTACK_ACCESS_KEY;
40+
capabilities['browserstack.local'] = true;
41+
capabilities['browserstack.localIdentifier'] = identifier;
42+
4243
driver = new webdriver.Builder().usingServer('http://hub.browserstack.com/wd/hub').withCapabilities(capabilities).build();
4344
console.log('Is Running ' + local.isRunning());
4445
driver.get("http://localhost:3000").then(function() {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"license": "MIT",
1717
"dependencies": {
1818
"npmlog": "2.0.2",
19+
"os-homedir": "^1.0.1",
1920
"unzip": "0.1.11"
2021
},
2122
"devDependencies": {

0 commit comments

Comments
 (0)