-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.ts
More file actions
30 lines (27 loc) · 960 Bytes
/
filesystem.ts
File metadata and controls
30 lines (27 loc) · 960 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
import fs from "fs";
export type FileSystem = {
readonly cwd: () => string;
readonly isFile: IsFile;
readonly isDirectory: IsDirectory;
readonly getRealpath: GetRealpath;
readonly readFile: ReadFile;
};
export type IsFile = (path: string) => boolean;
export type IsDirectory = (path: string) => boolean;
export type GetRealpath = (path: string) => string | undefined;
export type ReadFile = (filename: string) => string | undefined;
export function createDefaultFilesystem(): FileSystem {
return {
cwd: process.cwd,
isFile: (url: string) => fs.statSync(url, { throwIfNoEntry: false })?.isFile() ?? false,
isDirectory: (path: string) => fs.statSync(path, { throwIfNoEntry: false })?.isDirectory() ?? false,
getRealpath: (path: string) => {
try {
return fs.realpathSync(path);
} catch (e: unknown) {
return undefined;
}
},
readFile: (path: string) => fs.readFileSync(path, "utf8"),
};
}