forked from stack-auth/stack-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.tsx
More file actions
33 lines (30 loc) · 990 Bytes
/
fs.tsx
File metadata and controls
33 lines (30 loc) · 990 Bytes
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
import * as stackFs from "fs";
import * as path from "path";
export async function list(path: string) {
return await stackFs.promises.readdir(path);
}
export async function listRecursively(p: string, options: { excludeDirectories?: boolean } = {}): Promise<string[]> {
const files = await list(p);
return [
...(await Promise.all(files.map(async (fileName) => {
const filePath = path.join(p, fileName);
if ((await stackFs.promises.stat(filePath)).isDirectory()) {
return [
...(await listRecursively(filePath, options)),
...(options.excludeDirectories ? [] : [filePath]),
];
} else {
return [filePath];
}
}))).flat(),
];
}
export function writeFileSyncIfChanged(path: string, content: string): void {
if (stackFs.existsSync(path)) {
const existingContent = stackFs.readFileSync(path, "utf-8");
if (existingContent === content) {
return;
}
}
stackFs.writeFileSync(path, content);
}