Giter Site home page Giter Site logo

wsl-path's Introduction

Build Status

wsl-path

A small node utility for converting file paths from POSIX paths in wsl (Windows Subsystem for Linux) to their counterparts in the Windows Filesystem and vice versa.

How it works

This utility requires the wslpath CLI tool to be installed on the running machine and a wsl environment. Only the base drive letter (Windows) / mount folder (POSIX) is being translated into the windows world, the rest of the path is then appended in the correct form. Base path resolutions are cached, so only the first resolution causes wslpath to be called, while subsequent paths in the same drive/mount folder are resolved by the cache.

The utility checks the mount points for the WSL environment in order to determine cachable paths.

Usage

The following examples are taken from the unit tests:

windowsToWsl(path, options?)

Converts a windows path to a WSL (POSIX) path.

const correctPath = "C:\\Users";

const result = await windowsToWsl(correctPath);

expect(result).toEqual("/mnt/c/Users");

wslToWindows(path, options?)

Converts a WSL (POSIX) Path to a windows path.

const mountedPath = "/mnt/c/Users";

const result = await wslToWindows(mountedPath);

expect(result).toEqual("C:\\Users");

windowsToWslSync(path, options?)

Converts a windows path to a WSL (POSIX) path in a synchronous call.

const correctPath = "C:\\Users";

const result =  windowsToWslSync(correctPath);

expect(result).toEqual("/mnt/c/Users");

wslToWindowsSync(path, options?)

Converts a WSL (POSIX) Path to a windows path in a synchronous call.

const mountedPath = "/mnt/c/Users";

const result = wslToWindowsSync(mountedPath);

expect(result).toEqual("C:\\Users");

Resolving without wslpath

If you want to resolve without having wslpath (or without wanting to spawn a process calling it) you can provide your own cache in the options containing the base path resolution:

const windowsPath = "C:\\Users";

const result1 = await windowsToWsl(windowsPath, {
   basePathCache: { "C:\\": "/mnt/x" }
});

expect(result1).toEqual("/mnt/x/Users");

Using different wsl environments than the default

If you have multiple wsl environments installed, you can use the wslCommand option for setting up the shell that should be used:

const result1 = await windowsToWsl(windowsPath, {
   wslCommand: 'ubuntu run'
});

Building and testing

Install packages: npm install

Build: npm run build

Test: npm run test or npm run test -- --watch

Integration Test, which does not mock wslpath: CALL_WSL_PROCESS=1 npm run test

wsl-path's People

Contributors

crayon2000 avatar dependabot[bot] avatar github-actions[bot] avatar mojadev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

davehorner

wsl-path's Issues

windowsToWslSync returns incorrect path after second call

On my system, I have C:\ mounted at /mnt/c in WSL. Let's say I pass the path C:\Users\jamai\Documents\idris\tcs4\src\SmallStep.idr to windowsToWslSync. Then I get the result I expected: /mnt/c/Users/jamai/Documents/idris/tcs4/src/SmallStep.idr. But then when the function is called again with the same path and cache, windowsToWslSync returns /mnt/c/Users/jamai/Documents/idris/tcs4/src/Users/jamai/Documents/idris/tcs4/src/SmallStep.idr. This is the resolution cache after the calls:

{
	"basePathCache": {"wsl:C:\\": "/mnt/c/Users/jamai/Documents/idris/tcs4/src", "wsl:/mnt/c/Users/jamai/Documents/idris/tcs4/src": "C:\\"},
	"mountPoints": [],
	"wslCommand": "wsl"
}

I'm not sure how the resolution cache is supposed to work, but I'd think it should associate "C:" with "/mnt/c".

If I always pass a new empty cache to windowsToWslSync, the function works.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.