Skip to content

Commit

Permalink
WSL: Get drives mount point from wsl.conf (#219)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
kuzalekon and sindresorhus committed Feb 7, 2021
1 parent 5ce319c commit 36e9964
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
43 changes: 42 additions & 1 deletion index.js
Expand Up @@ -7,10 +7,49 @@ const isWsl = require('is-wsl');
const isDocker = require('is-docker');

const pAccess = promisify(fs.access);
const pReadFile = promisify(fs.readFile);

// 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 = (() => {
let mountPoint;

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

const configFilePath = '/etc/wsl.conf';

let isConfigFileExists = false;
try {
await pAccess(configFilePath, fs.constants.F_OK);
isConfigFileExists = true;
} catch (_) {}

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

const configContent = await pReadFile(configFilePath, {encoding: 'utf8'});

mountPoint = (/root\s*=\s*(.*)/g.exec(configContent)[1] || '').trim();
mountPoint = mountPoint.endsWith('/') ? mountPoint : mountPoint + '/';

return mountPoint;
};
})();

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

0 comments on commit 36e9964

Please sign in to comment.