Skip to content

Commit be0f794

Browse files
Pytalsindresorhus
andauthoredSep 29, 2020
Support WSL configuration where Windows paths are not in PATH (#195)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 45e50ca commit be0f794

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed
 

‎index.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ const wslToWindowsPath = async path => {
1919
return stdout.trim();
2020
};
2121

22+
// Convert a path from Windows format to WSL format
23+
const windowsToWslPath = async path => {
24+
const {stdout} = await pExecFile('wslpath', [path]);
25+
return stdout.trim();
26+
};
27+
28+
// Get an environment variable from Windows
29+
const wslGetWindowsEnvVar = async envVar => {
30+
const {stdout} = await pExecFile('wslvar', [envVar]);
31+
return stdout.trim();
32+
};
33+
2234
module.exports = async (target, options) => {
2335
if (typeof target !== 'string') {
2436
throw new TypeError('Expected a `target`');
@@ -57,7 +69,8 @@ module.exports = async (target, options) => {
5769
cliArguments.push('-a', app);
5870
}
5971
} else if (process.platform === 'win32' || (isWsl && !isDocker())) {
60-
command = 'powershell' + (isWsl ? '.exe' : '');
72+
const windowsRoot = isWsl ? await wslGetWindowsEnvVar('systemroot') : process.env.SYSTEMROOT;
73+
command = String.raw`${windowsRoot}\System32\WindowsPowerShell\v1.0\powershell${isWsl ? '.exe' : ''}`;
6174
cliArguments.push(
6275
'-NoProfile',
6376
'-NonInteractive',
@@ -66,7 +79,9 @@ module.exports = async (target, options) => {
6679
'-EncodedCommand'
6780
);
6881

69-
if (!isWsl) {
82+
if (isWsl) {
83+
command = await windowsToWslPath(command);
84+
} else {
7085
childProcessOptions.windowsVerbatimArguments = true;
7186
}
7287

0 commit comments

Comments
 (0)
Please sign in to comment.