-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
53 lines (46 loc) · 1.33 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import fs from 'fs';
const invertCase = (string: string) => {
let newString = '';
for (let i = 0; i < string.length; i += 1) {
const character = string[i];
const uppercase = character.toUpperCase();
newString += (
character === uppercase
? character.toLowerCase()
: uppercase
);
}
return newString;
};
const UPPER = 65;
const LOWER = 97;
const getRandomLetter = () => Math.floor(Math.random() * 26);
const getRandomWord = (length: number) => Array.from(
{ length },
() => String.fromCodePoint(
getRandomLetter()
+ (Math.random() > 0.5 ? UPPER : LOWER),
),
).join('');
export const isFsCaseSensitive = (
fsInstance = fs,
) => {
/**
* Check Node.js path if it exists
*
* Originally used the current file name, but it's more complicated
* because you have to use __filename or import.meta.url depending
* on the context, and the actual file name is user-defined and hence
* can be case-insensitive (e.g. _)
*/
const checkFile = process.execPath;
if (fsInstance.existsSync(checkFile)) {
return !fsInstance.existsSync(invertCase(checkFile));
}
// Generate random file and see if it exists
const fileName = `/${getRandomWord(10)}`;
fsInstance.writeFileSync(fileName, '');
const isCaseSensitive = !fsInstance.existsSync(invertCase(fileName));
fsInstance.unlinkSync(fileName);
return isCaseSensitive;
};