index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import process from 'node:process';
  2. import fs, {constants as fsConstants} from 'node:fs/promises';
  3. import isWsl from 'is-wsl';
  4. export const wslDrivesMountPoint = (() => {
  5. // Default value for "root" param
  6. // according to https://docs.microsoft.com/en-us/windows/wsl/wsl-config
  7. const defaultMountPoint = '/mnt/';
  8. let mountPoint;
  9. return async function () {
  10. if (mountPoint) {
  11. // Return memoized mount point value
  12. return mountPoint;
  13. }
  14. const configFilePath = '/etc/wsl.conf';
  15. let isConfigFileExists = false;
  16. try {
  17. await fs.access(configFilePath, fsConstants.F_OK);
  18. isConfigFileExists = true;
  19. } catch {}
  20. if (!isConfigFileExists) {
  21. return defaultMountPoint;
  22. }
  23. const configContent = await fs.readFile(configFilePath, {encoding: 'utf8'});
  24. const configMountPoint = /(?<!#.*)root\s*=\s*(?<mountPoint>.*)/g.exec(configContent);
  25. if (!configMountPoint) {
  26. return defaultMountPoint;
  27. }
  28. mountPoint = configMountPoint.groups.mountPoint.trim();
  29. mountPoint = mountPoint.endsWith('/') ? mountPoint : `${mountPoint}/`;
  30. return mountPoint;
  31. };
  32. })();
  33. export const powerShellPathFromWsl = async () => {
  34. const mountPoint = await wslDrivesMountPoint();
  35. return `${mountPoint}c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe`;
  36. };
  37. export const powerShellPath = async () => {
  38. if (isWsl) {
  39. return powerShellPathFromWsl();
  40. }
  41. return `${process.env.SYSTEMROOT || process.env.windir || String.raw`C:\Windows`}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;
  42. };
  43. export {default as isWsl} from 'is-wsl';