@synet/fs
Version:
Robust, battle-tested filesystem abstraction for Node.js
71 lines (70 loc) • 2.12 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeFileSystem = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
/**
* Node.js implementation of FileSystem interface
*/
class NodeFileSystem {
existsSync(path) {
return node_fs_1.default.existsSync(path);
}
readFileSync(path) {
return node_fs_1.default.readFileSync(path, "utf-8");
}
writeFileSync(path, data) {
try {
node_fs_1.default.writeFileSync(path, data);
}
catch (error) {
if (error instanceof Error &&
"code" in error &&
error.code !== "EEXIST") {
throw error;
}
}
}
deleteFileSync(path) {
if (node_fs_1.default.existsSync(path)) {
node_fs_1.default.unlinkSync(path);
}
}
deleteDirSync(path) {
if (node_fs_1.default.existsSync(path)) {
node_fs_1.default.rmSync(path, { recursive: true });
}
}
ensureDirSync(dirPath) {
if (!node_fs_1.default.existsSync(dirPath)) {
node_fs_1.default.mkdirSync(dirPath, { recursive: true });
}
}
readDirSync(dirPath) {
return node_fs_1.default.readdirSync(dirPath);
}
chmodSync(path, mode) {
try {
node_fs_1.default.chmodSync(path, mode);
}
catch (error) {
throw new Error(`Failed to change permissions for ${path}: ${error}`);
}
}
statSync(path) {
const stats = node_fs_1.default.statSync(path);
return {
isFile: () => stats.isFile(),
isDirectory: () => stats.isDirectory(),
isSymbolicLink: () => stats.isSymbolicLink(),
size: stats.size,
mtime: stats.mtime,
ctime: stats.ctime,
atime: stats.atime,
mode: stats.mode,
};
}
}
exports.NodeFileSystem = NodeFileSystem;