wsl-utils 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -15,6 +15,43 @@ Returns WSL path if in WSL, otherwise returns Windows path.
15
15
  */
16
16
  export function powerShellPath(): Promise<string>;
17
17
 
18
+ /**
19
+ Check if PowerShell is accessible in the current environment.
20
+
21
+ This is useful to determine whether Windows integration features can be used. In sandboxed WSL environments or systems where PowerShell is not accessible, this will return `false`.
22
+
23
+ @returns A promise that resolves to `true` if PowerShell is accessible, `false` otherwise.
24
+
25
+ @example
26
+ ```
27
+ import {canAccessPowerShell} from 'wsl-utils';
28
+
29
+ if (await canAccessPowerShell()) {
30
+ // Use Windows integration features
31
+ console.log('PowerShell is accessible');
32
+ } else {
33
+ // Fall back to Linux-native behavior
34
+ console.log('PowerShell is not accessible');
35
+ }
36
+ ```
37
+ */
38
+ export function canAccessPowerShell(): Promise<boolean>;
39
+
40
+ /**
41
+ Get the default browser in WSL.
42
+
43
+ @returns A promise that resolves to the [ProgID](https://setuserfta.com/guide-to-understanding-progids-and-file-type-associations/) of the default browser (e.g., `'ChromeHTML'`, `'FirefoxURL'`).
44
+
45
+ @example
46
+ ```
47
+ import {wslDefaultBrowser} from 'wsl-utils';
48
+
49
+ const progId = await wslDefaultBrowser();
50
+ //=> 'ChromeHTML'
51
+ ```
52
+ */
53
+ export function wslDefaultBrowser(): Promise<string>;
54
+
18
55
  /**
19
56
  Get the mount point for fixed drives in WSL.
20
57
  */
package/index.js CHANGED
@@ -1,8 +1,8 @@
1
- import process from 'node:process';
2
1
  import {promisify} from 'node:util';
3
2
  import childProcess from 'node:child_process';
4
3
  import fs, {constants as fsConstants} from 'node:fs/promises';
5
4
  import isWsl from 'is-wsl';
5
+ import {powerShellPath as windowsPowerShellPath, executePowerShell} from 'powershell-utils';
6
6
 
7
7
  const execFile = promisify(childProcess.execFile);
8
8
 
@@ -50,12 +50,33 @@ export const powerShellPathFromWsl = async () => {
50
50
  return `${mountPoint}c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe`;
51
51
  };
52
52
 
53
- export const powerShellPath = async () => {
54
- if (isWsl) {
55
- return powerShellPathFromWsl();
56
- }
53
+ export const powerShellPath = isWsl ? powerShellPathFromWsl : windowsPowerShellPath;
54
+
55
+ // Cache for PowerShell accessibility check
56
+ let canAccessPowerShellPromise;
57
+
58
+ export const canAccessPowerShell = async () => {
59
+ canAccessPowerShellPromise ??= (async () => {
60
+ try {
61
+ const psPath = await powerShellPath();
62
+ await fs.access(psPath, fsConstants.X_OK);
63
+ return true;
64
+ } catch {
65
+ // PowerShell is not accessible (either doesn't exist, no execute permission, or other error)
66
+ return false;
67
+ }
68
+ })();
69
+
70
+ return canAccessPowerShellPromise;
71
+ };
72
+
73
+ export const wslDefaultBrowser = async () => {
74
+ const psPath = await powerShellPath();
75
+ const command = String.raw`(Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice").ProgId`;
76
+
77
+ const {stdout} = await executePowerShell(command, {powerShellPath: psPath});
57
78
 
58
- return `${process.env.SYSTEMROOT || process.env.windir || String.raw`C:\Windows`}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;
79
+ return stdout.trim();
59
80
  };
60
81
 
61
82
  export const convertWslPathToWindows = async path => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wsl-utils",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Utilities for working with Windows Subsystem for Linux (WSL)",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/wsl-utils",
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "sideEffects": false,
19
19
  "engines": {
20
- "node": ">=18"
20
+ "node": ">=20"
21
21
  },
22
22
  "scripts": {
23
23
  "test": "xo && ava && tsc index.d.ts --skipLibCheck"
@@ -36,11 +36,12 @@
36
36
  "utilities"
37
37
  ],
38
38
  "dependencies": {
39
- "is-wsl": "^3.1.0"
39
+ "is-wsl": "^3.1.0",
40
+ "powershell-utils": "^0.1.0"
40
41
  },
41
42
  "devDependencies": {
42
- "ava": "^6.3.0",
43
- "typescript": "^5.8.3",
44
- "xo": "^1.0.0"
43
+ "ava": "^6.4.1",
44
+ "typescript": "^5.9.3",
45
+ "xo": "^1.2.3"
45
46
  }
46
47
  }
package/readme.md CHANGED
@@ -43,6 +43,41 @@ Get the PowerShell executable path for the current environment.
43
43
 
44
44
  Returns WSL path if in WSL, otherwise returns Windows path.
45
45
 
46
+ ### canAccessPowerShell()
47
+
48
+ Returns: `Promise<boolean>`
49
+
50
+ Check if PowerShell is accessible in the current environment.
51
+
52
+ This is useful to determine whether Windows integration features can be used. In sandboxed WSL environments or systems where PowerShell is not accessible, this will return `false`.
53
+
54
+ ```js
55
+ import {canAccessPowerShell} from 'wsl-utils';
56
+
57
+ if (await canAccessPowerShell()) {
58
+ // Use Windows integration features
59
+ console.log('PowerShell is accessible');
60
+ } else {
61
+ // Fall back to Linux-native behavior
62
+ console.log('PowerShell is not accessible');
63
+ }
64
+ ```
65
+
66
+ ### wslDefaultBrowser()
67
+
68
+ Returns: `Promise<string>`
69
+
70
+ Get the default browser in WSL.
71
+
72
+ Returns a promise that resolves to the [ProgID](https://setuserfta.com/guide-to-understanding-progids-and-file-type-associations/) of the default browser (e.g., `'ChromeHTML'`, `'FirefoxURL'`).
73
+
74
+ ```js
75
+ import {wslDefaultBrowser} from 'wsl-utils';
76
+
77
+ const progId = await wslDefaultBrowser();
78
+ //=> 'ChromeHTML'
79
+ ```
80
+
46
81
  ### wslDrivesMountPoint()
47
82
 
48
83
  Returns: `Promise<string>`