Skip to content

Commit

Permalink
Merge pull request #25 from ekryski/discovery-refactoring
Browse files Browse the repository at this point in the history
Finalize refactoring
  • Loading branch information
daffl committed Jul 10, 2014
2 parents f9efb75 + c084f9d commit 9c7de4f
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 135 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ Local launchers look up all currently installed browsers and allow you to start

// Launch a local browser
launch.local(function(err, local) {
local.browsers // -> List of all browsers found locally
launcher.browsers(function(error, browsers) {
// -> List of all browsers found locally with version
});
local.firefox('http://url', function(err, instance) {
// An instance is an event emitter
instance.on('stop', function() {
Expand All @@ -53,7 +56,10 @@ Launchpad allows you to start BrowserStack workers through its API like this:
password : 'password'
},
function(err, browserstack) {
browserstack.browsers // -> List of all browsers
launcher.browsers(function(error, browsers) {
// -> List of all Browserstack browsers
});
browserstack.ie('http://url', function(err, instance) {
// Shut the instance down after 5 seconds
setTimeout(function() {
Expand Down Expand Up @@ -94,7 +100,10 @@ the given host:
username : 'launcher',
password : 'testing'
}, function(err, api) {
api.browsers // -> List of browsers found on ie7machine
launcher.browsers(function(error, browsers) {
// -> List of all browsers found on ie7machine
});
api('http://github.com', {
browser : 'safari',
version : 'latest'
Expand Down
103 changes: 0 additions & 103 deletions README.md~

This file was deleted.

2 changes: 1 addition & 1 deletion examples/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var launch = require('../lib');
launch.local(function(err, launcher) {
// User the launcher api
launcher('http://github.com/ekryski', {
browser : 'chrome'
browser: 'safari'
}, function(error, worker) {
if(error) {
console.log('Error:', error);
Expand Down
4 changes: 3 additions & 1 deletion lib/local/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ module.exports = function (browser) {
}

// Run the pathQuery to see if we can find it somewhere else
return Q.nfcall(exec, current.pathQuery).then(function (stdout) {
return Q.nfcall(exec, current.pathQuery, {
cwd: current.cwd || '.'
}).then(function (stdout) {
var path = utils.getStdout(stdout);

if (!path) {
Expand Down
6 changes: 3 additions & 3 deletions lib/local/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ module.exports = function (settings, callback) {
}

var api = function(url, options, callback) {
var name = options.name;
var name = options.browser;

getBrowser(_.extend({ name: name }, platform[name])).then(function(browser) {
if(browser === null) {
return Q.reject(new Error('Browser ' + options.name + ' not available.'));
return Q.reject(new Error('Browser ' + name + ' not available.'));
}

var args = browser.args || [];
Expand Down Expand Up @@ -63,7 +63,7 @@ module.exports = function (settings, callback) {
options = {};
}

options.name = name;
options.browser = name;

return api(url, options, callback);
}
Expand Down
24 changes: 12 additions & 12 deletions lib/local/instance.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var path = require('path');
var spawn = require("child_process").spawn;
var exec = require("child_process").exec;
var EventEmitter = require('events').EventEmitter;

var getProcessId = function (name, callback) {

var commands = {
win32: 'tasklist /fi "Windowtitle eq ' + name + '" /fi "STATUS eq running"', //Not sure if this is right. reference: http://technet.microsoft.com/en-us/library/bb491010.aspx
darwin: "ps -clx | grep '" + name + "$' | awk '{print $2}' | head -1",
linux: "ps -ax | grep '" + name + "$' | awk '{print $2}' | head -1",
freebsd: "ps -clx | grep '" + name + "$' | awk '{print $2}' | head -1",
Expand Down Expand Up @@ -44,6 +44,8 @@ var Instance = function (cmd, args, settings, options) {
this.stderr = childProcess.stderr;
this.id = childProcess.pid;
this.process = childProcess;
this.cmd = cmd;
this.args = args;
};

Instance.prototype = new EventEmitter();
Expand All @@ -66,16 +68,12 @@ Instance.prototype.stop = function (callback) {

if (this.options.command === 'open') {
exec('osascript -e \'tell application "' + self.options.process + '" to quit\'');
}
else if (process.platform === 'win32') {
this.getPid(function (err, pid) {
if (err) {
callback(err);
}
spawn('taskkill', ['/PID', pid]);
});
}
else {
} else if (process.platform === 'win32') {
exec('taskkill /IM ' + (this.options.imageName || path.basename(this.cmd)))
.once('exit', function(data) {
self.emit('stop', data);
});
} else {
this.process.kill();
}
};
Expand All @@ -96,7 +94,9 @@ exports.start = function (cmd, args, settings, options, callback) {
return new Instance(cmd, args, settings, options);
};

if (options.process) {
// Check if the process is already running but only if it's a browser we
// can only launch once
if (options.process && !options.multi) {
getProcessId(options.process, function (err, pid) {
if (!err) {
return callback(new Error(options.process + ' seems already running with process id ' + pid));
Expand Down
3 changes: 2 additions & 1 deletion lib/local/platform/macos.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ module.exports = {
pathQuery: 'which phantomjs',
process: 'phantomjs',
args: [path.join(__dirname, '..', '..', '..', 'resources/phantom.js')],
defaultLocation: '/usr/local/bin/phantomjs'
defaultLocation: '/usr/local/bin/phantomjs',
multi: true
}
}
3 changes: 2 additions & 1 deletion lib/local/platform/unix.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
phantom: {
pathQuery: 'which phantomjs',
process: 'phantomjs',
args: [path.join(__dirname, '..', '..', '..', 'resources/phantom.js')]
args: [path.join(__dirname, '..', '..', '..', 'resources/phantom.js')],
multi: true
}
};
20 changes: 14 additions & 6 deletions lib/local/platform/windows.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var os = require('os');
var path = require('path');
var programFiles = os.arch() === "x64" ? process.env["ProgramFiles(x86)"] : process.env.ProgramFiles;
var cwd = path.dirname(programFiles);

function getPath() {
return path.join.apply(path, arguments);
Expand All @@ -9,28 +10,35 @@ function getPath() {
module.exports = {
chrome: {
defaultLocation: getPath(process.env.LOCALAPPDATA, 'Google', 'Chrome', 'Application', 'chrome.exe') ,
pathQuery: 'dir /s /b chrome.exe'
pathQuery: 'dir /s /b chrome.exe',
cwd: cwd
},
canary: {
defaultLocation: getPath(process.env.LOCALAPPDATA, 'Google', 'Chrome SxS', 'Application', 'chrome.exe')
},
firefox: {
defaultLocation: getPath(programFiles, 'Mozilla Firefox', 'firefox.exe'),
pathQuery: 'dir /s /b firefox.exe'
pathQuery: 'dir /s /b firefox.exe',
cwd: cwd
},
aurora: {
defaultLocation: getPath(programFiles, 'Aurora', 'firefox.exe')
},
opera: {
defaultLocation: getPath(programFiles, 'Opera', 'launcher.exe'),
pathQuery: 'dir /s /b opera.exe'
pathQuery: 'dir /s /b opera.exe',
cwd: cwd,
imageName: 'opera.exe'
},
ie: {
defaultLocation: getPath(programFiles, 'Internet Explorer', 'iexplore.exe'),
pathQuery: 'dir /s /b iexplore.exe'
pathQuery: 'dir /s /b iexplore.exe',
cwd: cwd
},
phantomjs: {
search: 'phantomjs.exe',
args: path.join(__dirname, '..', '..', '..', 'resources/phantom.js')
pathQuery: 'dir /s /b phantomjs.exe',
args: [path.join(__dirname, '..', '..', '..', 'resources/phantom.js')],
multi: true,
cwd: cwd
}
}
1 change: 0 additions & 1 deletion lib/remote/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var restify = require('restify');
var _ = require('underscore');
var EventEmitter = require('events').EventEmitter;
var logger = require('winston');

var localLauncher = require('../local');
var pkg = require('../../package.json');
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"main": "lib/index.js",
"engines": {
"node": "~0.8"
"node": "~0.10"
},
"scripts": {
"test": "grunt test"
Expand All @@ -41,8 +41,7 @@
"plist": "~0.4.0",
"q": "^1.0.1",
"restify": "~1.4.4",
"underscore": "~1.4.0",
"winston": "~0.6.2"
"underscore": "~1.4.0"
},
"keywords": [
"browsers",
Expand Down

0 comments on commit 9c7de4f

Please sign in to comment.