Skip to content

Commit

Permalink
Add executable check for local xdg-open (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
deansheather authored and sindresorhus committed Jun 29, 2019
1 parent 11b0426 commit 78fcc9e
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions index.js
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 78fcc9e

Please sign in to comment.