forked from firebase/firebase-tools
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsutils.ts
40 lines (36 loc) · 923 Bytes
/
fsutils.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
import { readFileSync, readdirSync, statSync } from "fs";
import { FirebaseError } from "./error";
export function fileExistsSync(path: string): boolean {
try {
return statSync(path).isFile();
} catch (e: any) {
return false;
}
}
export function dirExistsSync(path: string): boolean {
try {
return statSync(path).isDirectory();
} catch (e: any) {
return false;
}
}
export function readFile(path: string): string {
try {
return readFileSync(path).toString();
} catch (e: unknown) {
if ((e as NodeJS.ErrnoException).code === "ENOENT") {
throw new FirebaseError(`File not found: ${path}`);
}
throw e;
}
}
export function listFiles(path: string): string[] {
try {
return readdirSync(path);
} catch (e: unknown) {
if ((e as NodeJS.ErrnoException).code === "ENOENT") {
throw new FirebaseError(`Directory not found: ${path}`);
}
throw e;
}
}