forked from IsaacScript/isaacscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.mts
105 lines (88 loc) · 2.83 KB
/
utils.mts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as fs from "node:fs";
import * as url from "node:url";
// eslint-disable-next-line @typescript-eslint/naming-convention, no-underscore-dangle
export const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
export function capitalizeFirstLetter(string: string): string {
if (string === "") {
return string;
}
const firstCharacter = string.charAt(0);
const capitalizedFirstLetter = firstCharacter.toUpperCase();
const restOfString = string.slice(1);
return `${capitalizedFirstLetter}${restOfString}`;
}
export function deleteFileOrDirectory(filePath: string): void {
try {
fs.rmSync(filePath, {
recursive: true,
});
} catch (err) {
error(`Failed to delete file or directory "${filePath}":`, err);
}
}
/**
* Helper function to print out an error message and then exit the program.
*
* All of the arguments will be directly passed to the `console.error` function.
*/
export function error(...args: unknown[]): never {
console.error(...args);
return process.exit(1);
}
export function fileExists(filePath: string): boolean {
let pathExists: boolean;
try {
pathExists = fs.existsSync(filePath);
} catch (err) {
error(`Failed to check if "${filePath}" exists:`, err);
}
return pathExists;
}
/** From: https://github.com/expandjs/expandjs/blob/master/lib/kebabCaseRegex.js */
const KEBAB_CASE_REGEX =
/^([a-z](?![\d])|[\d](?![a-z]))+(-?([a-z](?![\d])|[\d](?![a-z])))*$|^$/;
/** Kebab case is the naming style of using all lowercase and hyphens, like "foo-bar". */
export function isKebabCase(string: string): boolean {
return KEBAB_CASE_REGEX.test(string);
}
/** Will recursively make as many subdirectories as needed. */
export function makeDir(dirPath: string): void {
try {
fs.mkdirSync(dirPath, {
recursive: true,
});
} catch (err) {
error(`Failed to create the "${dirPath}" directory:`, err);
}
}
export function readFile(filePath: string): string {
let fileContents: string;
try {
fileContents = fs.readFileSync(filePath, "utf8");
} catch (err) {
error(`Failed to read the "${filePath}" file:`, err);
}
return fileContents;
}
export function renameFile(srcPath: string, dstPath: string): void {
try {
fs.renameSync(srcPath, dstPath);
} catch (err) {
error(`Failed to rename "${srcPath}" to "${dstPath}":`, err);
}
}
/** Helper function to trim a suffix from a string, if it exists. Returns the trimmed string. */
export function trimSuffix(string: string, prefix: string): string {
if (!string.endsWith(prefix)) {
return string;
}
const endCharacter = string.length - prefix.length;
return string.slice(0, endCharacter);
}
export function writeFile(filePath: string, data: string): void {
try {
fs.writeFileSync(filePath, data);
} catch (err) {
error(`Failed to write to the "${filePath}" file:`, err);
}
}