diff --git a/opener.js b/opener.js index 8951fa2..5310d68 100644 --- a/opener.js +++ b/opener.js @@ -3,11 +3,24 @@ "use strict"; var childProcess = require("child_process"); +var os = require('os'); function opener(args, options, callback) { + + var platform = process.platform; + + // Attempt to detect Windows Subystem for Linux (WSL). + // Identifies itself as Linux (which works in most cases), but in this specific case we need to treat it as actually + // being Windows. The "Windows-way" of opening a browser through cmd.exe works just fine here. + if(platform === "linux" && os.release().indexOf("Microsoft") !== -1){ + + platform = "win32"; + } + // http://stackoverflow.com/q/1480971/3191, but see below for Windows. - var command = process.platform === "win32" ? "cmd" : - process.platform === "darwin" ? "open" : + // For proper functioning on WSL (see above) the Windows command needs to be "cmd.exe" (not merely "cmd"). + var command = platform === "win32" ? "cmd.exe" : + platform === "darwin" ? "open" : "xdg-open"; if (typeof args === "string") { @@ -20,7 +33,7 @@ function opener(args, options, callback) { } if (options && typeof options === "object" && options.command) { - if (process.platform === "win32") { + if (platform === "win32") { // *always* use cmd on windows args = [options.command].concat(args); } else { @@ -28,7 +41,7 @@ function opener(args, options, callback) { } } - if (process.platform === "win32") { + if (platform === "win32") { // On Windows, we really want to use the "start" command. But, the rules regarding arguments with spaces, and // escaping them with quotes, can get really arcane. So the easiest way to deal with this is to pass off the // responsibility to "cmd /c", which has that logic built in.