-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c6b218
commit 92f68e3
Showing
1 changed file
with
11 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
import * as nodePath from 'path'; | ||
|
||
const PATH_REGEXP = new RegExp('\\' + nodePath.win32.sep, 'g'); | ||
const ensureUnixPathCache = new Map<string, string>(); | ||
|
||
export const ensureUnixPath = (path?: string): string | null => { | ||
if (!path) { | ||
return null; | ||
} | ||
|
||
const cachePath = ensureUnixPathCache.get(path); | ||
if (cachePath) { | ||
return cachePath; | ||
} | ||
|
||
// we use a regex instead of the character literal due to a bug in some versions of node.js | ||
// the path separator needs to be preceded by an escape character | ||
const regex = new RegExp('\\' + nodePath.win32.sep, 'g'); | ||
return path.replace(regex, nodePath.posix.sep); | ||
const normalizedPath = path.replace(PATH_REGEXP, nodePath.posix.sep); | ||
ensureUnixPathCache.set(path, normalizedPath); | ||
return normalizedPath; | ||
}; |