|
| 1 | +import fs from "fs"; |
| 2 | +import * as os from "os"; |
| 3 | +import path from "path"; |
| 4 | + |
| 5 | +/* ****************************************************************************************************************** */ |
| 6 | +// region: Locals |
| 7 | +/* ****************************************************************************************************************** */ |
| 8 | + |
| 9 | +let isCaseSensitiveFilesystem: boolean | undefined; |
| 10 | + |
| 11 | +// endregion |
| 12 | + |
| 13 | +/* ****************************************************************************************************************** */ |
| 14 | +// region: Helpers |
| 15 | +/* ****************************************************************************************************************** */ |
| 16 | + |
| 17 | +function tryRmFile(fileName: string) { |
| 18 | + try { |
| 19 | + if (fs.existsSync(fileName)) fs.rmSync(fileName, { force: true }); |
| 20 | + } catch {} |
| 21 | +} |
| 22 | + |
| 23 | +function getIsFsCaseSensitive() { |
| 24 | + if (isCaseSensitiveFilesystem != null) return isCaseSensitiveFilesystem; |
| 25 | + |
| 26 | + for (let i = 0; i < 1000; i++) { |
| 27 | + const tmpFileName = path.join(os.tmpdir(), `tstp~${i}.tmp`); |
| 28 | + |
| 29 | + tryRmFile(tmpFileName); |
| 30 | + |
| 31 | + try { |
| 32 | + fs.writeFileSync(tmpFileName, ""); |
| 33 | + isCaseSensitiveFilesystem = !fs.existsSync(tmpFileName.replace("tstp", "TSTP")); |
| 34 | + return isCaseSensitiveFilesystem; |
| 35 | + } catch { |
| 36 | + } finally { |
| 37 | + tryRmFile(tmpFileName); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + console.warn( |
| 42 | + `Could not determine filesystem's case sensitivity. Please file a bug report with your system's details` |
| 43 | + ); |
| 44 | + isCaseSensitiveFilesystem = false; |
| 45 | + |
| 46 | + return isCaseSensitiveFilesystem; |
| 47 | +} |
| 48 | + |
| 49 | +function getMatchPortion(from: string, to: string) { |
| 50 | + const lowerFrom = from.toLocaleLowerCase(); |
| 51 | + const lowerTo = to.toLocaleLowerCase(); |
| 52 | + |
| 53 | + const maxLen = Math.max(lowerFrom.length, lowerTo.length); |
| 54 | + |
| 55 | + let i = 0; |
| 56 | + while (i < maxLen) { |
| 57 | + if (lowerFrom[i] !== lowerTo[i]) break; |
| 58 | + i++; |
| 59 | + } |
| 60 | + |
| 61 | + return from.slice(0, i); |
| 62 | +} |
| 63 | + |
| 64 | +// endregion |
| 65 | + |
| 66 | +/* ****************************************************************************************************************** */ |
| 67 | +// region: Utils |
| 68 | +/* ****************************************************************************************************************** */ |
| 69 | + |
| 70 | +export function getRelativePath(from: string, to: string) { |
| 71 | + try { |
| 72 | + from = fs.realpathSync.native(from); |
| 73 | + to = fs.realpathSync.native(to); |
| 74 | + } catch { |
| 75 | + if (!getIsFsCaseSensitive()) { |
| 76 | + const matchPortion = getMatchPortion(from, to); |
| 77 | + from = matchPortion + from.slice(matchPortion.length); |
| 78 | + to = matchPortion + to.slice(matchPortion.length); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return path.relative(from, to); |
| 83 | +} |
| 84 | + |
| 85 | +// endregion |
0 commit comments