From 78fcc9ec8866d506eebffb5899973e5cdeb185cb Mon Sep 17 00:00:00 2001 From: Dean Sheather Date: Sat, 29 Jun 2019 14:28:47 +0000 Subject: [PATCH] Add executable check for local xdg-open (#140) --- index.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index d791638..feea5b5 100644 --- a/index.js +++ b/index.js @@ -2,12 +2,17 @@ const {promisify} = require('util'); const path = require('path'); const childProcess = require('child_process'); +const fs = require('fs'); const isWsl = require('is-wsl'); +const pAccess = promisify(fs.access); const pExecFile = promisify(childProcess.execFile); +// Path to included `xdg-open` +const localXdgOpenPath = path.join(__dirname, 'xdg-open'); + // Convert a path from WSL format to Windows format: -// `/mnt/c/Program Files/Example/MyApp.exe` → `C:\Program Files\Example\MyApp.exe`` +// `/mnt/c/Program Files/Example/MyApp.exe` → `C:\Program Files\Example\MyApp.exe` const wslToWindowsPath = async path => { const {stdout} = await pExecFile('wslpath', ['-w', path]); return stdout.trim(); @@ -76,8 +81,16 @@ module.exports = async (target, options) => { // When bundled by Webpack, there's no actual package file path and no local `xdg-open`. const isBundled = !__dirname || __dirname === '/'; - const useSystemXdgOpen = process.versions.electron || process.platform === 'android' || isBundled; - command = useSystemXdgOpen ? 'xdg-open' : path.join(__dirname, 'xdg-open'); + // Check if local `xdg-open` exists and is executable. + let exeLocalXdgOpen = false; + try { + await pAccess(localXdgOpenPath, fs.constants.X_OK); + exeLocalXdgOpen = true; + } catch (error) {} + + const useSystemXdgOpen = process.versions.electron || + process.platform === 'android' || isBundled || !exeLocalXdgOpen; + command = useSystemXdgOpen ? 'xdg-open' : localXdgOpenPath; } if (appArguments.length > 0) {