43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import { IFileSystem } from "@/ports";
|
|
|
|
export class InMemoryFileSystem implements IFileSystem {
|
|
#files = new Map<string, string>();
|
|
|
|
reset(): void {
|
|
this.#files.clear();
|
|
}
|
|
|
|
async readFile(path: string): Promise<string> {
|
|
const content = this.#files.get(path);
|
|
if (content === undefined) {
|
|
throw new Error(`ENOENT: no such file: ${path}`);
|
|
}
|
|
return content;
|
|
}
|
|
|
|
async writeFile(path: string, content: string): Promise<void> {
|
|
this.#files.set(path, content);
|
|
}
|
|
|
|
async deleteFile(path: string): Promise<void> {
|
|
this.#files.delete(path);
|
|
}
|
|
|
|
async readdir(path: string): Promise<string[]> {
|
|
const prefix = path.endsWith("/") || path.endsWith("\\") ? path : path + "/";
|
|
const seen = new Set<string>();
|
|
for (const key of this.#files.keys()) {
|
|
if (key.startsWith(prefix)) {
|
|
const relative = key.slice(prefix.length);
|
|
const top = relative.split(/[/\\]/)[0];
|
|
if (top) seen.add(top);
|
|
}
|
|
}
|
|
return [...seen];
|
|
}
|
|
|
|
async mkdir(_path: string): Promise<void> {
|
|
// no-op for in-memory
|
|
}
|
|
}
|