-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Cover edge case issues with case-insensitive filesystems
- Loading branch information
Showing
3 changed files
with
90 additions
and
19 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,30 +1,15 @@ | ||
import url from "url"; | ||
import path from "path"; | ||
import { realpathSync } from "fs"; | ||
|
||
/* ****************************************************************************************************************** * | ||
* General Utilities & Helpers | ||
* ****************************************************************************************************************** */ | ||
|
||
export const isURL = (s: string): boolean => !!s && (!!url.parse(s).host || !!url.parse(s).hostname); | ||
|
||
export const cast = <T>(v: any): T => v; | ||
|
||
export const isBaseDir = (baseDir: string, testDir: string): boolean => { | ||
const relative = path.relative(baseDir, testDir); | ||
return relative ? !relative.startsWith("..") && !path.isAbsolute(relative) : true; | ||
}; | ||
|
||
export const maybeAddRelativeLocalPrefix = (p: string) => (p[0] === "." ? p : `./${p}`); | ||
|
||
export function tryRealpathNative(value: string) { | ||
try { | ||
return realpathSync.native(value); | ||
} catch { | ||
return value; | ||
} | ||
} | ||
|
||
export function nativeRelativePath(from: string, to: string) { | ||
return path.relative(tryRealpathNative(from), tryRealpathNative(to)); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import fs from "fs"; | ||
import * as os from "os"; | ||
import path from "path"; | ||
|
||
/* ****************************************************************************************************************** */ | ||
// region: Locals | ||
/* ****************************************************************************************************************** */ | ||
|
||
let isCaseSensitiveFilesystem: boolean | undefined; | ||
|
||
// endregion | ||
|
||
/* ****************************************************************************************************************** */ | ||
// region: Helpers | ||
/* ****************************************************************************************************************** */ | ||
|
||
function tryRmFile(fileName: string) { | ||
try { | ||
if (fs.existsSync(fileName)) fs.rmSync(fileName, { force: true }); | ||
} catch {} | ||
} | ||
|
||
function getIsFsCaseSensitive() { | ||
if (isCaseSensitiveFilesystem != null) return isCaseSensitiveFilesystem; | ||
|
||
for (let i = 0; i < 1000; i++) { | ||
const tmpFileName = path.join(os.tmpdir(), `tstp~${i}.tmp`); | ||
|
||
tryRmFile(tmpFileName); | ||
|
||
try { | ||
fs.writeFileSync(tmpFileName, ""); | ||
isCaseSensitiveFilesystem = !fs.existsSync(tmpFileName.replace("tstp", "TSTP")); | ||
return isCaseSensitiveFilesystem; | ||
} catch { | ||
} finally { | ||
tryRmFile(tmpFileName); | ||
} | ||
} | ||
|
||
console.warn( | ||
`Could not determine filesystem's case sensitivity. Please file a bug report with your system's details` | ||
); | ||
isCaseSensitiveFilesystem = false; | ||
|
||
return isCaseSensitiveFilesystem; | ||
} | ||
|
||
function getMatchPortion(from: string, to: string) { | ||
const lowerFrom = from.toLocaleLowerCase(); | ||
const lowerTo = to.toLocaleLowerCase(); | ||
|
||
const maxLen = Math.max(lowerFrom.length, lowerTo.length); | ||
|
||
let i = 0; | ||
while (i < maxLen) { | ||
if (lowerFrom[i] !== lowerTo[i]) break; | ||
i++; | ||
} | ||
|
||
return from.slice(0, i); | ||
} | ||
|
||
// endregion | ||
|
||
/* ****************************************************************************************************************** */ | ||
// region: Utils | ||
/* ****************************************************************************************************************** */ | ||
|
||
export function getRelativePath(from: string, to: string) { | ||
try { | ||
from = fs.realpathSync.native(from); | ||
to = fs.realpathSync.native(to); | ||
} catch { | ||
if (!getIsFsCaseSensitive()) { | ||
const matchPortion = getMatchPortion(from, to); | ||
from = matchPortion + from.slice(matchPortion.length); | ||
to = matchPortion + to.slice(matchPortion.length); | ||
} | ||
} | ||
|
||
return path.relative(from, to); | ||
} | ||
|
||
// endregion |
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