Skip to content

Commit

Permalink
feat: Add support for running Windows Firefox from WSL
Browse files Browse the repository at this point in the history
I'm assuming everyone who runs WSL wants to run the Windows version of Firefox (and not try to actually run the Linux version under WSL).
  • Loading branch information
birtles authored Jul 17, 2019
1 parent 2443631 commit b4e260e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
69 changes: 63 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var fs = require('fs')
var path = require('path')
var isWsl = require('is-wsl')
var { execSync } = require('child_process')

var PREFS = [
'user_pref("browser.shell.checkDefaultBrowser", false);',
Expand Down Expand Up @@ -66,6 +68,59 @@ var getFirefoxExe = function (firefoxDirName) {
return path.join('C:\\Program Files', firefoxDirNames[0], 'firefox.exe')
}

var getAllPrefixesWsl = function () {
var drives = []
// Some folks configure their wsl.conf to mount Windows drives without the
// /mnt prefix (e.g. see https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly)
//
// In fact, they could configure this to be any number of things. So we
// take each path, convert it to a Windows path, check if it looks like
// it starts with a drive and then record that.
var re = /^([A-Z]):\\/i
for (var pathElem of process.env.PATH.split(':')) {
var windowsPath = execSync('wslpath -w "' + pathElem + '"').toString()
var matches = windowsPath.match(re)
if (matches !== null && drives.indexOf(matches[1]) === -1) {
drives.push(matches[1])
}
}

var result = []
// We don't have the PROGRAMFILES or PROGRAMFILES(X86) environment variables
// in WSL so we just hard code them.
var prefixes = ['Program Files', 'Program Files (x86)']
for (var prefix of prefixes) {
for (var drive of drives) {
// We only have the drive, and only wslpath knows exactly what they map to
// in Linux, so we convert it back here.
var wslPath =
execSync('wslpath "' + drive + ':\\' + prefix + '"').toString().trim()
result.push(wslPath)
}
}

return result
}

var getFirefoxExeWsl = function (firefoxDirName) {
if (!isWsl) {
return null
}

var firefoxDirNames = Array.prototype.slice.call(arguments)

for (var prefix of getAllPrefixesWsl()) {
for (var dir of firefoxDirNames) {
var candidate = path.join(prefix, dir, 'firefox.exe')
if (fs.existsSync(candidate)) {
return candidate
}
}
}

return path.join('/mnt/c/Program Files/', firefoxDirNames[0], 'firefox.exe')
}

var getFirefoxWithFallbackOnOSX = function () {
if (process.platform !== 'darwin') {
return null
Expand Down Expand Up @@ -144,10 +199,12 @@ var FirefoxBrowser = function (id, baseBrowserDecorator, args) {
})
}

fs.writeFileSync(profilePath + '/prefs.js', this._getPrefs(args.prefs))
fs.writeFileSync(path.join(profilePath, 'prefs.js'), this._getPrefs(args.prefs))
var translatedProfilePath =
isWsl ? execSync('wslpath -w ' + profilePath).toString().trim() : profilePath
self._execCommand(
command,
[url, '-profile', profilePath, '-no-remote', '-wait-for-browser'].concat(flags)
[url, '-profile', translatedProfilePath, '-no-remote', '-wait-for-browser'].concat(flags)
)
}
}
Expand All @@ -156,7 +213,7 @@ FirefoxBrowser.prototype = {
name: 'Firefox',

DEFAULT_CMD: {
linux: 'firefox',
linux: isWsl ? getFirefoxExeWsl('Mozilla Firefox') : 'firefox',
freebsd: 'firefox',
darwin: getFirefoxWithFallbackOnOSX('Firefox'),
win32: getFirefoxExe('Mozilla Firefox')
Expand All @@ -175,7 +232,7 @@ var FirefoxDeveloperBrowser = function () {
FirefoxDeveloperBrowser.prototype = {
name: 'FirefoxDeveloper',
DEFAULT_CMD: {
linux: 'firefox',
linux: isWsl ? getFirefoxExeWsl('Firefox Developer Edition') : 'firefox',
darwin: getFirefoxWithFallbackOnOSX('FirefoxDeveloperEdition', 'FirefoxAurora'),
win32: getFirefoxExe('Firefox Developer Edition')
},
Expand All @@ -193,7 +250,7 @@ var FirefoxAuroraBrowser = function () {
FirefoxAuroraBrowser.prototype = {
name: 'FirefoxAurora',
DEFAULT_CMD: {
linux: 'firefox',
linux: isWsl ? getFirefoxExeWsl('Aurora') : 'firefox',
darwin: getFirefoxWithFallbackOnOSX('FirefoxAurora'),
win32: getFirefoxExe('Aurora')
},
Expand All @@ -212,7 +269,7 @@ FirefoxNightlyBrowser.prototype = {
name: 'FirefoxNightly',

DEFAULT_CMD: {
linux: 'firefox',
linux: isWsl ? getFirefoxExeWsl('Nightly', 'Firefox Nightly') : 'firefox',
darwin: getFirefoxWithFallbackOnOSX('FirefoxNightly', 'Firefox Nightly'),
win32: getFirefoxExe('Nightly', 'Firefox Nightly')
},
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@
"Schaaf, Martin <[email protected]>",
"Žilvinas Urbonas <[email protected]>"
],
"dependencies": {}
"dependencies": {
"is-wsl": "^2.1.0"
}
}

0 comments on commit b4e260e

Please sign in to comment.