@loom-io/node-filesystem-adapter
Version:
A file system wrapper for Node.js and Bun
26 lines (25 loc) • 899 B
JavaScript
import { Adapter } from "../core/adapter.js";
import { dirname, basename } from "node:path";
import { Directory, LoomFile } from "@loom-io/core/internal";
import { join as joinPath } from "node:path";
export class FilesystemAdapter {
adapter;
rootdir;
constructor(rootdir = process.cwd()) {
this.rootdir = rootdir;
this.adapter = new Adapter(rootdir);
}
file(path) {
const dir = new Directory(this.adapter, dirname(path));
return new LoomFile(this.adapter, dir, basename(path));
}
dir(path) {
return new Directory(this.adapter, path);
}
getFullPath(dirOrFile, ignoreAdapter = false) {
if (!ignoreAdapter && this.adapter !== dirOrFile.adapter) {
throw new Error("The directory or file does not belong to this adapter");
}
return joinPath(this.rootdir.toString(), dirOrFile.path);
}
}