| 12 | import { sortBy } from "./array"; |
| 13 | |
| 14 | export function fsPath(uri: URI, { useRealCasing = false }: { useRealCasing?: boolean; } = {}): string { |
| 15 | // eslint-disable-next-line no-restricted-properties |
| 16 | let newPath = typeof uri === "string" ? uri : uri.fsPath; |
| 17 | |
| 18 | // Fix drive letter first, so we won't write warnings if that's the only difference. |
| 19 | newPath = forceWindowsDriveLetterToUppercase(newPath); |
| 20 | |
| 21 | if (useRealCasing) { |
| 22 | const realPath = fs.existsSync(newPath) && safeRealpathSync(newPath, { useNative: true }); |
| 23 | // Since realpathSync.native will resolve symlinks, only do anything if the paths differ |
| 24 | // _only_ by case. |
| 25 | // when there was no symlink (eg. the lowercase version of both paths match). |
| 26 | if (realPath && realPath.toLowerCase() === newPath.toLowerCase() && realPath !== newPath) { |
| 27 | console.warn(`Rewriting path:\n ${newPath}\nto:\n ${realPath} because the casing appears incorrect`); |
| 28 | newPath = realPath; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return newPath; |
| 33 | } |
| 34 | |
| 35 | export function safeRealpathSync(filePath: string, { useNative = false }: { useNative?: boolean; } = {}): string { |
| 36 | try { |