-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFileSystem.ts
More file actions
37 lines (34 loc) · 1.01 KB
/
FileSystem.ts
File metadata and controls
37 lines (34 loc) · 1.01 KB
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
import * as fs from "fs";
import * as glob from "glob";
import * as path from "path";
import type { IFileSystem } from "../types/FileSystem";
export class FsFileSystem implements IFileSystem {
public fileExists(filePath: string): boolean {
try {
return fs.statSync(filePath).isFile();
} catch (err) {
return false;
}
}
public readFile(filePath: string, encoding?: BufferEncoding): string {
if (encoding) {
return fs.readFileSync(filePath, { encoding });
}
return fs.readFileSync(filePath).toString();
}
public writeFile(filePath: string, text: string): void {
fs.writeFileSync(filePath, text);
}
public directoryExists(dirPath: string): boolean {
try {
return fs.statSync(dirPath).isDirectory();
} catch (e) {
return false;
}
}
public glob(dirPath: string, pattern: string): string[] {
// NB!: glob 8+ patterns use `\` as escape only, so ensure posix-style:
const globPattern = path.join(dirPath, pattern).replace(/\\/g, "/");
return glob.sync(globPattern, { nodir: true });
}
}