Skip to content

Commit

Permalink
Support WSL configuration where Windows paths are not in PATH (#195)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Pytal and sindresorhus committed Sep 29, 2020
1 parent 45e50ca commit be0f794
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ const wslToWindowsPath = async path => {
return stdout.trim();
};

// Convert a path from Windows format to WSL format
const windowsToWslPath = async path => {
const {stdout} = await pExecFile('wslpath', [path]);
return stdout.trim();
};

// Get an environment variable from Windows
const wslGetWindowsEnvVar = async envVar => {
const {stdout} = await pExecFile('wslvar', [envVar]);
return stdout.trim();
};

module.exports = async (target, options) => {
if (typeof target !== 'string') {
throw new TypeError('Expected a `target`');
Expand Down Expand Up @@ -57,7 +69,8 @@ module.exports = async (target, options) => {
cliArguments.push('-a', app);
}
} else if (process.platform === 'win32' || (isWsl && !isDocker())) {
command = 'powershell' + (isWsl ? '.exe' : '');
const windowsRoot = isWsl ? await wslGetWindowsEnvVar('systemroot') : process.env.SYSTEMROOT;
command = String.raw`${windowsRoot}\System32\WindowsPowerShell\v1.0\powershell${isWsl ? '.exe' : ''}`;
cliArguments.push(
'-NoProfile',
'-NonInteractive',
Expand All @@ -66,7 +79,9 @@ module.exports = async (target, options) => {
'-EncodedCommand'
);

if (!isWsl) {
if (isWsl) {
command = await windowsToWslPath(command);
} else {
childProcessOptions.windowsVerbatimArguments = true;
}

Expand Down

0 comments on commit be0f794

Please sign in to comment.