@@ -1,36 +1,48 @@
|
||||
import { syscall } from "./syscall.ts";
|
||||
import type { FileMeta, ProxyFileSystem } from "./types.ts";
|
||||
|
||||
export type FileMeta = {
|
||||
name: string;
|
||||
lastModified: number;
|
||||
};
|
||||
export class LocalFileSystem implements ProxyFileSystem {
|
||||
constructor(readonly root: string) {
|
||||
}
|
||||
|
||||
export function readFile(
|
||||
path: string,
|
||||
encoding: "utf8" | "dataurl" = "utf8",
|
||||
): Promise<string> {
|
||||
return syscall("fs.readFile", path, encoding);
|
||||
}
|
||||
readFile(
|
||||
path: string,
|
||||
encoding: "utf8" | "dataurl" = "utf8",
|
||||
): Promise<string> {
|
||||
return syscall("fs.readFile", `${this.root}/${path}`, encoding);
|
||||
}
|
||||
|
||||
export function getFileMeta(path: string): Promise<FileMeta> {
|
||||
return syscall("fs.getFileMeta", path);
|
||||
}
|
||||
async getFileMeta(path: string): Promise<FileMeta> {
|
||||
return this.removeRootDir(
|
||||
await syscall("fs.getFileMeta", `${this.root}/${path}`),
|
||||
);
|
||||
}
|
||||
|
||||
export function writeFile(
|
||||
path: string,
|
||||
text: string,
|
||||
encoding: "utf8" | "dataurl" = "utf8",
|
||||
): Promise<FileMeta> {
|
||||
return syscall("fs.writeFile", path, text, encoding);
|
||||
}
|
||||
writeFile(
|
||||
path: string,
|
||||
text: string,
|
||||
encoding: "utf8" | "dataurl" = "utf8",
|
||||
): Promise<FileMeta> {
|
||||
return syscall("fs.writeFile", `${this.root}/${path}`, text, encoding);
|
||||
}
|
||||
|
||||
export function deleteFile(path: string): Promise<void> {
|
||||
return syscall("fs.deleteFile", path);
|
||||
}
|
||||
deleteFile(path: string): Promise<void> {
|
||||
return syscall("fs.deleteFile", `${this.root}/${path}`);
|
||||
}
|
||||
|
||||
export function listFiles(
|
||||
dirName: string,
|
||||
recursive = false,
|
||||
): Promise<FileMeta[]> {
|
||||
return syscall("fs.listFiles", dirName, recursive);
|
||||
async listFiles(
|
||||
dirName: string,
|
||||
recursive = false,
|
||||
): Promise<FileMeta[]> {
|
||||
return (await syscall(
|
||||
"fs.listFiles",
|
||||
`${this.root}/${dirName}`,
|
||||
recursive,
|
||||
)).map(this.removeRootDir.bind(this));
|
||||
}
|
||||
|
||||
private removeRootDir(fileMeta: FileMeta): FileMeta {
|
||||
fileMeta.name = fileMeta.name.substring(this.root.length + 1);
|
||||
return fileMeta;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export * as asset from "./asset.ts";
|
||||
export * as events from "./event.ts";
|
||||
export * as fs from "./fs.ts";
|
||||
// export * as fs from "./fs.ts";
|
||||
export { LocalFileSystem } from "./fs.ts";
|
||||
export * as sandbox from "./sandbox.ts";
|
||||
export * as fulltext from "./fulltext.ts";
|
||||
export * as shell from "./shell.ts";
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
export type FileMeta = {
|
||||
name: string;
|
||||
lastModified: number;
|
||||
};
|
||||
|
||||
export interface ProxyFileSystem {
|
||||
readFile(
|
||||
path: string,
|
||||
encoding: "utf8" | "dataurl",
|
||||
): Promise<string>;
|
||||
|
||||
getFileMeta(path: string): Promise<FileMeta>;
|
||||
|
||||
writeFile(
|
||||
path: string,
|
||||
text: string,
|
||||
encoding: "utf8" | "dataurl",
|
||||
): Promise<FileMeta>;
|
||||
|
||||
deleteFile(path: string): Promise<void>;
|
||||
|
||||
listFiles(
|
||||
path: string,
|
||||
): Promise<FileMeta[]>;
|
||||
}
|
||||
Reference in New Issue
Block a user