+99
-0
@@ -82,1 +82,100 @@ /** | ||
| export function convertWslPathToWindows(path: string): Promise<string>; | ||
| /** | ||
| Convert multiple WSL Linux paths to Windows-accessible paths. | ||
| URLs (strings starting with a protocol like `https://`) are returned unchanged. | ||
| @param paths - The WSL paths to convert. | ||
| @returns The Windows-accessible paths in the same order, or the original paths if conversion fails. | ||
| @example | ||
| ``` | ||
| import {convertWslPathToWindows} from 'wsl-utils'; | ||
| const windowsPaths = await convertWslPathToWindows([ | ||
| '/home/user/file.html', | ||
| '/mnt/c/Users/file.txt', | ||
| 'https://example.com' | ||
| ]); | ||
| //=> ['\\wsl.localhost\Ubuntu\home\user\file.html', 'C:\Users\file.txt', 'https://example.com'] | ||
| ``` | ||
| */ | ||
| export function convertWslPathToWindows(paths: string[]): Promise<string[]>; | ||
| /** | ||
| Check if a Windows path is a UNC path (e.g., `\\wsl.localhost\...` or `\\wsl$\...`). | ||
| UNC paths indicate the file resides on the WSL Linux filesystem rather than a Windows drive. | ||
| @param path - The Windows path to check. | ||
| @returns `true` if the path is a UNC path, `false` otherwise. | ||
| @example | ||
| ``` | ||
| import {isUncPath} from 'wsl-utils'; | ||
| isUncPath('\\\\wsl.localhost\\Ubuntu\\home\\user'); | ||
| //=> true | ||
| isUncPath('C:\\Users\\file.txt'); | ||
| //=> false | ||
| ``` | ||
| */ | ||
| export function isUncPath(path: string): boolean; | ||
| /** | ||
| Check if a WSL path maps to the Windows filesystem. | ||
| This converts the path and checks if it's on a Windows drive (e.g., `/mnt/c/...` → `C:\...`) rather than the Linux filesystem (e.g., `/home/...` → `\\wsl$\...`). | ||
| @param path - The WSL path to check. | ||
| @returns `true` if the path is on a Windows drive, `false` if it's on the Linux filesystem. | ||
| @example | ||
| ``` | ||
| import {isPathOnWindowsFilesystem} from 'wsl-utils'; | ||
| await isPathOnWindowsFilesystem('/mnt/c/Users/file.txt'); | ||
| //=> true | ||
| await isPathOnWindowsFilesystem('/home/user/file.txt'); | ||
| //=> false | ||
| ``` | ||
| */ | ||
| export function isPathOnWindowsFilesystem(path: string): Promise<boolean>; | ||
| /** | ||
| Convert a Windows path to a WSL Linux path. | ||
| @param path - The Windows path to convert (e.g., `C:\Users\file.txt`). | ||
| @returns The WSL path (e.g., `/mnt/c/Users/file.txt`) or the original path if conversion fails. | ||
| @example | ||
| ``` | ||
| import {convertWindowsPathToWsl} from 'wsl-utils'; | ||
| const wslPath = await convertWindowsPathToWsl('C:\\Users\\file.txt'); | ||
| //=> '/mnt/c/Users/file.txt' | ||
| ``` | ||
| */ | ||
| export function convertWindowsPathToWsl(path: string): Promise<string>; | ||
| /** | ||
| Convert multiple Windows paths to WSL Linux paths. | ||
| @param paths - The Windows paths to convert. | ||
| @returns The WSL paths in the same order, or the original paths if conversion fails. | ||
| @example | ||
| ``` | ||
| import {convertWindowsPathToWsl} from 'wsl-utils'; | ||
| const wslPaths = await convertWindowsPathToWsl([ | ||
| 'C:\\Users\\file.txt', | ||
| 'D:\\Projects\\app' | ||
| ]); | ||
| //=> ['/mnt/c/Users/file.txt', '/mnt/d/Projects/app'] | ||
| ``` | ||
| */ | ||
| export function convertWindowsPathToWsl(paths: string[]): Promise<string[]>; |
+50
-8
@@ -83,14 +83,56 @@ import {promisify} from 'node:util'; | ||
| export const convertWslPathToWindows = async path => { | ||
| // Don't convert URLs | ||
| if (/^[a-z]+:\/\//i.test(path)) { | ||
| return path; | ||
| const isUrl = path => /^[a-z]+:\/\//i.test(path); | ||
| export const convertWslPathToWindows = async paths => { | ||
| const isBatch = Array.isArray(paths); | ||
| const pathArray = isBatch ? paths : [paths]; | ||
| // Find indices of non-URL paths that need conversion | ||
| const indicesToConvert = []; | ||
| const pathsToConvert = []; | ||
| for (const [index, path] of pathArray.entries()) { | ||
| if (!isUrl(path)) { | ||
| indicesToConvert.push(index); | ||
| pathsToConvert.push(path); | ||
| } | ||
| } | ||
| // Start with original paths (URLs stay as-is) | ||
| const results = [...pathArray]; | ||
| if (pathsToConvert.length > 0) { | ||
| try { | ||
| const {stdout} = await execFile('wslpath', ['-aw', ...pathsToConvert], {encoding: 'utf8'}); | ||
| const convertedPaths = stdout.split(/\r?\n/).filter(Boolean); | ||
| for (const [index, originalIndex] of indicesToConvert.entries()) { | ||
| results[originalIndex] = convertedPaths[index] ?? pathArray[originalIndex]; | ||
| } | ||
| } catch { | ||
| // If wslpath fails, keep original paths | ||
| } | ||
| } | ||
| return isBatch ? results : results[0]; | ||
| }; | ||
| export const isUncPath = path => /^\\\\/u.test(path); | ||
| export const isPathOnWindowsFilesystem = async path => { | ||
| const windowsPath = await convertWslPathToWindows(path); | ||
| return !isUncPath(windowsPath); | ||
| }; | ||
| export const convertWindowsPathToWsl = async paths => { | ||
| const isBatch = Array.isArray(paths); | ||
| const pathArray = isBatch ? paths : [paths]; | ||
| try { | ||
| const {stdout} = await execFile('wslpath', ['-aw', path], {encoding: 'utf8'}); | ||
| return stdout.trim(); | ||
| const {stdout} = await execFile('wslpath', ['-u', ...pathArray], {encoding: 'utf8'}); | ||
| const convertedPaths = stdout.split(/\r?\n/).filter(Boolean); | ||
| const results = pathArray.map((original, index) => convertedPaths[index] ?? original); | ||
| return isBatch ? results : results[0]; | ||
| } catch { | ||
| // If wslpath fails, return the original path | ||
| return path; | ||
| return isBatch ? pathArray : pathArray[0]; | ||
| } | ||
@@ -97,0 +139,0 @@ }; |
+1
-1
| { | ||
| "name": "wsl-utils", | ||
| "version": "0.3.1", | ||
| "version": "0.4.0", | ||
| "description": "Utilities for working with Windows Subsystem for Linux (WSL)", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
+112
-0
@@ -112,1 +112,113 @@ # wsl-utils | ||
| The WSL path to convert (e.g., `/home/user/file.html`). | ||
| ### convertWslPathToWindows(paths) | ||
| Returns: `Promise<string[]>` | ||
| Convert multiple WSL Linux paths to Windows-accessible paths. | ||
| ```js | ||
| import {convertWslPathToWindows} from 'wsl-utils'; | ||
| const windowsPaths = await convertWslPathToWindows([ | ||
| '/home/user/file.html', | ||
| '/mnt/c/Users/file.txt', | ||
| 'https://example.com' | ||
| ]); | ||
| //=> ['\\wsl.localhost\Ubuntu\home\user\file.html', 'C:\Users\file.txt', 'https://example.com'] | ||
| ``` | ||
| #### paths | ||
| Type: `string[]` | ||
| The WSL paths to convert. | ||
| ### isUncPath(path) | ||
| Returns: `boolean` | ||
| Check if a Windows path is a UNC path (e.g., `\\wsl.localhost\...` or `\\wsl$\...`). | ||
| UNC paths indicate the file resides on the WSL Linux filesystem rather than a Windows drive. | ||
| ```js | ||
| import {isUncPath} from 'wsl-utils'; | ||
| isUncPath('\\\\wsl.localhost\\Ubuntu\\home\\user'); | ||
| //=> true | ||
| isUncPath('C:\\Users\\file.txt'); | ||
| //=> false | ||
| ``` | ||
| #### path | ||
| Type: `string` | ||
| The Windows path to check. | ||
| ### isPathOnWindowsFilesystem(path) | ||
| Returns: `Promise<boolean>` | ||
| Check if a WSL path maps to the Windows filesystem. | ||
| This converts the path and checks if it's on a Windows drive (e.g., `/mnt/c/...` → `C:\...`) rather than the Linux filesystem (e.g., `/home/...` → `\\wsl$\...`). | ||
| ```js | ||
| import {isPathOnWindowsFilesystem} from 'wsl-utils'; | ||
| await isPathOnWindowsFilesystem('/mnt/c/Users/file.txt'); | ||
| //=> true | ||
| await isPathOnWindowsFilesystem('/home/user/file.txt'); | ||
| //=> false | ||
| ``` | ||
| #### path | ||
| Type: `string` | ||
| The WSL path to check. | ||
| ### convertWindowsPathToWsl(path) | ||
| Returns: `Promise<string>` | ||
| Convert a Windows path to a WSL Linux path. | ||
| ```js | ||
| import {convertWindowsPathToWsl} from 'wsl-utils'; | ||
| const wslPath = await convertWindowsPathToWsl('C:\\Users\\file.txt'); | ||
| //=> '/mnt/c/Users/file.txt' | ||
| ``` | ||
| #### path | ||
| Type: `string` | ||
| The Windows path to convert. | ||
| ### convertWindowsPathToWsl(paths) | ||
| Returns: `Promise<string[]>` | ||
| Convert multiple Windows paths to WSL Linux paths. | ||
| ```js | ||
| import {convertWindowsPathToWsl} from 'wsl-utils'; | ||
| const wslPaths = await convertWindowsPathToWsl([ | ||
| 'C:\\Users\\file.txt', | ||
| 'D:\\Projects\\app' | ||
| ]); | ||
| //=> ['/mnt/c/Users/file.txt', '/mnt/d/Projects/app'] | ||
| ``` | ||
| #### paths | ||
| Type: `string[]` | ||
| The Windows paths to convert. |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
16484
61.96%261
68.39%224
100%3
50%