Skip to content

Commit

Permalink
fix: handle Chrome location on Windows
Browse files Browse the repository at this point in the history
Currently the task assumes Chrome is located under process.env.LOCALAPPDATA
directory. This may not be as Chrome can be installed globally, to
process.env["PROGRAMFILES(X86)"]. Should Chrome be released for Windows in a 64-bit
flavor, it can be located under process.env.PROGRAMFILES. This commit adds fallbacks
to those paths, being forward-compatible in case 64-bit Chrome gets released for Windows.
  • Loading branch information
mgol committed Nov 23, 2013
1 parent 9ebd997 commit 62df301
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var os = require('os'),
fs = require('fs');
var fs = require('fs');

var ChromeBrowser = function(baseBrowserDecorator, args) {
baseBrowserDecorator(this);
Expand All @@ -19,13 +18,33 @@ var ChromeBrowser = function(baseBrowserDecorator, args) {
};
};

// Return location of chrome.exe file for a given Chrome directory (available: "Chrome", "Chrome SxS").
function getChromeExe(chromeDirName) {
if (process.platform !== 'win32') {
return null;
}
var windowsChromeDirectory, i, prefix;
var suffix = '\\Google\\'+ chromeDirName + '\\Application\\chrome.exe';
var prefixes = [process.env.LOCALAPPDATA, process.env.PROGRAMFILES, process.env['PROGRAMFILES(X86)']];

for (i = 0; i < prefixes.length; i++) {
prefix = prefixes[i];
if (fs.existsSync(prefix + suffix)) {
windowsChromeDirectory = prefix + suffix;
break;
}
}

return windowsChromeDirectory;
}

ChromeBrowser.prototype = {
name: 'Chrome',

DEFAULT_CMD: {
linux: 'google-chrome',
darwin: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
win32: windowsChromePath('\\Google\\Chrome\\Application\\chrome.exe')
win32: getChromeExe('Chrome')
},
ENV_CMD: 'CHROME_BIN'
};
Expand All @@ -49,26 +68,13 @@ ChromeCanaryBrowser.prototype = {
DEFAULT_CMD: {
linux: 'google-chrome-canary',
darwin: '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
win32: windowsChromePath('\\Google\\Chrome SxS\\Application\\chrome.exe')
win32: getChromeExe('Chrome SxS')
},
ENV_CMD: 'CHROME_CANARY_BIN'
};

ChromeCanaryBrowser.$inject = ['baseBrowserDecorator', 'args'];

var windowsChromePath = function(chromeExe) {
if (os.platform() !== 'win32') {
return '';
}

var globalInstall = os.arch() === 'x64' ? process.env['ProgramFiles(x86)'] : process.env.ProgramFiles;

if (fs.existsSync(globalInstall + chromeExe)) {
return globalInstall + chromeExe;
}

return process.env.LOCALAPPDATA + chromeExe;
}

// PUBLISH DI MODULE
module.exports = {
Expand Down

0 comments on commit 62df301

Please sign in to comment.