Skip to content

Commit

Permalink
Add compabitility with WSL
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsputman committed Jul 27, 2018
1 parent 51a4058 commit 788437a
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions opener.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -20,15 +33,15 @@ 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 {
command = options.command;
}
}

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.
Expand Down

0 comments on commit 788437a

Please sign in to comment.