wsl-utils 0.1.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 +60 -0
- package/index.js +45 -5
- package/package.json +7 -6
- package/readme.md +61 -0
package/index.d.ts
CHANGED
|
@@ -15,7 +15,67 @@ 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
|
*/
|
|
21
58
|
export function wslDrivesMountPoint(): Promise<string>;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
Convert a WSL Linux path to a Windows-accessible path.
|
|
62
|
+
|
|
63
|
+
URLs (strings starting with a protocol like `https://`) are returned unchanged.
|
|
64
|
+
|
|
65
|
+
@param path - The WSL path to convert (e.g., `/home/user/file.html`).
|
|
66
|
+
@returns The Windows-accessible path (e.g., `\\wsl.localhost\Ubuntu\home\user\file.html`) or the original path if conversion fails.
|
|
67
|
+
|
|
68
|
+
@example
|
|
69
|
+
```
|
|
70
|
+
import {convertWslPathToWindows} from 'wsl-utils';
|
|
71
|
+
|
|
72
|
+
// Convert a Linux path
|
|
73
|
+
const windowsPath = await convertWslPathToWindows('/home/user/file.html');
|
|
74
|
+
//=> '\\wsl.localhost\Ubuntu\home\user\file.html'
|
|
75
|
+
|
|
76
|
+
// URLs are not converted
|
|
77
|
+
const url = await convertWslPathToWindows('https://example.com');
|
|
78
|
+
//=> 'https://example.com'
|
|
79
|
+
```
|
|
80
|
+
*/
|
|
81
|
+
export function convertWslPathToWindows(path: string): Promise<string>;
|
package/index.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {promisify} from 'node:util';
|
|
2
|
+
import childProcess from 'node:child_process';
|
|
2
3
|
import fs, {constants as fsConstants} from 'node:fs/promises';
|
|
3
4
|
import isWsl from 'is-wsl';
|
|
5
|
+
import {powerShellPath as windowsPowerShellPath, executePowerShell} from 'powershell-utils';
|
|
6
|
+
|
|
7
|
+
const execFile = promisify(childProcess.execFile);
|
|
4
8
|
|
|
5
9
|
export const wslDrivesMountPoint = (() => {
|
|
6
10
|
// Default value for "root" param
|
|
@@ -46,12 +50,48 @@ export const powerShellPathFromWsl = async () => {
|
|
|
46
50
|
return `${mountPoint}c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe`;
|
|
47
51
|
};
|
|
48
52
|
|
|
49
|
-
export const powerShellPath =
|
|
50
|
-
|
|
51
|
-
|
|
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});
|
|
78
|
+
|
|
79
|
+
return stdout.trim();
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export const convertWslPathToWindows = async path => {
|
|
83
|
+
// Don't convert URLs
|
|
84
|
+
if (/^[a-z]+:\/\//i.test(path)) {
|
|
85
|
+
return path;
|
|
52
86
|
}
|
|
53
87
|
|
|
54
|
-
|
|
88
|
+
try {
|
|
89
|
+
const {stdout} = await execFile('wslpath', ['-aw', path], {encoding: 'utf8'});
|
|
90
|
+
return stdout.trim();
|
|
91
|
+
} catch {
|
|
92
|
+
// If wslpath fails, return the original path
|
|
93
|
+
return path;
|
|
94
|
+
}
|
|
55
95
|
};
|
|
56
96
|
|
|
57
97
|
export {default as isWsl} from 'is-wsl';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wsl-utils",
|
|
3
|
-
"version": "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": ">=
|
|
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.
|
|
43
|
-
"typescript": "^5.
|
|
44
|
-
"xo": "^1.
|
|
43
|
+
"ava": "^6.4.1",
|
|
44
|
+
"typescript": "^5.9.3",
|
|
45
|
+
"xo": "^1.2.3"
|
|
45
46
|
}
|
|
46
47
|
}
|
package/readme.md
CHANGED
|
@@ -43,8 +43,69 @@ 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>`
|
|
49
84
|
|
|
50
85
|
Get the mount point for fixed drives in WSL.
|
|
86
|
+
|
|
87
|
+
### convertWslPathToWindows(path)
|
|
88
|
+
|
|
89
|
+
Returns: `Promise<string>`
|
|
90
|
+
|
|
91
|
+
Convert a WSL Linux path to a Windows-accessible path.
|
|
92
|
+
|
|
93
|
+
URLs (strings starting with a protocol like `https://`) are returned unchanged.
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
import {convertWslPathToWindows} from 'wsl-utils';
|
|
97
|
+
|
|
98
|
+
// Convert a Linux path
|
|
99
|
+
const windowsPath = await convertWslPathToWindows('/home/user/file.html');
|
|
100
|
+
//=> '\\wsl.localhost\Ubuntu\home\user\file.html'
|
|
101
|
+
|
|
102
|
+
// URLs are not converted
|
|
103
|
+
const url = await convertWslPathToWindows('https://example.com');
|
|
104
|
+
//=> 'https://example.com'
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
#### path
|
|
108
|
+
|
|
109
|
+
Type: `string`
|
|
110
|
+
|
|
111
|
+
The WSL path to convert (e.g., `/home/user/file.html`).
|