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

WSL: Get drives mount point from wsl.conf #219

Merged
36 changes: 35 additions & 1 deletion index.js
Expand Up @@ -11,6 +11,38 @@ const pAccess = promisify(fs.access);
// Path to included `xdg-open`.
const localXdgOpenPath = path.join(__dirname, 'xdg-open');

/**
Get the mount point for fixed drives in WSL.

@inner
@returns {string} The mount point.
*/
const getWslDrivesMountPoint = (() => {
kuzalekon marked this conversation as resolved.
Show resolved Hide resolved
let mountPoint = null;
kuzalekon marked this conversation as resolved.
Show resolved Hide resolved

return function () {
if (mountPoint) {
// Return memoized mount point value
return mountPoint;
}

const confFile = '/etc/wsl.conf';
kuzalekon marked this conversation as resolved.
Show resolved Hide resolved

if (!fs.existsSync(confFile)) {
// Default value for "root" param
// according to https://docs.microsoft.com/en-us/windows/wsl/wsl-config
return '/mnt/';
}

const confContent = fs.readFileSync(confFile, {encoding: 'utf-8'});
kuzalekon marked this conversation as resolved.
Show resolved Hide resolved

mountPoint = (/root\s*=\s*(.*)/g.exec(confContent)[1] || '').trim();
mountPoint = mountPoint.endsWith('/') ? mountPoint : mountPoint.concat('/');
kuzalekon marked this conversation as resolved.
Show resolved Hide resolved

return mountPoint;
};
})();

module.exports = async (target, options) => {
if (typeof target !== 'string') {
throw new TypeError('Expected a `target`');
Expand Down Expand Up @@ -49,8 +81,10 @@ module.exports = async (target, options) => {
cliArguments.push('-a', app);
}
} else if (process.platform === 'win32' || (isWsl && !isDocker())) {
const mountPoint = getWslDrivesMountPoint();

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

cliArguments.push(
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -15,7 +15,7 @@ Note: The original [`open` package](https://github.com/pwnall/node-open) was pre
- Safer as it uses `spawn` instead of `exec`.
- Fixes most of the open original `node-open` issues.
- Includes the latest [`xdg-open` script](https://cgit.freedesktop.org/xdg/xdg-utils/commit/?id=c55122295c2a480fa721a9614f0e2d42b2949c18) for Linux.
- Supports WSL paths to Windows apps under `/mnt/*`.
- Supports WSL paths to Windows apps.

## Install

Expand Down Expand Up @@ -108,7 +108,7 @@ Type: `boolean`\
Default: `false`

Allow the opened app to exit with nonzero exit code when the `wait` option is `true`.

We do not recommend setting this option. The convention for success is exit code zero.

## Caveats
Expand Down