Skip to content

Commit 36e9964

Browse files
kuzalekonsindresorhus
andauthoredFeb 7, 2021
WSL: Get drives mount point from wsl.conf (#219)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 5ce319c commit 36e9964

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed
 

‎index.js

+42-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,49 @@ const isWsl = require('is-wsl');
77
const isDocker = require('is-docker');
88

99
const pAccess = promisify(fs.access);
10+
const pReadFile = promisify(fs.readFile);
1011

1112
// Path to included `xdg-open`.
1213
const localXdgOpenPath = path.join(__dirname, 'xdg-open');
1314

15+
/**
16+
Get the mount point for fixed drives in WSL.
17+
18+
@inner
19+
@returns {string} The mount point.
20+
*/
21+
const getWslDrivesMountPoint = (() => {
22+
let mountPoint;
23+
24+
return async function () {
25+
if (mountPoint) {
26+
// Return memoized mount point value
27+
return mountPoint;
28+
}
29+
30+
const configFilePath = '/etc/wsl.conf';
31+
32+
let isConfigFileExists = false;
33+
try {
34+
await pAccess(configFilePath, fs.constants.F_OK);
35+
isConfigFileExists = true;
36+
} catch (_) {}
37+
38+
if (!isConfigFileExists) {
39+
// Default value for "root" param
40+
// according to https://docs.microsoft.com/en-us/windows/wsl/wsl-config
41+
return '/mnt/';
42+
}
43+
44+
const configContent = await pReadFile(configFilePath, {encoding: 'utf8'});
45+
46+
mountPoint = (/root\s*=\s*(.*)/g.exec(configContent)[1] || '').trim();
47+
mountPoint = mountPoint.endsWith('/') ? mountPoint : mountPoint + '/';
48+
49+
return mountPoint;
50+
};
51+
})();
52+
1453
module.exports = async (target, options) => {
1554
if (typeof target !== 'string') {
1655
throw new TypeError('Expected a `target`');
@@ -49,8 +88,10 @@ module.exports = async (target, options) => {
4988
cliArguments.push('-a', app);
5089
}
5190
} else if (process.platform === 'win32' || (isWsl && !isDocker())) {
91+
const mountPoint = await getWslDrivesMountPoint();
92+
5293
command = isWsl ?
53-
'/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe' :
94+
`${mountPoint}c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe` :
5495
`${process.env.SYSTEMROOT}\\System32\\WindowsPowerShell\\v1.0\\powershell`;
5596

5697
cliArguments.push(

‎readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Note: The original [`open` package](https://github.com/pwnall/node-open) was pre
1515
- Safer as it uses `spawn` instead of `exec`.
1616
- Fixes most of the open original `node-open` issues.
1717
- Includes the latest [`xdg-open` script](https://cgit.freedesktop.org/xdg/xdg-utils/commit/?id=c55122295c2a480fa721a9614f0e2d42b2949c18) for Linux.
18-
- Supports WSL paths to Windows apps under `/mnt/*`.
18+
- Supports WSL paths to Windows apps.
1919

2020
## Install
2121

@@ -108,7 +108,7 @@ Type: `boolean`\
108108
Default: `false`
109109

110110
Allow the opened app to exit with nonzero exit code when the `wait` option is `true`.
111-
111+
112112
We do not recommend setting this option. The convention for success is exit code zero.
113113

114114
## Caveats

0 commit comments

Comments
 (0)
Please sign in to comment.