Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error invalid arg type on WSL systems on url open #118

Closed
38 changes: 32 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
}

const options = { ...opts, shell: false }
const realArgs = []
let realArgs = []
let script = cmd

// first, determine if we're in windows because if we are we need to know if we're
Expand Down Expand Up @@ -114,6 +114,12 @@
}
realArgs.push('/d', '/s', '/c', script)
options.windowsVerbatimArguments = true
} else if (opts.isWSL) {
// handling for WSL
realArgs = ['-COmmand', 'Start-Process']
args.forEach(arg => {
realArgs.push(escapeWindowsArg(arg))
Fixed Show fixed Hide fixed
})
} else {
for (const arg of args) {
script += ` ${escape.sh(arg)}`
Expand All @@ -126,16 +132,22 @@

// open a file with the default application as defined by the user's OS
const open = (_args, opts = {}, extra = {}) => {
const options = { ...opts, shell: true }
const args = [].concat(_args)
let args = [].concat(_args)

let platform = process.platform
let isWSL = false
// process.platform === 'linux' may actually indicate WSL, if that's the case
// we want to treat things as win32 anyway so the host can open the argument
if (platform === 'linux' && os.release().toLowerCase().includes('microsoft')) {
platform = 'win32'

if (platform === 'linux') {
const osRelease = os.release().toLowerCase()
if (osRelease.includes('microsoft') || osRelease.includes('wsl')) {
platform = 'win32'
isWSL = true
}
}

const options = { ...opts, shell: true, isWSL }
let command = options.command
if (!command) {
if (platform === 'win32') {
Expand All @@ -145,7 +157,14 @@
// also, the start command accepts a title so to make sure that we don't
// accidentally interpret the first arg as the title, we stick an empty
// string immediately after the start command
command = 'start ""'
if (isWSL) {
// For WSL, use wslpath to convert the path if necessary
command = 'powershell.exe'
args = args.map(arg => `$(wslpath -w '${escapeWindowsArg(arg)}')`)
args.unshift('Start-Process')
} else {
command = 'start ""'
}
} else if (platform === 'darwin') {
command = 'open'
} else {
Expand Down Expand Up @@ -203,4 +222,11 @@
}
}

const escapeWindowsArg = (arg) => {
if (typeof arg !== 'string') {
return arg
}
return `${arg.replace(/"/g, '""')}"`
}

module.exports = promiseSpawn
Loading