-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfileSystem.ts
More file actions
104 lines (86 loc) · 3.04 KB
/
fileSystem.ts
File metadata and controls
104 lines (86 loc) · 3.04 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
const mkdir = promisify(fs.mkdir);
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
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 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 isDirectory(dirPath: string): Promise<boolean> {
try {
const stats = await stat(dirPath);
return stats.isDirectory();
} catch {
return false;
}
}
async getDirectoryContents(dirPath: string): Promise<string[]> {
try {
return await readdir(dirPath);
} catch (error) {
throw new Error(`Failed to read directory ${dirPath}: ${error}`);
}
}
async copyFile(source: string, destination: string): Promise<void> {
const content = await this.readFileContent(source);
await this.writeFileContent(destination, content);
}
async calculateFileHash(content: string): Promise<string> {
// GitHub uses the following format: `blob ${content.length}\0${content}`
const blobPrefix = `blob ${Buffer.from(content).length}\0`;
const fullContent = blobPrefix + content;
const encoder = new TextEncoder();
const data = encoder.encode(fullContent);
const hashBuffer = await crypto.subtle.digest('SHA-1', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}
getRelativePath(from: string, to: string): string {
return path.relative(from, to);
}
joinPath(...paths: string[]): string {
return path.join(...paths);
}
normalizePath(filePath: string): string {
return path.normalize(filePath);
}
getExtension(filePath: string): string {
return path.extname(filePath);
}
getBasename(filePath: string): string {
return path.basename(filePath);
}
getDirname(filePath: string): string {
return path.dirname(filePath);
}
}