import fs from "fs";
import path from "path";

export interface FileSystem {
	exists(path: string): boolean;
	readFile(path: string, encoding?: BufferEncoding): Promise<string>;
}

export class RealFileSystem implements FileSystem {
	constructor(private readonly root: string) {}

	exists(p: string): boolean {
		return fs.existsSync(path.join(this.root, p));
	}

	async readFile(p: string, encoding?: BufferEncoding): Promise<string> {
		const contents = await fs.promises.readFile(path.join(this.root, p));
		return contents.toString(encoding);
	}
}
