2022-10-10 12:50:21 +00:00
|
|
|
import { syscall } from "./syscall.ts";
|
2023-01-13 14:41:29 +00:00
|
|
|
import type { FileMeta, ProxyFileSystem } from "./types.ts";
|
2022-07-06 10:17:42 +00:00
|
|
|
|
2023-01-13 14:41:29 +00:00
|
|
|
export class LocalFileSystem implements ProxyFileSystem {
|
|
|
|
constructor(readonly root: string) {
|
|
|
|
}
|
2022-07-06 10:17:42 +00:00
|
|
|
|
2023-01-13 14:41:29 +00:00
|
|
|
readFile(
|
|
|
|
path: string,
|
|
|
|
encoding: "utf8" | "dataurl" = "utf8",
|
|
|
|
): Promise<string> {
|
|
|
|
return syscall("fs.readFile", `${this.root}/${path}`, encoding);
|
|
|
|
}
|
2022-07-06 10:17:42 +00:00
|
|
|
|
2023-01-13 14:41:29 +00:00
|
|
|
async getFileMeta(path: string): Promise<FileMeta> {
|
|
|
|
return this.removeRootDir(
|
|
|
|
await syscall("fs.getFileMeta", `${this.root}/${path}`),
|
|
|
|
);
|
|
|
|
}
|
2022-07-06 10:17:42 +00:00
|
|
|
|
2023-01-13 14:41:29 +00:00
|
|
|
writeFile(
|
|
|
|
path: string,
|
|
|
|
text: string,
|
|
|
|
encoding: "utf8" | "dataurl" = "utf8",
|
|
|
|
): Promise<FileMeta> {
|
|
|
|
return syscall("fs.writeFile", `${this.root}/${path}`, text, encoding);
|
|
|
|
}
|
2022-07-06 10:17:42 +00:00
|
|
|
|
2023-01-13 14:41:29 +00:00
|
|
|
deleteFile(path: string): Promise<void> {
|
|
|
|
return syscall("fs.deleteFile", `${this.root}/${path}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
async listFiles(
|
|
|
|
dirName: string,
|
|
|
|
recursive = false,
|
|
|
|
): Promise<FileMeta[]> {
|
|
|
|
return (await syscall(
|
|
|
|
"fs.listFiles",
|
|
|
|
`${this.root}/${dirName}`,
|
|
|
|
recursive,
|
|
|
|
)).map(this.removeRootDir.bind(this));
|
|
|
|
}
|
2022-07-06 10:17:42 +00:00
|
|
|
|
2023-01-13 14:41:29 +00:00
|
|
|
private removeRootDir(fileMeta: FileMeta): FileMeta {
|
|
|
|
fileMeta.name = fileMeta.name.substring(this.root.length + 1);
|
|
|
|
return fileMeta;
|
|
|
|
}
|
2022-07-06 10:17:42 +00:00
|
|
|
}
|