1
0
silverbullet/plug-api/plugos-syscall/fs.ts

49 lines
1.2 KiB
TypeScript
Raw Normal View History

import { syscall } from "./syscall.ts";
2023-01-13 14:41:29 +00:00
import type { FileMeta, ProxyFileSystem } from "./types.ts";
2023-01-13 14:41:29 +00:00
export class LocalFileSystem implements ProxyFileSystem {
constructor(readonly root: string) {
}
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);
}
2023-01-13 14:41:29 +00:00
async getFileMeta(path: string): Promise<FileMeta> {
return this.removeRootDir(
await syscall("fs.getFileMeta", `${this.root}/${path}`),
);
}
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);
}
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));
}
2023-01-13 14:41:29 +00:00
private removeRootDir(fileMeta: FileMeta): FileMeta {
fileMeta.name = fileMeta.name.substring(this.root.length + 1);
return fileMeta;
}
}