wsl-utils 0.1.0 → 0.2.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
@@ -19,3 +19,26 @@ export function powerShellPath(): Promise<string>;
19
19
  Get the mount point for fixed drives in WSL.
20
20
  */
21
21
  export function wslDrivesMountPoint(): Promise<string>;
22
+
23
+ /**
24
+ Convert a WSL Linux path to a Windows-accessible path.
25
+
26
+ URLs (strings starting with a protocol like `https://`) are returned unchanged.
27
+
28
+ @param path - The WSL path to convert (e.g., `/home/user/file.html`).
29
+ @returns The Windows-accessible path (e.g., `\\wsl.localhost\Ubuntu\home\user\file.html`) or the original path if conversion fails.
30
+
31
+ @example
32
+ ```
33
+ import {convertWslPathToWindows} from 'wsl-utils';
34
+
35
+ // Convert a Linux path
36
+ const windowsPath = await convertWslPathToWindows('/home/user/file.html');
37
+ //=> '\\wsl.localhost\Ubuntu\home\user\file.html'
38
+
39
+ // URLs are not converted
40
+ const url = await convertWslPathToWindows('https://example.com');
41
+ //=> 'https://example.com'
42
+ ```
43
+ */
44
+ export function convertWslPathToWindows(path: string): Promise<string>;
package/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  import process from 'node:process';
2
+ import {promisify} from 'node:util';
3
+ import childProcess from 'node:child_process';
2
4
  import fs, {constants as fsConstants} from 'node:fs/promises';
3
5
  import isWsl from 'is-wsl';
4
6
 
7
+ const execFile = promisify(childProcess.execFile);
8
+
5
9
  export const wslDrivesMountPoint = (() => {
6
10
  // Default value for "root" param
7
11
  // according to https://docs.microsoft.com/en-us/windows/wsl/wsl-config
@@ -54,4 +58,19 @@ export const powerShellPath = async () => {
54
58
  return `${process.env.SYSTEMROOT || process.env.windir || String.raw`C:\Windows`}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;
55
59
  };
56
60
 
61
+ export const convertWslPathToWindows = async path => {
62
+ // Don't convert URLs
63
+ if (/^[a-z]+:\/\//i.test(path)) {
64
+ return path;
65
+ }
66
+
67
+ try {
68
+ const {stdout} = await execFile('wslpath', ['-aw', path], {encoding: 'utf8'});
69
+ return stdout.trim();
70
+ } catch {
71
+ // If wslpath fails, return the original path
72
+ return path;
73
+ }
74
+ };
75
+
57
76
  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.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Utilities for working with Windows Subsystem for Linux (WSL)",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/wsl-utils",
package/readme.md CHANGED
@@ -48,3 +48,29 @@ Returns WSL path if in WSL, otherwise returns Windows path.
48
48
  Returns: `Promise<string>`
49
49
 
50
50
  Get the mount point for fixed drives in WSL.
51
+
52
+ ### convertWslPathToWindows(path)
53
+
54
+ Returns: `Promise<string>`
55
+
56
+ Convert a WSL Linux path to a Windows-accessible path.
57
+
58
+ URLs (strings starting with a protocol like `https://`) are returned unchanged.
59
+
60
+ ```js
61
+ import {convertWslPathToWindows} from 'wsl-utils';
62
+
63
+ // Convert a Linux path
64
+ const windowsPath = await convertWslPathToWindows('/home/user/file.html');
65
+ //=> '\\wsl.localhost\Ubuntu\home\user\file.html'
66
+
67
+ // URLs are not converted
68
+ const url = await convertWslPathToWindows('https://example.com');
69
+ //=> 'https://example.com'
70
+ ```
71
+
72
+ #### path
73
+
74
+ Type: `string`
75
+
76
+ The WSL path to convert (e.g., `/home/user/file.html`).