@sidekick-coder/db
Version:
Cli Tool to manipulate data from diferent sources
112 lines (109 loc) • 2.79 kB
JavaScript
import { dirname, basename } from 'path';
// src/core/filesystem/createFsFake.ts
function createFsFake() {
const entries = /* @__PURE__ */ new Map();
entries.set("/", {
name: "/",
type: "directory",
path: "/"
});
const existsSync = (path) => {
if (path === "/") return true;
const result = entries.has(path);
return result;
};
const exists = async (path) => {
return existsSync(path);
};
const read = async (path) => {
const entry = entries.get(path);
if (!entry || entry.type !== "file") {
return null;
}
return entry.content;
};
const readSync = (path) => {
const entry = entries.get(path);
if (!entry || entry.type !== "file") {
return null;
}
return entry.content;
};
const readdirSync = (path) => {
const directory = entries.get(path);
if (!directory || directory.type !== "directory") {
return [];
}
const result = Array.from(entries.values()).filter((entry) => dirname(entry.path) === directory.path).map((entry) => entry.name);
return result;
};
const readdir = async (path) => {
return readdirSync(path);
};
const writeSync = (path, content) => {
const directory = entries.get(dirname(path));
if (!directory || directory.type !== "directory") {
throw new Error(`Directory ${dirname(path)} does not exist`);
}
entries.set(path, {
name: basename(path),
path,
type: "file",
content
});
};
const write = async (path, content) => {
return writeSync(path, content);
};
const mkdirSync = (path) => {
const directory = entries.get(dirname(path));
if (!directory || directory.type !== "directory") {
throw new Error(`Directory ${dirname(path)} does not exist`);
}
entries.set(path, {
name: basename(path),
path,
type: "directory"
});
};
const mkdir = async (path) => {
entries.set(path, {
name: basename(path),
path,
type: "directory"
});
};
const removeSync = (path) => {
const entry = entries.get(path);
if (!entry) return;
const keys = [path];
if ((entry == null ? void 0 : entry.type) === "directory") {
Array.from(entries.keys()).filter((key) => key.startsWith(path)).forEach((key) => keys.push(key));
}
keys.forEach((key) => entries.delete(key));
};
const remove = async (path) => {
removeSync(path);
};
const removeAt = async (filepath, milliseconds) => {
setTimeout(() => removeSync(filepath), milliseconds);
return true;
};
return {
entries,
exists,
existsSync,
read,
readSync,
readdir,
readdirSync,
write,
writeSync,
mkdir,
mkdirSync,
remove,
removeSync,
removeAt
};
}
export { createFsFake };