sindresorhus / open

Open stuff like URLs, files, executables. Cross-platform.
MIT License
3.18k stars 218 forks source link

I cann't understand. Use powershell in wsl to open the default? #270

Open bigbigDreamer opened 2 years ago

bigbigDreamer commented 2 years ago

https://github.com/sindresorhus/open/blob/7579417bbd3b3fc155b10f9b9b2eb71381e13e9a/index.js#L123

command = isWsl ?
   `${mountPoint}c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe` :
   `${process.env.SYSTEMROOT}\\System32\\WindowsPowerShell\\v1.0\\powershell`;

Now I modified the source file in node modules and used xdg-open can work normal.

Delete || (isWsl && !isDocker()), because wsl also is linux plateform.

But, Note: You should give your current wsl system login user root privileges.

269

bigbigDreamer commented 2 years ago

In particular, you can install xdg-open in your wsl in advance.

$ sudo apt-get update 

$ sudo apt-get install xdg-open

Then, xdg-open can automatically open the default browser on your host machine, provided that you have sufficient permissions.

you can try edit the source code in open from node_modules:

if (app) {
    command = app;
} else {
    // When bundled by Webpack, there's no actual package file path and no local `xdg-open`.
    const isBundled = !__dirname || __dirname === '/';

    // Check if local `xdg-open` exists and is executable.
    let exeLocalXdgOpen = false;
    try {
            await fs.access(localXdgOpenPath, fsConstants.X_OK);
            exeLocalXdgOpen = true;
    } catch {}

        // const useSystemXdgOpen = process.versions.electron ||
        //  platform === 'android' || isBundled || !exeLocalXdgOpen;
        // command = useSystemXdgOpen ? 'xdg-open' : localXdgOpenPath;
        command =  'xdg-open';
}

Finally,u can use as follow:

const open = require('open');

open(url, { wait: true })

Don't add app param!

mklueh commented 2 years ago

In my case, I have given WSL restricted access to the host machine intentionally to work as a sandbox. It still tries to access Powershell. What should I do?