-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfileSystem.ts
More file actions
77 lines (66 loc) · 2.07 KB
/
Copy pathfileSystem.ts
File metadata and controls
77 lines (66 loc) · 2.07 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
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
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
const stat = promisify(fs.stat);
const mkdir = promisify(fs.mkdir);
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
const readdir = promisify(fs.readdir);
export class FileSystemManager {
async ensureDirectoryExists(dirPath: string): Promise<void> {
try {
await stat(dirPath);
} catch (error) {
// Directory doesn't exist, create it
await mkdir(dirPath, { recursive: true });
}
}
async directoryExists(dirPath: string): Promise<boolean> {
try {
const stats = await stat(dirPath);
return stats.isDirectory();
} catch {
return false;
}
}
async readDirectory(dirPath: string): Promise<string[]> {
try {
return await readdir(dirPath);
} catch (error) {
throw new Error(`Failed to read directory ${dirPath}: ${error}`);
}
}
async writeFileContent(filePath: string, content: string): Promise<void> {
const dir = path.dirname(filePath);
await this.ensureDirectoryExists(dir);
await writeFile(filePath, content, 'utf8');
}
async readFileContent(filePath: string): Promise<string> {
try {
return await readFile(filePath, 'utf8');
} catch (error) {
throw new Error(`Failed to read file ${filePath}: ${error}`);
}
}
async fileExists(filePath: string): Promise<boolean> {
try {
await stat(filePath);
return true;
} catch {
return false;
}
}
async getFileSize(filePath: string): Promise<number> {
const stats = await stat(filePath);
return stats.size;
}
joinPath(...paths: string[]): string {
return path.join(...paths);
}
normalizePath(filePath: string): string {
return path.normalize(filePath);
}
getBasename(filePath: string): string {
return path.basename(filePath);
}
}