idleberg idleberg - 5 months ago 70
Node.js Question

How to translate a Windows path for Windows Subsystem for Linux

The latest Windows 10 Insider Build introduced the Windows Subsystem for Linux (WSL) including a native

bash
provided by Canonical, the company behind Ubuntu. Their implementation of
bash
goes by the rather complicated name of Bash on Ubuntu on Windows, which I will refer to as
bash.exe
in the following.

The equivalent of accessing the Windows path
C:\Users\me\Desktop
in
bash.exe
is
/mnt/c/Users/me/Desktop
, but since I'm trying to pass a path to
bash.exe
through
cmd.exe
(e.g.
bash -c ls /mnt/me/Desktop
), I was wondering if Microsoft offers any tools to translate these paths programmatically, something like
cygpath
in Cygwin.

Although an official Windows way is preferred, I'm open to alternatives to determine the path, e.g. in Node or Python.

Answer

It turns out that NodeJS has a built-in module for this kind of stuff, called path. Although it doesn't completely fix the issue at hand, it's a valid workaround (to me). Just require("path") and decide on the block or one liner.

Here's the block:

var p = require("path")
var path = "C:\\Users\\me\\Desktop"
var sepa = p.split(p.win32.sep)
var newS = [].concat([sepa[0].toLowerCase()], sepa.slice(1))
var newP = "/mnt/" + p.posix.join.apply(p.posix, newS).replace(":", "")
// newP == "/mnt/c/Users/me/Desktop

As a one liner:

var p = require("path")
var d = "/mnt/" + p.posix.join.apply(p.posix, [].concat(["F:\\Programming\\Code".split(p.win32.sep)[0].toLowerCase()], "F:\\Programming\\Code".split(p.win32.sep).slice(1))).replace(":", "")
Comments