path-interop
Provides path format conversion between Windows and Linux including path seperator conversion and environment variable expansion.
Allows you to add Linux path format interoperability into your Windows/Linux cross-platform apps.
Install
npm install --save path-interop
Usage
// Single instanceconst PathInterop = ; // When multiple instances are required, use clone()const interop1 = ;const interop2 = interop1;
Functions
windowsToLinux(path, expand)
Converts path format from Windows to Linux with/without environment variable expansion.
Specify true
for expand
in order to expand environment variables like %VAR%
.
Otherwise, variable names are preserved. For example,
windowsToLinux('%USERPROFILE%/')
will return '~/'
without expansion.
NOTE: Windows Drive letter and colon (C:
) is not convertible to Linux
format. Therefore, by default it is preserved. For exmaple,
windowsToLinux('C:\\')
returns 'C:/'
. You can change this behavior by using
driveLetterConverter option.
linuxToWindows(path, expand)
Converts path format from Linux to Windows with/without environment variable expansion.
Specify true
for expand
in order to expand environment variables like ~/
,
$VAR
or ${VAR}
.
Otherwise, variable names are preserved. For example, linuxToWindows('~/')
will return '%USERPROFILE%/'
without expansion.
NOTE: ~otherUserName/
is unsupported
toLinux(path, expand)
Forcely converts path format to Linux with/without environment variable expansion.
Equivalent to windowsToLinux(linuxToWindows(path), expand)
.
toWindows(path, expand)
Forcely converts path format to Windows with/without environment variable expansion.
Equivalent to linuxToWindows(windowsToLinux(path), expand)
.
toSystem(path, expand)
Forcely converts path format to current platform. If process.platform
is
win32
internally calls toWindows
, otherwise calls toLinux
.
Options
driveLetterConverter
Specify converter function to convert drive letter.
// Default value (Preserve)PathInterop { return letter + ':'} // For MSYS2 (C:\ => /c)PathInterop { return '/' + letter; } // Just only remove drive letter and colonPathInterop { return '';}
caseInsensitivePlatforms
Array of platform name that store case-insensitive variable name like Windows.
Return values of process.platform()
are used.
// Default valuePathInteropcaseInsensitivePlatforms = 'win32' ; // Custom additional valuePathInteropcaseInsensitivePlatforms; // Custom valuePathInteropcaseInsensitivePlatforms = 'win32' 'futurePlatform'
linuxEnvToWindowsEnv
Object that represent conversion table of environment variable name (Linux to Windows).
// Default valuePathInteroplinuxEnvToWindowsEnvHOME = 'USERPROFILE'; // Custom additonal valuePathInteroplinuxEnvToWindowsEnvLINUXENV = 'WINENV'; // Custom overwritePathInteroplinuxEnvToWindowsEnv = LINUXENV: 'WINENV' ;
windowsEnvToLinuxEnv
Object that represent conversion table of environment variable name (Windows to Linux).
// Default valuePathInteropwindowsEnvToLinuxEnvUSERPROFILE = 'HOME'; // Custom additonal valuePathInteropwindowsEnvToLinuxEnvWINENV = 'LINUXENV'; // Custom overwritePathInteropwindowsEnvToLinuxEnv = WINENV: 'LINUXENV' ;
clone
Creates a new object that is a copy of the current instance.
var p1 = ;var p2 = ; console; // True (Because of require-cache) var p3 = p1; console; // Falseconsole; // False
License
Distributed under the MIT license
Copyright (C) 2016 Retorillo